【发布时间】:2015-04-12 16:14:48
【问题描述】:
这篇文章的上下文涉及到 ASP.NET Web API 2.2 + OWIN 该环境是具有 OWIN 服务器和 Web Api 的单个应用程序。
背景:
在 Startup 类中,必须指定 OAuthBearerServerOptions,它提供给 OAuthBearerAuthenticationProvider。这些选项是在 OWIN 服务器启动期间创建的。在OAuthBearerServerOptions 上,我必须指定AccessTokenExpireTimeSpan,以便确保令牌到期。
问题
我必须能够根据每个身份验证请求动态指定到期时间跨度。我不确定这是否可以做到并且想知道:
- 可以吗?
- 如果是;我可以在什么时候执行此查找和到期分配?
启动配置内容:
var config = new HttpConfiguration();
WebApiConfig.Register(config);
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
var OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/OAuth"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(**THIS NEEDS TO BE DYNAMIC**)),
Provider = new AuthorizationServerProvider()
};
//STOP!!!!!!!!
//DO NOT CHANGE THE ORDER OF THE BELOW app.Use statements!!!!!
//Token Generation
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); //this MUST come before oauth registration
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
Provider = new BearerProvider()
});
app.UseAutofacMiddleware(container); //this MUST come before UseAutofacWebApi
app.UseAutofacWebApi(config);//this MUST come before app.UseWebApi
app.UseWebApi(config);
我开始弄乱 BearerProvider 类(请参阅上面的 app.UseOAuthBearerAuthentication 了解我在哪里使用此类),特别是 ValidateIdentity 方法,但不确定这是否是身份验证工作流中设置此值的正确点.这似乎是合适的,但我寻求验证我的立场。
public class BearerProvider : OAuthBearerAuthenticationProvider
{
public override async Task RequestToken(OAuthRequestTokenContext context)
{
await base.RequestToken(context);
//No token? attempt to retrieve from query string
if (String.IsNullOrEmpty(context.Token))
{
context.Token = context.Request.Query.Get("access_token");
}
}
public override Task ValidateIdentity(OAuthValidateIdentityContext context)
{
//context.Ticket.Properties.ExpiresUtc= //SOME DB CALL TO FIND OUT EXPIRE VALUE..IS THIS PROPER?
return base.ValidateIdentity(context);
}
}
提前致谢!
【问题讨论】:
标签: asp.net-web-api oauth token owin