我可以在Owin中使用两种身份验证方法吗?

问题描述 投票:2回答:2

在我的应用程序中,我使用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
2个回答
1
投票

您的SimpleAuthorizationServerProvider需要实现。使用身份验证在VS2015中生成WebAPI项目可以为您提供something like this


0
投票

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

一些要点是:

  • 每个身份验证选项中间件应该使用不同的AuthenticationType(它只是一个字符串键,有一些默认值可供选择)
  • 您可以设置cookie路径并使用相应的MVC区域(请参阅RouteAreaAttribute)来控制哪些cookie用于哪些请求
© www.soinside.com 2019 - 2024. All rights reserved.