扩展这个问题的答案:
Change routing in ASP.NET Core Identity UI?
Javier recommends one of the following options when wanting to customise the URLs:
- Use the scaffolding element of the Default UI and make all necessary customisations yourself.
- Use a redirection rule that points the old routes to the new routes.
- Don't use the Default UI at all.
在新的 ASP.NET Core 2.1 MVC 项目中,设置了身份验证:个人用户帐户,如何不使用默认 UI?它似乎默认与 Identity Core 一起安装。
项目创建完成后,有什么方法可以去掉默认UI Razor 页面,仍然使用Identity Core?
我可以删除
/Identity/
吗?区域,并创建我自己的 AccountController
反而?
最佳答案
使用 the article linked by Panagiotis Kanavos ,我能够找到解决方案。
从 ASP.NET Core 2.1.0-preview1 开始,有一行 .AddDefaultUI()
,您不必将其包含在 Startup.cs
中.
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
然而,在 Core 2.1 的最终发布版本中,同一部分被简化为:
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
解决方案 ,如果你换了
AddDefaultIdentity
返回 AddIdentity
,您可以覆盖默认值。 IE。不包括 .AddDefaultUI()
(也不要搭建 UI 的脚手架),您可以自己编写。services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
// .AddDefaultUI()
.AddDefaultTokenProviders();
然后,我认为删除
/Areas/Identity/
是安全的文件夹,但我不是 100%更新:
我清理了我的答案以详细说明我最终采用的最终解决方案,删除 ASP.NET Core 2.1 附带的默认标识 UI Razor 页面,并改用 MVC。
1) 在
Startup.cs
, public void ConfigureServices(IServiceCollection services)
{
// Unrelated stuff commented out...
// BEGIN: Identity Setup (Overrides default identity)
services.AddIdentity<ApplicationUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// END: Identity Setup
services.Configure<IdentityOptions>(options =>
{
// Set your identity Settings here (password length, etc.)
});
// More unrelated stuff commented out...
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Added after AddMvc()
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/account/login";
options.LogoutPath = $"/account/logout";
options.AccessDeniedPath = $"/account/access-denied";
});
// More unrelated stuff commented out...
}
显然,同时替换
ApplicationUser
, 和 IdentityRole
如果需要,可以使用您自己的类(class)。2) 删除 ASP.NET Core 2.1 项目默认的 Identity 文件夹。
3) 创建一个新的单独的 ASP.NET Core 2.0 项目(不是“2.1”),使用
Individual User Account
在项目创建窗口中选择的身份验证。4) 复制
AccountController
和 ManageController
, 对应 ViewModels
和 Views
,从 2.0 项目到您的 ASP.NET Core 2.1 项目。执行上述操作,到目前为止我还没有遇到任何问题。
关于ASP.NET Core 2.1 标识 : How to remove the Default UI razor pages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288801/