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

asp.net core mvc change default Identity area routes

asp.net 核心 2.2 身份是通过

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

登录设置为“/Identity/Account/Login”:

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

现在如何更改通过 Startup.cs 添加的身份区域的路由,例如:

https://www.example.com/admin instead of having 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 时,您没有任何要修改的特定页面或控制器。

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

  1. 鼠标右键点击项目
  2. 添加 --> 新脚手架项目(如果禁用,停止 debugging/running)
  3. 在左侧select标识并点击添加
  4. Select 您的布局页面 (~/Views/Shared/_Layoutcshtml) 以获得正确的布局
  5. select 您要覆盖的页面,例如Account\Register
  6. Select 您的数据上下文 class(或通过单击 [+] 创建一个新的)
  7. 单击添加

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

展开Register.cshtml并打开文件Register.cshtml.cs。 ('codebehind')

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

public void OnGet(string returnUrl = null)
{
  ReturnUrl = returnUrl;
}

与:

public IActionResult OnGet(string returnUrl = null) => RedirectToPage("/Account/Login"); // disable registrations

附加信息:

万一 [MSFT] 正在阅读这篇文章:如果您可以通过 Startup.cs 禁用注册或更改重定向路由而无需脚手架(这将允许您更新软件包而无需 UI 您曾经在版本 x.

搭建过的脚手架