在我的应用程序中,我使用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?
我感谢任何帮助:)
您的SimpleAuthorizationServerProvider需要实现。使用身份验证在VS2015中生成WebAPI项目可以为您提供something like this。
Kelvin Amorim撰写的文章Applying Cookie-Stored Sessions With ASP.NET and OpenID Connect 1.0对于理解支持多个身份验证中间件的多个因素非常有用。
一些要点是:
AuthenticationType
(它只是一个字符串键,有一些默认值可供选择)RouteAreaAttribute
)来控制哪些cookie用于哪些请求