如何在ASP.NET中使用多个授权方案发布相应的承载和Cookie标识?

时间:2017-02-01 17:03:20

标签: asp.net asp.net-mvc asp.net-core katana asp.net-authentication

这部分documentation describes部分如何使用多种身份验证方案:

  

在某些情况下,例如单页应用程序,最终可能会有多种身份验证方法。例如,您的应用程序可能会使用基于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;
});

但是它仅描述了如何使用Bearer或Cookie auth。不清楚的是其他组合是有效的,或者如何正确地向客户发放承载或cookie。

如何实现?

1 个答案:

答案 0 :(得分:4)

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
});
  • 默认值为长期存在且用于非关键身份验证方案,例如在Facebook上,这可能是为了查看您的个人资料页面。
  • 更安全,更短暂的用于安全关键用户操作,例如更改密码或个人资料信息。

这为您提供了方便,无需一直使用长期存在的cookie登录,但只要您需要做一些有潜在危险的事情,您就可以切换到使用更短的生命值进行身份验证,因此需要更安全的cookie用户再次登录。

相关问题