zoukankan      html  css  js  c++  java
  • authorize(权限验证)

    Startup 中 ConfigureServices 插入

    services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = "MyCookieAuthenticationScheme";
                    options.DefaultChallengeScheme = "MyCookieAuthenticationScheme";
                    options.DefaultSignInScheme = "MyCookieAuthenticationScheme";
    
                })
                .AddCookie("MyCookieAuthenticationScheme", options =>
                {
                    options.AccessDeniedPath ="/Home";
                    options.LoginPath = "/Home";
                });
    

    Configure 中增加

    app.UseAuthentication();

    控制器中使用

    public JsonResult LoginCheck(string username, string password)
            {
                var user = _***.***(username, password); //检测用户是否正确
                
                if (user.code == 0)
                {
                    var claims = new List<Claim>()
                    {
                        new Claim(ClaimTypes.Sid,Convert.ToString(user.data.id)),
                        new Claim(ClaimTypes.Name,user.data.username),                    
                        //new Claim(ClaimTypes.Role,user.data.usergroup)
                    };
    
                    string groupstr = user.data.usergroup; //通过后台调用权限属性
                    string[] GroupSplit = groupstr.Split(',');
    
                    if (GroupSplit != null)
                    {
                        for (int i = 0; i < GroupSplit.Length; i++)
                        {
                            claims.Add(new Claim(ClaimTypes.Role, GroupSplit[i]));
                        }
                    }
                    var identity = new ClaimsIdentity(claims, "Login");
                    var userPrincipal = new ClaimsPrincipal(identity);
                    HttpContext.SignInAsync("MyCookieAuthenticationScheme", userPrincipal, new AuthenticationProperties
                    {
                        ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
                        IsPersistent = false,
                        AllowRefresh = false
                    });
    
                }
                return Json(new { code = user.code, msg = user.result, data = user.data });
            }

    获取结果

    var userId = User.FindFirst(ClaimTypes.Sid).Value;
                var userName = User.Identity.Name;
                var rolelist = User.FindAll(ClaimTypes.Role);
                HttpContext.Response.WriteAsync($"测试结果  {userId}---{userName}--{rolelist}");

    退出登录

    public async Task<IActionResult> Logout()
            {
                await HttpContext.SignOutAsync("MyCookieAuthenticationScheme");
                return RedirectToAction("Index", "Home");
    
            }
    

      

  • 相关阅读:
    github fork项目后,代码更新
    UIScrollView的用法,属性
    调整屏幕亮度,调整字体大小
    iOS UIFont 字体名字大全
    ios 6以后,UILabel全属性
    oc中的各种遍历(迭代)方法
    判断app是否是第一次启动
    ios 显示代码块(show the code snippet library)
    ios 添加动画的方法
    添加app第一次启动页面
  • 原文地址:https://www.cnblogs.com/almmm/p/11533154.html
Copyright © 2011-2022 走看看