asp.net core identity does not redirect to correct logon pages

会有一股神秘感。 提交于 2019-12-11 10:59:22

问题


Configured this way it is not working.

    services
        .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

            options.LoginPath = $"/logon";
            options.LogoutPath = $"/logoff";
            options.AccessDeniedPath = $"/accessdenied";
            options.SlidingExpiration = true;
        })

configured this way it is working :

    services.ConfigureApplicationCookie(options =>
    {
        options.Cookie.Name = "Caldr.Auth";
        options.LoginPath = $"/logon";
        options.LogoutPath = $"/logoff";
        options.AccessDeniedPath = $"/accessdenied";
    });

    services
        .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)

I would expect both to have the same behavior. Apprantly not. Bug or I did not got how to configure it ? :-)

Any thoughts.


回答1:


At the time of posting my question I had identity framework configured/added too. So it might have been the mix of several factors that may it not work properly.

The working solution:

CONFIG:

var authenticationBuilder = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = $"/logon";
        options.LogoutPath = $"/logoff";
        options.AccessDeniedPath = $"/accessdenied";
    });
ConfigureSocialLogins(authenticationBuilder);

The actual logon (i.e. writing the cookies is done via)

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());
        }

Take note of all the usages of CookieAuthenticationDefaults.AuthenticationScheme.



来源:https://stackoverflow.com/questions/55846770/asp-net-core-identity-does-not-redirect-to-correct-logon-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!