Asp.Net Core 2.0的认证和授权

转自:https://www.cnblogs.com/axzxs2001/p/7482771.html 

在asp.net core中,微软提供了基于认证(Authentication)和授权(Authorization)的方式,来实现权限管理的,本篇博文,介绍基于固定角色的权限管理和自定义角色权限管理,本文内容,更适合传统行业的BS应用,而非互联网应用。

在asp.net core中,我们认证(Authentication)通常是在Login的Post Action中进行用户名或密码来验证用户是否正确,如果通过验证,即该用户就会获得一个或几个特定的角色,通过ClaimTypes.Role来存储角色,从而当一个请求到达时,用这个角色和Controller或Action上加的特性 [Authorize(Roles = "admin,system")]来授权是否有权访问该Action。本文中的自定义角色,会把验证放在中间件中进行处理。

 一、固定角色:

即把角色与具体的Controller或Action直接关联起来,整个系统中的角色是固定的,每种角色可以访问那些Controller或Action也是固定的,这做法比较适合小型项目,角色分工非常明确的项目。

项目代码:

https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86/RolePrivilegeManagement

始于startup.cs

需要在ConfigureServices中注入Cookie的相关信息,options是CookieAuthenticationOptions,关于这个类型提供如下属性,可参考:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x

 

它提供了登录的一些信息,或登录生成Cookie的一些信息,用以后

Asp.Net Core 2.0的认证和授权
 1         public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddMvc();
 4             //添加认证Cookie信息
 5             services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
 6              .AddCookie(options =>
 7              {
 8                  options.LoginPath = new PathString("/login");
 9                  options.AccessDeniedPath = new PathString("/denied");
10              });
11         }
12 
13         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
14         {
15             if (env.IsDevelopment())
16             {
17                 app.UseDeveloperExceptionPage();
18                 app.UseBrowserLink();
19             }
20             else
21             {
22                 app.UseExceptionHandler("/Home/Error");
23             }
24             app.UseStaticFiles();
25             //验证中间件
26             app.UseAuthentication();
27             app.UseMvc(routes =>
28             {
29                 routes.MapRoute(
30                     name: "default",
31                     template: "{controller=Home}/{action=Index}/{id?}");
32             });
33         }
Asp.Net Core 2.0的认证和授权

HomeController.cs

对于Login Get的Action,把returnUrl用户想要访问的地址(有可能用户记录下想要访问的url了,但系统会转到登录页,登录成功后直接跳转到想要访问的returnUrl页)

对于Login Post的Action,验证用户密和密码,成功能,定义一个ClaimsIdentity,把用户名和角色,和用户姓名的声明都添回进来(这个角色,就是用来验证可访问action的角色 )作来该用户标识,接下来调用HttpContext.SignInAsync进行登录,注意此方法的第一个参数,必需与StartUp.cs中services.AddAuthentication的参数相同,AddAuthentication是设置登录,SigninAsync是按设置参数进行登录

对于Logout Get的Action,是退出登录

HomeController上的[Authorize(Roles=”admin,system”)]角色和权限的关系时,所有Action只有admin和system两个角色能访问到,About上的[Authorize(Roles=”admin”)]声明这个action只能admin角色访问,Contact上的[Authorize(Roles=”system”)]声明这个action只能system角色访问,如果action上声明的是[AllowAnomymous],说明不受授权管理,可以直接访问。

Asp.Net Core 2.0的认证和授权
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore.Mvc;
 7 using RolePrivilegeManagement.Models;
 8 using System.Security.Claims;
 9 using Microsoft.AspNetCore.Authentication;
10 using Microsoft.AspNetCore.Authentication.Cookies;
11 using Microsoft.AspNetCore.Authorization;
12 
13 namespace RolePrivilegeManagement.Controllers
14 {
15     [Authorize(Roles = "admin,system")]
16     public class HomeController : Controller
17     {
18         public IActionResult Index()
19         {
20             return View();
21         }
22         [Authorize(Roles = "admin")]
23         public IActionResult About()
24         {
25             ViewData["Message"] = "Your application description page.";
26             return View();
27         }
28         [Authorize(Roles = "system")]
29         public IActionResult Contact()
30         {
31             ViewData["Message"] = "Your contact page.";
32             return View();
33         }
34         public IActionResult Error()
35         {
36             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
37         }
38         [AllowAnonymous]
39         [HttpGet("login")]
40         public IActionResult Login(string returnUrl = null)
41         {
42             TempData["returnUrl"] = returnUrl;
43             return View();
44         }
45         [AllowAnonymous]
46         [HttpPost("login")]
47         public async Task<IActionResult> Login(string userName, string password, string returnUrl = null)
48         {
49             var list = new List<dynamic> {
50                 new { UserName = "gsw", Password = "111111", Role = "admin" },
51                 new { UserName = "aaa", Password = "222222", Role = "system" }
52             };
53             var user = list.SingleOrDefault(s => s.UserName == userName && s.Password == password);
54             if (user!=null)
55             {
56                 //用户标识
57                 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
58                 identity.AddClaim(new Claim(ClaimTypes.Sid, userName));
59                 identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
60                 identity.AddClaim(new Claim(ClaimTypes.Role, user.Role));
61                 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
62                 if (returnUrl == null)
63                 {
64                     returnUrl = TempData["returnUrl"]?.ToString();
65                 }
66                 if (returnUrl != null)
67                 {
68                     return Redirect(returnUrl);
69                 }
70                 else
71                 {
72                     return RedirectToAction(nameof(HomeController.Index), "Home");
73                 }
74             }
75             else
76             {
77                 const string badUserNameOrPasswordMessage = "用户名或密码错误!";
78                 return BadRequest(badUserNameOrPasswordMessage);
79             }
80         }
81         [HttpGet("logout")]
82         public async Task<IActionResult> Logout()
83         {
84             await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
85             return RedirectToAction("Index", "Home");
86         }
87         [AllowAnonymous]
88         [HttpGet("denied")]
89         public IActionResult Denied()
90         {
91             return View();
92         }
93     }
94 }
Asp.Net Core 2.0的认证和授权

前端_Layout.cshtml布局页,在登录成功后的任何页面都可以用@User.Identity.Name就可以获取用户姓名,同时用@User.Claims.SingleOrDefault(s=>s.Type== System.Security.Claims.ClaimTypes.Sid).Value可以获取用户名或角色。

Asp.Net Core 2.0的认证和授权
 1    <nav class="navbar navbar-inverse navbar-fixed-top">
 2         <div class="container">
 3             <div class="navbar-header">
 4                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
 5                     <span class="sr-only">Toggle navigation</span>
 6                     <span class="icon-bar"></span>
 7                     <span class="icon-bar"></span>
 8                     <span class="icon-bar"></span>
 9                 </button>
10                 <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">RolePrivilegeManagement</a>
11             </div>
12             <div class="navbar-collapse collapse">
13                 <ul class="nav navbar-nav">
14                     <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
15                     <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
16                     <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
17                 </ul>
18                 <ul class="" style="float:right; margin:0;">
19                     <li style="overflow:hidden;">
20                         <div style="float:left;line-height:50px;margin-right:10px;">
21                             <span style="color:#ffffff">当前用户:@User.Identity.Name</span>
22                         </div>
23                         <div style="float:left;line-height:50px;">
24                             <a asp-area="" asp-controller="Home" asp-action="Logout">注销</a>
25                         </div>
26                     </li>
27                 </ul>
28             </div>
29         </div>
30     </nav>
Asp.Net Core 2.0的认证和授权

现在可以用chrome运行了,进行登录页后F12,查看Network—Cookies,可以看到有一个Cookie,这个是记录returnUrl的Cookie,是否记得HomeController.cs中的Login Get的Action中代码:TempData["returnUrl"] = returnUrl;这个TempData最后转成了一个Cookie返回到客户端了,如下图:

Asp.Net Core 2.0的认证和授权

输入用户名,密码登录,再次查看Cookies,发现多了一个.AspNetCore.Cookies,即把用户验证信息加密码保存在了这个Cookie中,当跳转到别的页面时,这两个Cookie会继续在客户端和服务传送,用以验证用户角色。

Asp.Net Core 2.0的认证和授权