簡體   English   中英

Aspnet Core 身份管理總是讓我返回登錄頁面

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

在我的應用程序中,我的帳戶管理頁面不起作用我正在嘗試將 go 更改為以下內容。

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

根據我的腳手架,身份管理頁面應該在哪里,我已經更改了代碼以確保它指向我的 ApplicaitonUser Class 但是由於某種原因,當我訪問該頁面時,即使我已經登錄,它也會讓我回到登錄狀態與登錄頁面。

在此處輸入圖像描述

我已經這樣配置了我的啟動 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
});

確保禁用 cookie 警告,因為我相信如果尚未設置它可能會導致問題。 我還使用以下代碼啟用了 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");

cookie 是在調試選項卡的邊緣創建的,所以我不知道為什么我的代碼拒絕 go 到我正在嘗試遵循此處指南的管理頁面。

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

完整這里是我的配置設置

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

我的問題是我的 AddDefaultUI 覆蓋了路徑,因此從未找到該頁面。 把這個留給其他人,這是 asp.net 核心 5。

模板生成的應用程序不使用授權。 包含 app.UseAuthorization 以確保在應用添加授權時以正確的順序添加它。 UseRouting、UseAuthentication、UseAuthorization 和 UseEndpoints 必須按照上述代碼中顯示的順序調用。

參考: 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