asp.net core 2.2 Identity 是通过添加的
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddDefaultUI();
登录设置为“/Identity/Account/Login”:
options.LoginPath = "/Identity/Account/Login";
现在如何更改通过 Startup.cs 添加的标识区域的路由,例如,我可以这样做:
https://www.example.com/admin而不是 https://www.example.com/identity/account/login
或
https://www.example.com/register以便它转到 HomeController->RegisterAction。这将允许我捕获 HomeController->RegisterAction 并将其重定向到 HomeController->IndexAction,以便禁用注册。
最佳答案
如果有人搜索 asp.net core mvc 2.2 的答案:
当您调用 DefaultUI 时,您没有任何特定的页面或 Controller 需要修改。
您必须脚手架您要修改的页面。
- 鼠标右键点击项目
- 添加 --> 新脚手架项(如果禁用,停止调试/运行)
- 在左侧选择身份并点击添加
- 选择您的布局页面 (~/Views/Shared/_Layoutcshtml) 以获得正确的布局
- 选择您要覆盖的页面,例如账号\注册
- 选择您的数据上下文类(或点击 [+] 创建一个新类)
- 点击添加
现在您将在/Areas/Identity/Pages/Account/Register.cshtml 中看到
展开 Register.cshtml 并打开文件 Register.cshtml.cs。 ('代码隐藏')
现在如果你想禁用注册,你可以替换
public void OnGet(string returnUrl = null)
{
ReturnUrl = returnUrl;
}
与:
public IActionResult OnGet(string returnUrl = null) => RedirectToPage("/Account/Login"); // disable registrations
附加信息:
- https://github.com/aspnet/Identity/issues/1824
- https://github.com/aspnet/Docs/issues/10226
- https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio
如果任何 [MSFT] 正在阅读此内容:如果您可以通过 Startup.cs 禁用注册或更改重定向路由而无需脚手架(这将允许您更新包而无需拥有您曾经在版本 x 搭建的 UI。
关于c# - asp.net core mvc 更改默认标识区域路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54194930/