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