【问题标题】:Can I use two authentication methods in Owin?我可以在 Owin 中使用两种身份验证方法吗?
【发布时间】:2016-10-10 10:19:07
【问题描述】:

在我的应用程序中,我使用 MVC 和 Web.API。

MVC 部分处理管理前端、为cshtml 页面提供服务、与后端通信、使用 cookie 进行常规身份验证等:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

Web.API 处理 iOS 和 Android 应用程序发出的 REST 请求。对于那个我想使用基于令牌的身份验证:

var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
    AllowInsecureHttp = true, //todo-err: change in prod
    TokenEndpointPath = new PathString("/token"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
    Provider = new SimpleAuthorizationServerProvider()
};

// Token Generation
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

我的问题是,我需要什么才能完成这项工作?常规和令牌身份验证。我需要为 API 控制器创建自定义 AuthorizeAttribute 吗?

感谢您的帮助:)

【问题讨论】:

    标签: asp.net-mvc authentication asp.net-web-api oauth owin


    【解决方案1】:

    您的 SimpleAuthorizationServerProvider 需要实施。 在 VS2015 中生成带有身份验证的 WebAPI 项目会给你something like this

    【讨论】:

    • 您好,感谢您的回答。它被实施。我放在这里不是为了节省空间。我的问题是它是否会以这种方式与 both 身份验证方法(Regular 和 Web.API)一起使用。再次感谢。
    【解决方案2】:

    Kelvin Amorim 的文章 Applying Cookie-Stored Sessions With ASP.NET and OpenID Connect 1.0 对于理解支持多个身份验证中间件的多个因素非常有用。

    一些要点是:

    • 每个身份验证选项中间件应使用不同的AuthenticationType(它只是一个字符串键,有一些默认值可供选择)
    • 您可以设置cookie路径并使用对应的MVC区域(见RouteAreaAttribute)来控制哪些cookie用于哪些请求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-30
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      相关资源
      最近更新 更多