简体   繁体   English

dotnet核心授权始终重定向到身份默认登录页面

[英]dotnet core authorize always redirect to identity default login page

I create custom login page in pages/login.cshtml , and also i set [authorize] config in startup.cs to redirect to /login , but it keep redirect to Identity/Account/Login . 我在pages/login.cshtml创建自定义登录页面,并在startup.cs中将[authorize]配置设置为重定向到/login ,但它仍然重定向到Identity/Account/Login

this is my startup.cs 这是我的startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity<IdentityUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.Configure<IdentityOptions>(options =>
            {
                // Password settings.
                options.Password.RequireDigit = true;
                options.Password.RequireLowercase = true;
                options.Password.RequireNonAlphanumeric = true;
                options.Password.RequireUppercase = true;
                options.Password.RequiredLength = 6;
                options.Password.RequiredUniqueChars = 1;

                // Lockout settings.
                options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.Lockout.AllowedForNewUsers = true;

                // User settings.
                options.User.AllowedUserNameCharacters =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
                options.User.RequireUniqueEmail = false;
            });



            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

                options.LoginPath = "/Login";
                options.AccessDeniedPath = "/AccessDenied";
                options.LogoutPath = "/Logout";
                options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                options.SlidingExpiration = true;
            });

            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddRazorPagesOptions(options =>
                {
                    options.Conventions.AddPageRoute("/Dashboards/Dashboard1", "");
                    options.Conventions.AllowAnonymousToPage("/Login");
                });


        }

I already specify loginpath = "/login" , but it always back to identity default page, why and how to solve it? 我已经指定了loginpath = "/login" ,但是它总是返回到身份默认页面,为什么以及如何解决呢?

For this issue, try services.PostConfigure<CookieAuthenticationOptions> like 对于此问题,请尝试使用services.PostConfigure<CookieAuthenticationOptions>类的方法

services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AddPageRoute("/Dashboards/Dashboard1", "");
        options.Conventions.AllowAnonymousToPage("/Login");
    });
services.PostConfigure<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme,
    opt => {
        //configure your other properties
        opt.LoginPath = "/Login";
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net core 5、使用Identity,如何更改[Authorize]属性的默认重定向? - Asp.net core 5, using Identity, how do I change the default redirect of the [Authorize] Attribute? Aspnet Core 身份管理总是让我返回登录页面 - Aspnet Core Identity management always returning me to the login page ASP .NET 核心身份服务器默认登录页面为微软 - ASP .NET Core identity server default login page as Microsoft 如果用户在 url 中输入 login.aspx,如何重定向到 dotnet core 中的登录页面 - how to redirect to login page in dotnet core, if the user enters login.aspx in the url Angular 8 和 Microsoft 登录身份用户 (dotnet Core 3.0 3.1) - Angular 8 and Microsoft Login Identity User (dotnet Core 3.0 3.1) ASP.NET Core 2.0 Identity,两个控制器分别重定向到不同的登录页面 - ASP.NET Core 2.0 Identity, 2 controllers redirect each to different login page .NET Core 身份登录页面处理程序 OnGetAync() - .NET Core Identity Login Page handler OnGetAync() 如果我使用授权,则进入登录循环不会重定向到另一个页面 - if I use Authorize then it goes to login loop does not redirect to another page configuration.Save()始终在登录页面上重定向 - configuration.Save() always redirect on login page 使用 Google 和 ASP.NET 核心身份的无限登录重定向循环 - Infinite login redirect loop with Google and ASP.NET Core Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM