• asp.net core 基于角色的认证登陆


    一、登陆页面的Controller

    [Authorize(Roles = "Admin,SuperAdmin")]
    public class ManageController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
    
            [AllowAnonymous]
            public IActionResult Login(string returnUrl = null)
            {
                _logger.LogInformation("进入登录页面");
                TempData["returnUrl"] = returnUrl;
                ViewBag.Msg = " ";
                return View();
            }
    
    
            [AllowAnonymous]
            [HttpPost]
            public async Task<IActionResult> LoginCheck(string name, string password, string returnUrl)
            {
                string loginName = Filter.FilterHTML(name);
                var account = await _context.Account.FirstOrDefaultAsync(g => g.LoginName.Equals(loginName));
                if (account == null || (!account.Password.Equals(password)))
                {
                    ViewBag.Msg = "账号或密码有误,请重新输入";
                    return View("Index");
                }
                else
                {
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Sid, account.Id.ToString()));
                    identity.AddClaim(new Claim(ClaimTypes.Name, account.Name));
                    identity.AddClaim(new Claim(ClaimTypes.Role, account.Role));
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties
                    {
                        IsPersistent = true,
                        ExpiresUtc = DateTime.Now.AddDays(1)
                    });
    
    
    
                    if (returnUrl == null)
                    {
                        returnUrl = TempData["returnUrl"]?.ToString();
                    }
                    if (returnUrl != null)
                    {
                        return LocalRedirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction(nameof(HomeController.Index), "Manage");
                    }
                }
            }
    
    
            [HttpGet]
            public async Task<IActionResult> Logout()
            {
                await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                return RedirectToAction("login");
            }
    
            [AllowAnonymous]
            public IActionResult Denied()
            {
                return View();
            }
        }
    

      二、配置Startup.cs的ConfigureServices方法,增加如下代码

                //配置使用Authorize登陆认证
                services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                      .AddCookie(options =>
                      {
                          options.LoginPath = new PathString("/manage/login");
                          options.AccessDeniedPath = new PathString("/manage/denied");
                      }); 
    

      三、配置Startup.cs的Configure方法,增加如下代码

    app.UseAuthentication();//配置使用Authorize登陆认证
    

      

  • 相关阅读:
    使用runOnUiThread更新UI
    Leetcode Symmetric Tree
    EBS 开发中如何动态启用和禁止请求(Current Request)的参数
    c 陷阱与缺陷(一)
    钟浩荣战胜病魔,不负众望重踏传智播客!
    【原创】分布式之elk日志架构的演进
    【强烈谴责】博客园园友随意抄袭他人文章并作为自己原创的行为
    【原创】研发应该懂的binlog知识(下)
    【原创】研发应该懂的binlog知识(上)
    【原创】一个线程oom,进程里其他线程还能运行吗?
  • 原文地址:https://www.cnblogs.com/fireicesion/p/10706326.html
Copyright © 2020-2023  润新知