微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为asp net core 5的两个面板授权cookies

如何解决为asp net core 5的两个面板授权cookies

用户登录未授权。我不知道登录代码可能是哪里出错了。通常在正确输入时它可以在没有路径的情况下工作,但是当我将路径分开时是不允许的。我希望以自己的方式创建管理员(“AdminAuth”)和用户(“UserAuth”)面板 cookie。感谢朋友的帮助。

启动

            services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        }).AddCookie("UserAuth",options =>
        {
            options.Cookie.Name = "UserAuthentication";
            options.Cookie.Path = "/";
            options.LoginPath = "/Home/Login";
            options.logoutPath = "/Home/logout";
            options.ExpireTimeSpan = TimeSpan.FromDays(30);
        }).AddCookie("AdminAuth",options =>
        {
            options.Cookie.Name = "AdminAuthentication";
            options.Cookie.Path = "/Manager";
            options.LoginPath = "/Manager/Login";
            options.logoutPath = "/Manager/logout";
            options.ExpireTimeSpan = TimeSpan.FromDays(7);
        });

登录

                        if (ModelState.IsValid)
                    {
                        var claims = new List<Claim>()
                        {
                            new Claim(ClaimTypes.NameIdentifier,employers.Id.ToString()),new Claim(ClaimTypes.Name,employers.Username.ToString()),new Claim(ClaimTypes.Email,loginEmployersviewmodel.EmailOrPhone)
                        };
                        var CookieAuth = CookieAuthenticationDefaults.AuthenticationScheme;
                        var identity = new ClaimsIdentity(claims,CookieAuth);
                        var principal = new ClaimsPrincipal(identity);

                        var properties = new AuthenticationProperties()
                        {
                            IsPersistent = loginEmployersviewmodel.RememberMe
                        };
                        await HttpContext.SignInAsync("AdminAuth",principal,properties);
                        await HttpContext.AuthenticateAsync("AdminAuth");

            return RedirectToAction("Dashboard","Manager");

控制器

    [Authorize(AuthenticationSchemes = "AdminAuth")]
    public IActionResult Index()
    {
        return View();
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。