亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP.NET Core Identity 不會重定向到正確的登錄頁面

ASP.NET Core Identity 不會重定向到正確的登錄頁面

C#
慕勒3428872 2022-12-31 10:47:01
以這種方式配置它不起作用。    services        .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)        .AddCookie(options =>        {            options.ExpireTimeSpan = TimeSpan.FromMinutes(5);            options.LoginPath = $"/logon";            options.LogoutPath = $"/logoff";            options.AccessDeniedPath = $"/accessdenied";            options.SlidingExpiration = true;        })以這種方式配置它正在工作:    services.ConfigureApplicationCookie(options =>    {        options.Cookie.Name = "Caldr.Auth";        options.LoginPath = $"/logon";        options.LogoutPath = $"/logoff";        options.AccessDeniedPath = $"/accessdenied";    });    services        .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)我希望兩者都有相同的行為。顯然不是。錯誤或我沒有得到如何配置它?:-)有什么想法嗎。
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

在發布我的問題時,我也配置/添加了身份框架。因此,可能是多種因素共同作用導致它無法正常工作。


工作解決方案:


配置:


var authenticationBuilder = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)

    .AddCookie(options =>

    {

        options.LoginPath = $"/logon";

        options.LogoutPath = $"/logoff";

        options.AccessDeniedPath = $"/accessdenied";

    });

ConfigureSocialLogins(authenticationBuilder);

實際登錄(即寫入 cookie 是通過完成的)


private async Task SignInUser(AppUser appUser)

        {

            var claims = new List<Claim>

            {

                new Claim(ClaimTypes.NameIdentifier, appUser.Email),

                new Claim(ClaimTypes.Name, appUser.Displayname ?? appUser.Email),

                new Claim(ClaimTypes.Email, appUser.Email),

            };

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);


            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties());

        }

記下 CookieAuthenticationDefaults.AuthenticationScheme 的所有用法。


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號