asp.net - ASP.NET Core 2.1 标识:如何删除默认 UI 剃须刀页面?
问题描述
扩展这个问题的答案: Change routing in ASP.NET Core Identity UI?
当想要自定义 URL 时,Javier 建议使用以下选项之一:
- 使用默认 UI 的脚手架元素并自己进行所有必要的自定义。
- 使用将旧路由指向新路由的重定向规则。
- 根本不要使用默认 UI。
从一个新的 ASP.NET Core 2.1 MVC 项目中,设置了身份验证:个人用户帐户,您如何不使用默认 UI?它似乎默认与 Identity Core 一起安装。
项目创建后,去掉 Default UI razor pages 的方法是什么,仍然使用 Identity Core?
我可以删除该/Identity/
区域,然后创建自己的区域AccountController
吗?
解决方案
使用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 剃须刀页面,并改用 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
您自己的类。
2) 删除 ASP.NET Core 2.1 项目默认提供的 Identity 区域文件夹。
3) 创建一个新的单独的 ASP.NET Core 2.0 项目(不是“2.1”),Individual User Account
在项目创建窗口中选择身份验证。
4) 将 2.0 项目中的AccountController
and 和ManageController
对应的ViewModels
and复制Views
到您的 ASP.NET Core 2.1 项目中。
执行上述操作,到目前为止我还没有遇到任何问题。
推荐阅读
- javascript - Node.js 405(方法不允许)和列不能为空 MySQL
- c++ - 无法解析对重载函数的 c++ 引用;
- java - 没有实现的接口可以在spring boot的服务类中自动装配吗
- rust - 演员&Vec
作为 &str - r - 模拟一个简单的线性模型
- python-3.x - Python3 Virtual env 无法将文件保存在与 virtualenv 不同的位置
- android - 错误:找不到:vectordrawable-animated-1.1.0
- elasticsearch - 如何使用 ILM 翻转来管理弹性映射更改?
- linux - Apache2 无法启动 Putty AWS Windows
- python - ModuleNotFoundError 即使模块出现在 pip3 列表中