为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 (将#修改为@)