c# - ASP.NET Core 2.0 身份,2 个 Controller 分别重定向到不同的登录页面

标签 c# asp.net-core asp.net-core-identity

我有两个 Controller

  • 测试 Controller
  • 演示 Controller

他们都使用[Authorize]属性和默认身份登录:邮箱+密码。

我的问题:如何将所有未经身份验证的请求重定向到 TestControoler 到 TestAccountController/Login 并将所有到 DemoController 的请求重定向到 DemoAccountController/Login ?

这样做的原因是我想有 2 个登录环境用于 2 个不同的目的。

最佳答案

ASP.net core 支持多种身份验证方案。

您可以通过将以下内容添加到您的 Startup.cs 来添加两个

services.AddAuthentication()
  .AddCookie("scheme1", options => { options.LoginPath = "/login1"; })
  .AddCookie("scheme2", options => { options.LoginPath = "/login2"; });

然后指定在 Controller 中使用的方案:

[Authorize(AuthenticationSchemes = "scheme1")]
public sealed class SampleController : Controller
{
  //...
}

参见 https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-2.1&tabs=aspnetcore2x更多详情

关于c# - ASP.NET Core 2.0 身份,2 个 Controller 分别重定向到不同的登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52505088/

相关文章:

c# - Ubuntu 上的 ASP.NET

c# - 我的查询有什么问题吗.Nest elastic C#

visual-studio-2013 - aspnet50目标框架是什么,我可以从VS2013引用它吗?

c# - 尝试激活 SecurityStampValidator 时无法解析 ISystemClock 的服务

c# - 身份不工作,无法访问身份数据

c# - 形状比应有的要大

c# - 将 lambda 表达式用于事件处理程序的最佳实践

c# - 在没有超时的情况下在 AspNetCore 应用程序上运行进程

c# - 带有 ASP.Net Core 的 Azure SignalR 无服务器

asp.net-core - 在 Google OAuth 流程中访问 EF DbContext