江苏做网站的公司,哪家做网站,中文网站建设英文网站建设,手机端网站源码在使用 WebForm 技术开发网站的时候#xff0c;微软就提供了 Form 身份认证#xff0c;这使得登录认证简单了许多#xff0c;不同于 WebForm 以及后来的 Asp.Net Mvc#xff0c;Asp.Net Core 中的身份认证与之前相比使用更加便捷#xff0c;本文介绍 Asp.Net Core 2.0 多角… 在使用 WebForm 技术开发网站的时候微软就提供了 Form 身份认证这使得登录认证简单了许多不同于 WebForm 以及后来的 Asp.Net MvcAsp.Net Core 中的身份认证与之前相比使用更加便捷本文介绍 Asp.Net Core 2.0 多角色授权认证首先我们需要在 Startup.cs 中开启授权认证相关模块中间件代码如下 services.AddAuthentication(options{options.DefaultChallengeScheme CookieAuthenticationDefaults.AuthenticationScheme;options.DefaultAuthenticateScheme CookieAuthenticationDefaults.AuthenticationScheme;})
.AddCookie(options {options.LoginPath /Account/;options.Cookie.HttpOnly true;});
services.AddTransientHttpContextAccessor();app.UseAuthentication(); 之后我们在登录模块编写多角色登录逻辑代码如下 [HttpPost]
public async TaskIActionResult Login(string userCode, string userPassword, int userType 0, string returnUrl )
{if ((userCode.Trim().ToLower() admin || userCode.Trim().ToLower() user) userPassword.Trim().ToLower() 123456){var claimsIdentity new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);claimsIdentity.AddClaim(new Claim(ClaimTypes.Sid, userCode));if (userType RoleTypeEnum.UserType_Admin){claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.Admin));}else{claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.User));}var claimsPrincipal new ClaimsPrincipal(claimsIdentity);await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties{ExpiresUtc DateTime.UtcNow.AddMinutes(20)});if (!string.IsNullOrEmpty(returnUrl)){return this.Redirect(returnUrl);}else{if (userType RoleTypeEnum.UserType_Admin){return this.Redirect(Url.Action(Index, Home, new { area Admin }));}else{return this.Redirect(Url.Action(Index, Home, new { area User }));}}}else{return this.Content(string.Format(scriptalert(用户名或者密码错误);location.href{0}/script, Url.Action(Index, Account)), text/html;charsetutf8);}
} 本例只提供管理和普通用户两种角色类别可以根据情况自由添加接着我们就可以在相关授权模块添加 Authorize 元属性来进行角色授权代码如下 // 管理员模块
[Authorize(Roles RoleTypeEnum.Authorize_Admin)]
[Area(Admin)]
public class BaseController : Controller
{protected string userCode;public BaseController(IHttpContextAccessor contextAccessor){this.userCode contextAccessor.HttpContext.User.FindFirst(ClaimTypes.Sid).Value;}protected void InitCookieViewData(){ViewData.Add(UserCode, this.userCode);}
}
// 用户模块
[Authorize(Roles RoleTypeEnum.Authorize_User)]
[Area(User)]
public class BaseController : Controller
{protected string userCode;public BaseController(IHttpContextAccessor contextAccessor){this.userCode contextAccessor.HttpContext.User.FindFirst(ClaimTypes.Sid).Value;}protected void InitCookieViewData(){ViewData.Add(UserCode, this.userCode);}
} 到此多角色授权认证已经结束而且我们也获得了登录的角色信息退出登录的代码如下 public async TaskIActionResult Logout()
{await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);return this.Redirect(Url.Action(Index, Account, new { area }));
} 本文已提供案例下载地址。 原文地址https://www.liziwu.net/topic/31.html.NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注