Asp.Net Core-Authorize 特性

在本章中,我们将讨论Authorize特性。 到目前为止,在我们的应用程序中,我们允许匿名用户做任何事情。 他们可以编辑员工详细信息,查看详细信息,但我们没有创建新员工的功能。 让我们先添加创建功能,然后我们将使用Authorize属性限制用户访问。

我们需要首先在Views→Home文件夹中创建一个新的MVC View页面,并调用Create.cshtml然后添加以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@model Employee 
@{ 
    ViewBag.Title =  "Create"
<h1>Create</h1>  
@ using  (Html.BeginForm()) { 
    <div> 
       @Html.LabelFor(m => m.Name) 
       @Html.EditorFor(m => m.Name) 
       @Html.ValidationMessageFor(m => m.Name) 
    </div> 
    
    <div> 
       <input type =  "submit"  value =  "Save"  /> 
    </div> 
}

现在我们将在HomeController中为POST和GET添加action方法,如下面的程序所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[HttpGet] 
public  ViewResult Create() { 
    return  View(); 
}  
[HttpPost] 
public  IActionResult Create(EmployeeEditViewModel model) {
    if  (ModelState.IsValid) { 
       var employee =  new  Employee(); 
       employee.Name = model.Name;  
       var context =  new  FirstAppDemoDbContext(); 
       
       SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
       sqlData.Add(employee);  
       return  RedirectToAction( "Details" new  { id = employee.Id }); 
   
    return  View(); 
}

让我们在Index.cshtml文件中添加一个指向Create View的链接,如下面的程序所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@model HomePageViewModel  
@{  
    ViewBag.Title =  "Home"
<h1>Welcome!</h1> 
<table> 
    @ foreach  (var employee  in  Model.Employees) { 
    <tr> 
       <td>@employee.Name 
          <td> 
             <a asp-controller =  "Home"  asp-action =  "Details" 
                asp-routeid =  "@employee.Id" >Details</a> 
             
             <a asp-controller =  "Home"  asp-action =  "Edit" 
                asp-routeid =  "@employee.Id" >Edit</a> 
          </td> 
       </tr> 
   
</table>  
<div> 
    <a asp-action =  "Create" >Create</a>
</div>

运行应用程序; 您将看到以下页面。


在主页上,您将看到Create链接。 当您单击Create链接,它将带您到创建视图。


在名称输入框中输入名称,然后单击保存按钮。


现在,您将看到新添加的员工的详细信息视图。 让我们点击Home链接。


在此应用程序中,每个用户都可以创建,编辑员工,每个人都可以看到详细视图。 我们希望更改此行为,以便将来匿名用户只能看到主页上的员工列表,但每个其他操作都需要用户标识自己并登录。我们可以使用Authorize属性来完成此操作。

您可以将Authorize属性放在单个控制器上或控制器中的单个操作上。

1
2
3
4
[Authorize] 
public  class  HomeController : Controller { 
    //....  
}

当我们将Authorize属性放在控制器本身上时,authorize属性应用于其中的所有动作。

MVC框架不允许请求达到受此属性保护的操作,除非用户通过授权检查。

默认情况下,如果不使用其他参数,则只检查Authorize属性将检查以确保用户已登录,以便我们知道其身份。

但您可以使用参数指定任何喜欢的自定义授权策略。

还有一个AllowAnonymous属性。 当您想要使用控制器上的Authorize属性来保护所有操作时,此属性非常有用,但是此单个操作或一个或两个要取消保护的操作,并允许匿名用户访问该特定操作。

1
2
3
4
5
6
7
8
9
10
[AllowAnonymous] 
public  ViewResult Index() { 
    var model =  new  HomePageViewModel(); 
    
    using  (var context =  new  FirstAppDemoDbContext()) { 
       SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
       model.Employees = sqlData.GetAll(); 
    }  
    return  View(model); 
}

让我们在我们的应用程序中尝试这些属性。 在运行的应用程序中,匿名用户可以编辑员工信息。


我们想要更改此选项,并强制用户在编辑员工之前登录并识别自己。 现在让我们进入HomeController。 我们将在此限制访问一个或两个操作。 我们可以始终将Authorize属性放在我们要保护的那些特定操作上。 我们还可以将Authorize属性放在控制器本身上,并且此Authorize属性位于Microsoft.AspNet.Authorization命名空间中。

我们现在将使用Authorize属性,并强制用户识别自己进入此控制器,除了主页,如下面的程序所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[Authorize] 
public  class  HomeController : Controller { 
    [AllowAnonymous] 
    public  ViewResult Index() { 
       var model =  new  HomePageViewModel(); 
       using  (var context =  new  FirstAppDemoDbContext()) { 
          SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
          model.Employees = sqlData.GetAll(); 
       }  
       return  View(model); 
    }  
    public  IActionResult Details( int  id) {  
       var context =  new  FirstAppDemoDbContext(); 
       SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
       var model = sqlData.Get(id); 
         
       if  (model ==  null ) { 
          return  RedirectToAction( "Index" ); 
      
       return  View(model); 
   
    [HttpGet] 
    public  IActionResult Edit( int  id) { 
       var context =  new  FirstAppDemoDbContext(); 
       SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
       var model = sqlData.Get(id); 
         
       if  (model ==  null ) { 
          return  RedirectToAction( "Index" ); 
      
       return  View(model); 
    }  
    [HttpPost] 
    public  IActionResult Edit( int  id, EmployeeEditViewModel input) { 
       var context =  new  FirstAppDemoDbContext(); 
       SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
       var employee = sqlData.Get(id); 
         
       if  (employee !=  null  && ModelState.IsValid) { 
          employee.Name = input.Name; 
          context.SaveChanges();  
          return  RedirectToAction( "Details" new  { id = employee.Id }); 
      
       return  View(employee);
    }  
    [HttpGet] 
    public  ViewResult Create() { 
       return  View(); 
    }  
    [HttpPost] 
    public  IActionResult Create(EmployeeEditViewModel model) { 
       if  (ModelState.IsValid) { 
          var employee =  new  Employee(); 
          employee.Name = model.Name;  
          var context =  new  FirstAppDemoDbContext(); 
          
          SQLEmployeeData sqlData =  new  SQLEmployeeData(context); 
          sqlData.Add(employee);  
          return  RedirectToAction( "Details" new  { id = employee.Id }); 
      
       return  View(); 
   
}

显示雇员列表的主页或Index.cshtml文件具有AllowAnonymous属性。 让我们现在运行你的应用程序。


按F12键,打开开发工具。 现在,转到网络选项卡。


我们想在开发者工具中看到一些东西,所以我们可以看到它是如何工作的。 当您单击编辑链接时,您将看到一个空白页。


如果您查看开发人员工具,您将看到从服务器返回的HTTP状态代码是401状态代码。


401状态代码告诉浏览器该请求不允许通过,因为它缺少有效的身份验证凭据。 这告诉我们Authorize属性正在工作。

同样,当您单击主页上的创建链接时,您将看到与以下屏幕截图中所示的相同的错误。


  • 这里,坏的部分是用户留在空白页面上,除非他们有开发者工具打开,他们可能不知道这是一个身份验证问题。

  • 这就是Identity框架可以介入和帮助的地方。

  • Identity框架可以检测应用程序的某一部分何时希望返回401状态代码,因为用户不允许到达那里,Identity框架可以将其转换为登录页面,并允许用户克服这个问题。

  • 一旦我们得到Identity框架的安装和配置,我们将看到它是如何工作的。

  • 但是现在,我们可以看到Authorize属性正在工作。

版权声明:本站所有教程均为本站原创或翻译,转载请注明出处,请尊重他人劳动果实。请记住本站地址:www.yuanjiaocheng.net (猿教程) 作者:卿文刚
本文标题: Asp.Net Core-Authorize 特性 
本文地址:http://www.yuanjiaocheng.net/ASPNET-CORE/core-authorize-attribute.html
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值