asp.net核心mvc更改默认标识区域路由



asp.net核心2.2身份通过添加

services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddDefaultUI();  

登录设置为"/Identity/Account/login":

options.LoginPath = "/Identity/Account/Login";

现在,我如何更改通过Startup.cs添加的Identity区域中的路线,以便我可以执行例如:

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核心mvc 2.2:的答案

当您调用DefaultUI时,您没有任何特定的页面或控制器可以修改。

您必须脚手架要修改的页面。

  1. 用鼠标右键单击项目
  2. 添加-->新脚手架项(如果禁用,则停止调试/运行)
  3. 在左侧选择"标识",然后单击"添加">
  4. 选择布局页面(~/Views/Shared/_Layoutshtml)以获得正确的布局
  5. 选择要覆盖的页面,例如"帐户\注册">
  6. 选择您的数据上下文类(或单击[+]创建一个新类)
  7. 单击"添加">

现在您将在/Areas/Identity/Pages/Account/Register.cshtml中看到

展开Register.cshtml并打开文件Register.cshhtml.cs.('debehind')

现在,如果你想禁用注册,你可以替换

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

如果任何[MST]正在阅读这篇文章:如果您可以通过Startup.cs禁用注册或更改重定向路线,而不必脚手架(这将允许您更新包,而无需在x版本上搭建UI。

最新更新