asp.net core2.2多用户验证与授权示例详解

所属分类: 网络编程 / ASP.NET 阅读数: 952
收藏 0 赞 0 分享

前言

asp.net core2.2 用户验证 和授权有很详细和特贴心的介绍,我感兴趣的主要是这两篇:

我的项目有两类用户:

  • 微信公众号用户,用户名为公众号的openid
  • 企业微信的用户,用户名为企业微信的userid

每类用户中部分人员具有“Admin”角色

因为企业微信的用户有可能同时是微信公众号用户,即一个人两个名,所以需要多用户验证和授权。咱用代码说话最简洁,如下所示:

public class DemoController : Controller
{
 /// <summary>
 /// 企业微信用户使用的模块
 /// </summary>
 /// <returns></returns>
 public IActionResult Work()
 {
 return Content(User.Identity.Name +User.IsInRole("Admin"));
 }
 /// <summary>
 /// 企业微信管理员使用的模块
 /// </summary>
 /// <returns></returns>
 public IActionResult WorkAdmin()
 {
 return Content(User.Identity.Name + User.IsInRole("Admin"));
 }
 /// <summary>
 /// 微信公众号用户使用的模块
 /// </summary>
 /// <returns></returns>
 public IActionResult Mp()
 {
 return Content(User.Identity.Name + User.IsInRole("Admin"));
 }
 /// <summary>
 /// 微信公众号管理员使用的模块
 /// </summary>
 /// <returns></returns>
 public IActionResult MpAdmin()
 {
 return Content(User.Identity.Name + User.IsInRole("Admin"));
 }
}

下面咱一步一步实现。

第一步 改造类Startup

修改ConfigureServices方法,加入以下代码

  services.AddAuthentication
   (
   "Work" //就是设置一个缺省的cookie验证的名字,缺省的意思就是需要写的时候可以不写。另外很多时候用CookieAuthenticationDefaults.AuthenticationScheme,这玩意就是字符串常量“Cookies”,
   )
   .AddCookie
   (
   "Work", //cookie验证的名字,“Work”可以省略,因为是缺省名
   option =>
   {
    option.LoginPath = new PathString("/Demo/WorkLogin"); //设置验证的路径
    option.AccessDeniedPath= new PathString("/Demo/WorkDenied");//设置无授权访问跳转的路径
   }).AddCookie("Mp", option =>
   {
    option.LoginPath = new PathString("/Demo/MpLogin");
    option.AccessDeniedPath = new PathString("/Demo/MpDenied");
   });

修改Configure方法,加入以下代码

app.UseAuthentication();

第二步 添加验证

 public async Task WorkLogin(string returnUrl)
 {
  var claims = new List<Claim>
  {
   new Claim(ClaimTypes.Name, "UserId"),
   new Claim(ClaimTypes.Role, "Admin") //如果是管理员
  };

  var claimsIdentity = new ClaimsIdentity(claims, "Work");//“,"Work"”可以省略,因为是缺省名

  var authProperties = new AuthenticationProperties
  {
   AllowRefresh = true,
   //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), 
   // The time at which the authentication ticket expires. A 
   // value set here overrides the ExpireTimeSpan option of 
   // CookieAuthenticationOptions set with AddCookie.
   IsPersistent = false, //持久化保存,到底什么意思我也不太清楚,哪位兄弟清楚的话,盼解释
   //IssuedUtc = <DateTimeOffset>,
   // The time at which the authentication ticket was issued.
   RedirectUri = returnUrl ?? "/Demo/Work"
  };

  await HttpContext.SignInAsync("Work", new ClaimsPrincipal(claimsIdentity), authProperties);
 }
 public IActionResult WorkDenied()
 {
  return Forbid();
 }


 public async Task MpLogin(string returnUrl)
 {
  var claims = new List<Claim>
  {
   new Claim(ClaimTypes.Name, "OpenId"),
   new Claim(ClaimTypes.Role, "Admin") //如果是管理员
  };

  var claimsIdentity = new ClaimsIdentity(claims, "Mp");//“,"Mp"”不能省略,因为不是缺省名

  var authProperties = new AuthenticationProperties
  {
   AllowRefresh = true,
   IsPersistent = false,
   RedirectUri = returnUrl ?? "/Demo/Mp"
  };

  await HttpContext.SignInAsync("Mp", new ClaimsPrincipal(claimsIdentity), authProperties);
 }
 public IActionResult MpDenied()
 {
  return Forbid();
 }

第三步 添加授权

就是在对应的Action前面加[Authorize]

 /// <summary>
 /// 企业微信用户使用的模块
 /// </summary>
 /// <returns></returns>
 [Authorize(
  AuthenticationSchemes ="Work" //缺省名可以省略
  )]
 public IActionResult Work()
 {
  return Content(User.Identity.Name + User.IsInRole("Admin"));
 }
 /// <summary>
 /// 企业微信管理员使用的模块
 /// </summary>
 /// <returns></returns>
 [Authorize(AuthenticationSchemes ="Work",Roles ="Admin")]
 public IActionResult WorkAdmin()
 {
  return Content(User.Identity.Name + User.IsInRole("Admin"));
 }
 /// <summary>
 /// 微信公众号用户使用的模块
 /// </summary>
 /// <returns></returns>
 [Authorize(AuthenticationSchemes ="Mp")]
 public IActionResult Mp()
 {
  return Content(User.Identity.Name + User.IsInRole("Admin"));
 }
 /// <summary>
 /// 微信公众号管理员使用的模块
 /// </summary>
 /// <returns></returns>
 [Authorize(AuthenticationSchemes ="Mp",Roles ="Admin")]
 public IActionResult MpAdmin()
 {
  return Content(User.Identity.Name + User.IsInRole("Admin"));
 }

Ctrl+F5运行,截屏如下:


最后,讲讲碰到的坑和求助


一开始的验证的代码如下:

 public async Task<IActionResult> Login(string returnUrl)
 {
  var claims = new List<Claim>
  {
   new Claim(ClaimTypes.Name, "UserId"),
   new Claim(ClaimTypes.Role, "Admin") //如果是管理员
  };

  var claimsIdentity = new ClaimsIdentity(claims, "Work");//“,"Work"”可以省略,因为是缺省名

  var authProperties = new AuthenticationProperties
  {
   //AllowRefresh = true,
   //IsPersistent = false,
   //RedirectUri 
  };

  await HttpContext.SignInAsync("Work", new ClaimsPrincipal(claimsIdentity), authProperties);

  return Content("OK");
 }
  • 返回类型为Task<IActionResult> ,因为懒得写View,顺手写了句return Content("OK");
  • 从网站复制过来代码,AuthenticationProperties没有设置任何内容

运行起来以后不停的调用login,百度了半天,改了各种代码,最后把return Content("OK");改成return RedirectToAction("Index");一切OK!

揣摩原因可能是当 return Content("OK");时,自动调用AuthenticationProperties的RedirectUri,而RedirectUri为空时,自动调用自己。也不知道对不对。

这时候重视起RedirectUri,本来就要返回到returnUrl,是不是给RedirectUri赋值returnUrl就能自动跳转?

确实,return Content("OK");时候自动跳转了,return RedirectToAction("Index");无效。

最后把Task<IActionResult> 改成Task ,把return ...删除,一切完美!(弱弱问一句,是不是原来就应该这样写?我一直在走弯路?)

求助

User有属性Identities,看起来可以有多个Identity,如何有?

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多