goo*_*ate 5 asp.net asp.net-mvc asp.net-authentication katana asp.net-core
在某些情况下,例如单页应用程序,最终可能会有多种身份验证方法.例如,您的应用程序可能会使用基于cookie的身份验证来登录JavaScript请求的承载身份验证.在某些情况下,您可能有多个身份验证中间件实例.例如,两个cookie中间件,其中一个包含基本身份,另一个是在多因素身份验证触发时创建的,因为用户请求了需要额外安全性的操作.
例:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "Cookie",
LoginPath = new PathString("/Account/Unauthorized/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = false
});
app.UseBearerAuthentication(options =>
{
options.AuthenticationScheme = "Bearer";
options.AutomaticAuthenticate = false;
});
Run Code Online (Sandbox Code Playgroud)
但是它仅描述了如何使用Bearer或Cookie auth.不清楚的是其他组合是有效的,或者如何正确地向客户发放承载或cookie.
怎么能实现呢?
Facebook、Google 等大型网站使用的一个常见用例是使用多个 cookie 身份验证中间件,并将其中一个设置为默认使用 AutomaticAuthenticate
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "InsecureLongLived",
LoginPath = new PathString("/Account/Unauthorized/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true
});
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "SecureAndShortLived",
LoginPath = new PathString("/Account/Unauthorized/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = false
});
Run Code Online (Sandbox Code Playgroud)
这为您提供了便利,您不必一直使用长期存在的 cookie 登录,但是一旦您需要做一些有潜在危险的事情,您就切换到使用寿命短得多、因此更安全的 cookie 进行身份验证,这需要用户再次登录。
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |