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");
    
            }
    

      

  • 相关阅读:
    6、深入理解计算机系统笔记:存储器层次结构,存储技术(1)
    流程图【占无内容】
    程序的三种基本控制架构【只有提纲】
    Console算法[for]穷举法:百钱买百鸡
    Logic算法(狼羊白菜)
    Console算法continue与break的区别?
    Console算法[for]简单画图
    Console算法[for]输出等腰三角形
    Console算法[for]国王与老人的六十四格
    Console算法[for]素数
  • 原文地址:https://www.cnblogs.com/almmm/p/11533154.html
Copyright © 2011-2022 走看看