简体   繁体   English

Aspnet Core 身份管理总是让我返回登录页面

[英]Aspnet Core Identity management always returning me to the login page

In My application my account manage page is not working I am trying to go to the following.在我的应用程序中,我的帐户管理页面不起作用我正在尝试将 go 更改为以下内容。

https://localhost:5001/Identity/Account/Manage https://localhost:5001/Identity/Account/Manage

Which is where the pages for identy manage should be according to my scaffolding, i have changed the code to ensure it points to my ApplicaitonUser Class but for some reason when i access that page its throwing me back to the login even though I have logged in with the login page.根据我的脚手架,身份管理页面应该在哪里,我已经更改了代码以确保它指向我的 ApplicaitonUser Class 但是由于某种原因,当我访问该页面时,即使我已经登录,它也会让我回到登录状态与登录页面。

在此处输入图像描述

I have configured my start-up class as such我已经这样配置了我的启动 class

services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
    config.SignIn.RequireConfirmedEmail = true;
    config.Tokens.AuthenticatorTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
    config.User.RequireUniqueEmail = true;

})
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<MSFSAddonDBContext>()
    .AddDefaultTokenProviders()
    .AddDefaultUI()
    .AddRoles<IdentityRole>();

services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>,
AdditionalUserClaimsPrincipalFactory>();
services.AddSession(opts =>
{
    opts.Cookie.IsEssential = false; // make the session cookie Essential
});

Ensuring that the cookie warning is disable as I Believe it can cause issues if not setup yet.确保禁用 cookie 警告,因为我相信如果尚未设置它可能会导致问题。 I have also enabled MFA using the following code.我还使用以下代码启用了 MFA。

services.ConfigureApplicationCookie(config =>
{
    config.Cookie.Name = "Identity.Cookie";
    config.LoginPath = "/Identity/Account/Login";
});

services.AddAuthorization(options =>
options.AddPolicy("TwoFactorEnabled",
 x => x.RequireClaim("amr", "mfa")));
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");

The cookie is being created in edge in the debug tabs so I no its being created any idea why my code is refusing to go to the management page I am trying to follow the guide here. cookie 是在调试选项卡的边缘创建的,所以我不知道为什么我的代码拒绝 go 到我正在尝试遵循此处指南的管理页面。

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/mfa?view=aspnetcore-5.0 https://docs.microsoft.com/en-us/aspnet/core/security/authentication/mfa?view=aspnetcore-5.0

For complete here is my configure settings完整这里是我的配置设置

app.UseHttpsRedirection();
app.UseStaticFiles();
        
app.UseRouting();

app.UseAuthorization();
app.UseAuthentication();


app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
    name: "areas",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");


    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

My issue was I had AddDefaultUI which over ride the path so the page was never being found.我的问题是我的 AddDefaultUI 覆盖了路径,因此从未找到该页面。 Leaving this herre for anyone else this is asp.net core 5.把这个留给其他人,这是 asp.net 核心 5。

The template-generated app doesn't use authorization.模板生成的应用程序不使用授权。 app.UseAuthorization is included to ensure it's added in the correct order should the app add authorization.包含 app.UseAuthorization 以确保在应用添加授权时以正确的顺序添加它。 UseRouting, UseAuthentication, UseAuthorization, and UseEndpoints must be called in the order shown in the preceding code. UseRouting、UseAuthentication、UseAuthorization 和 UseEndpoints 必须按照上述代码中显示的顺序调用。

reference: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?source=recommendations&view=aspnetcore-3.1&tabs=netcore-cli参考: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?source=recommendations&view=aspnetcore-3.1&tabs=netcore-cli

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM