And*_* M. 5 asp.net-core-mvc asp.net-core
这是我的情况。
要在网站上执行某些操作,用户应该经过身份验证。不同的行动需要不同的主张。例如,要下订单,用户仅通过电话号码进行身份验证,要查看购买历史记录,用户应通过电话号码和密码进行身份验证,而要更改电话号码,用户应使用双因素身份验证进行身份验证。
我为每种身份验证方法创建一个登录页面,当用户通过身份验证时,我根据身份验证方法向她提供一组声明。我添加[Authorize(Policy="CanCreateOrder")]到CreateOrder操作方法中。该策略具有授权用户所需的声明逻辑。如果用户未获得授权,我想将用户重定向到适当的登录页面。
问题是我如何指定应将用户重定向以进行身份验证的网址?
看看CookieAuthenticationMiddleware我不知道如何根据所需的声明来指定登录页面。文档建议在配置时设置LoginPath属性,但在我的情况下,登录 URL 取决于我需要授权用户的声明。
您可以对每个不同的声明使用不同的身份验证方案:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "Phone",
LoginPath = "<phone - path>"
....
}
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "Password",
LoginPath = "<password - path>"
....
}
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "TwoFactor",
LoginPath = "<twofactor - path>",
....
}
Run Code Online (Sandbox Code Playgroud)
然后用法:
[Authorize(Policy="CanCreateOrder", ActiveAuthenticationSchemes = "Phone")]
Run Code Online (Sandbox Code Playgroud)
您也可以使用多种方案:
[Authorize(Policy="CanCreateOrder", ActiveAuthenticationSchemes = "Phone,TwoFactor")]
Run Code Online (Sandbox Code Playgroud)
请参阅https://docs.asp.net/en/latest/security/authorization/limitingidentitybyscheme.html
| 归档时间: |
|
| 查看次数: |
3168 次 |
| 最近记录: |