猿问

ASP.NET Core Identity 不会重定向到正确的登录页面

以这种方式配置它不起作用。


    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)

我希望两者都有相同的行为。显然不是。错误或我没有得到如何配置它?:-)


有什么想法吗。


慕勒3428872
浏览 106回答 1
1回答

守着一只汪

在发布我的问题时,我也配置/添加了身份框架。因此,可能是多种因素共同作用导致它无法正常工作。工作解决方案:配置:var authenticationBuilder = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)&nbsp; &nbsp; .AddCookie(options =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; options.LoginPath = $"/logon";&nbsp; &nbsp; &nbsp; &nbsp; options.LogoutPath = $"/logoff";&nbsp; &nbsp; &nbsp; &nbsp; options.AccessDeniedPath = $"/accessdenied";&nbsp; &nbsp; });ConfigureSocialLogins(authenticationBuilder);实际登录(即写入 cookie 是通过完成的)private async Task SignInUser(AppUser appUser)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var claims = new List<Claim>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Claim(ClaimTypes.NameIdentifier, appUser.Email),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Claim(ClaimTypes.Name, appUser.Displayname ?? appUser.Email),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Claim(ClaimTypes.Email, appUser.Email),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties());&nbsp; &nbsp; &nbsp; &nbsp; }记下 CookieAuthenticationDefaults.AuthenticationScheme 的所有用法。
随时随地看视频慕课网APP
我要回答