我,正在使用 .net 6 進行應用程式開發。我正在使用來自 .net core 的 Identity Manager。我制作了一個自定義引導登錄頁面。同樣根據我的應用程式結構,我不需要 _loginpartial.cshtml。
我已將 [Authorize] 屬性賦予 MVC .net 核心專案中 HomeController 中的默認隱私頁面操作。還在 Home>Index.cshtml 中為隱私添加了錨標記。
當我點擊鏈接時,應用程式吐出 404 頁面未找到錯誤。它正在重定向到以下鏈接: https://localhost:7188/Identity/Account/Login?ReturnUrl=/Home/Privacy 。但是頁面沒有加載。
但是,如果我從上面的鏈接中洗掉 /Identity/ 則頁面會加載。即 https://localhost:7188/Account/Login?ReturnUrl=/Home/Privacy。
由于我使用的是 .net 6,因此我將 Startup.cs 中的所有代碼都用于 Program.cs 并相應地遷移了代碼。
當我添加 app.MapRazorPages(); 在端點部分之后,鏈接有效!但它提供了一個與我創建的完全不同的登錄頁面。它似乎正在加載一個預構建的身份頁面。如何使用我的登錄頁面?當我使用 [Authorize] 屬性來保護我的控制器方法時?
程式.cs
using MyApplication.Data;
using MyApplication.Service;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders().AddDefaultUI();
builder.Services.AddControllersWithViews(options => { options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); });
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
如果我需要提供更多細節來解決問題,請告訴我?
uj5u.com熱心網友回復:
您需要 [腳手架登錄頁面](請參閱檔案 - https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-6.0&tabs=visual-studio #scaffold-identity-into-an-mvc-project-with-authorization ) 標識頁面使用,然后在該剃刀頁面上實作您的自定義登錄頁面。
uj5u.com熱心網友回復:
我在這里找到了以上 Microsoft 知識庫的解決方案:https : //docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-6.0
解決方案是將我的自定義登錄頁面映射到 Identity。這是通過在 .net 6 的 program.cs 中添加以下代碼行來實作的
services.ConfigureApplicationCookie(options =>
{
//Location for your Custom Access Denied Page
options.AccessDeniedPath = "Account/AccessDenied";
//Location for your Custom Login Page
options.LoginPath = "Account/Login";
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364424.html
