.NET Core中鉴权 Authentication Authorization

  • Authentication: 鉴定身份信息,例如用户有没有登录,用户基本信息

  • Authorization: 判定用户有没有权限

使用框架提供的Cookie鉴权方式

1.首先在服务容器注入鉴权服务和Cookie服务支持

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;//不能少
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = "Cookie/Login";
})
.AddCookie(options =>{});

2.注册鉴权和授权中间件,用于在管道中调用拦截校验鉴权和授权

app.UseAuthentication();
app.UseAuthorization();         

3.在控制器引入特性 [Authorize] ,调用登录接口时使用HttpContext.SignInAsync()写入鉴权信息

        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> LoginAsync(string name, string password)
        {
            //用户名密码不正确直接返回
            if (!"Admin".Equals(name) || !"123456".Equals(password))
            {
                return new JsonResult(new { Result = false, Message = "登录失败" });
            }

            var claimIdentity = new ClaimsIdentity("Cookie");
            claimIdentity.AddClaim(new Claim(ClaimTypes.Name, name));
            claimIdentity.AddClaim(new Claim(ClaimTypes.Email, "MyEmail@qq.com"));
            claimIdentity.AddClaim(new Claim(ClaimTypes.System, "EmployeeManager"));

            var Properties = new AuthenticationProperties
            {
                ExpiresUtc = DateTime.UtcNow.AddMinutes(30),
            };

            //写入鉴权信息
            await base.HttpContext.SignInAsync(new ClaimsPrincipal(claimIdentity), Properties);
            return new JsonResult(new { Result = true, Message = "登录成功" });
        }

4.因为调用 HttpContext.AuthenticateAsync() 获取鉴权的步骤,由第二部注册的中间件AuthenticationMiddleware已经替我们完成,所以可以直接在控制器内部获取HttpContext.User信息,系统提供的相对于自己实现的,框架帮我们封装了获取鉴权信息,并把它加入管道中,而不用每次在控制器中手动获取鉴权信息。

        [HttpGet]
        public IActionResult Authentication()
        {
            //这里由中间件管道已经实现了鉴权信息取值
            var CookiesInfo = base.HttpContext.User;
            if (CookiesInfo != null)
            {
                return new JsonResult(new { Result = true, Message = $"鉴权认证成功,用户已登录" });
            }
            return new JsonResult(new { Result = true, Message = $"鉴权认证失败,用户未登录" });
        }

相关推荐

  1. .NET Core Authentication Authorization

    2023-12-19 12:32:04       61 阅读
  2. 通过SpringCloudGateway的GlobalFilter实现过滤

    2023-12-19 12:32:04       29 阅读
  3. HBase设计以及Kerberos方法

    2023-12-19 12:32:04       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-19 12:32:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-19 12:32:04       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-19 12:32:04       82 阅读
  4. Python语言-面向对象

    2023-12-19 12:32:04       91 阅读

热门阅读

  1. ASP.NET Core Web API 缓存

    2023-12-19 12:32:04       66 阅读
  2. 树莓派通过网线连接电脑并且设置设置链接wifi

    2023-12-19 12:32:04       67 阅读
  3. Upgrading GitHub.com to MySQL 8.0

    2023-12-19 12:32:04       54 阅读
  4. iOS中宿主APP与录屏扩展进程数据传递方式

    2023-12-19 12:32:04       49 阅读
  5. do{ __HAL_RCC_GPIOH_CLK_ENABLE(); }while(0);

    2023-12-19 12:32:04       37 阅读
  6. 数据结构-顺序表的大小

    2023-12-19 12:32:04       63 阅读
  7. jquery、vue、uni-app、小程序的页面传参方式

    2023-12-19 12:32:04       50 阅读
  8. ubuntu18.04 升级到ubuntu22.04版本

    2023-12-19 12:32:04       84 阅读
  9. 如何在断线后不重连加入音视频房间

    2023-12-19 12:32:04       70 阅读
  10. 四舍五入专题

    2023-12-19 12:32:04       50 阅读