asp.net核心身份不能重定向到正确的登录页面 [英] asp.net core identity does not redirect to correct logon pages

查看:117
本文介绍了asp.net核心身份不能重定向到正确的登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以这种方式配置后无法正常工作.

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

以这种方式进行配置:

    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 ? :-)

任何想法.

推荐答案

在发布我的问题时,我也配置/添加了身份框架.因此,可能是由于多种因素共同作用,它可能无法正常工作.

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.

可行的解决方案:

配置:

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

实际登录(即通过以下方式编写Cookie)

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

记下CookieAuthenticationDefaults.AuthenticationScheme的所有用法.

Take note of all the usages of CookieAuthenticationDefaults.AuthenticationScheme.

这篇关于asp.net核心身份不能重定向到正确的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆