use*_*393 14 asp.net oauth oauth-2.0 asp.net-web-api owin
我试图动态设置令牌到期时间,但它似乎只是默认为20分钟.
这是我的ConfigureAuth:
public void ConfigureAuth(IAppBuilder app)
{
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(""),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
}
Run Code Online (Sandbox Code Playgroud)
这是我的GrantResourceOwnerCredentials方法:
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var hasValidLogin = (new login().authenticate(context.UserName, context.Password, "") == "valid");
if (hasValidLogin == false)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return Task.FromResult<object>(null);
}
var oAuthIdentity = CreateIdentity(context);
var oAuthProperties = CreateProperties(context);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, oAuthProperties);
context.Validated(ticket);
return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)
这是我的SetProperties方法,我可以设置过期:
public static AuthenticationProperties CreateProperties(OAuthGrantResourceOwnerCredentialsContext context)
{
IDictionary<string, string> data = new Dictionary<string, string>
{
{ "client_id", context.ClientId }
};
var response = new AuthenticationProperties(data);
response.ExpiresUtc = DateTime.Now.AddMonths(1);
return response;
}
Run Code Online (Sandbox Code Playgroud)
即使在那之后,令牌也会返回:
{
"access_token": ".....",
"token_type": "bearer",
"expires_in": 1199,
"client_id": ".....",
".expires": "Fri, 13 Nov 2015 20:24:06 GMT",
".issued": "Fri, 13 Nov 2015 20:04:06 GMT"
}
Run Code Online (Sandbox Code Playgroud)
任何想法为什么我不能设置我目前的到期时间?此服务器将采用具有不同指定到期时间的各种不同客户端,因此我认为这是执行此操作的地方.还有其他地方我应该这样做吗?谢谢!
Mic*_*ael 13
我们有类似的情况,不同的客户端具有不同的令牌超时,因此我们希望能够相应地设置到期时间.在我们实现的AuthenticationTokenProvider中,我们设置了到期时间,但是在令牌被签名时它被覆盖了.
我们最终满意的解决方案是重写TokenEndpoint方法.然后,我们可以实现客户特定的到期:
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
if (context.TokenIssued)
{
// client information
var accessExpiration = DateTimeOffset.Now.AddSeconds(accessTokenTimeoutSeconds);
context.Properties.ExpiresUtc = accessExpiration;
}
return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)
*编辑解决竞争条件.
Kév*_*let 11
您看到的行为直接是由于OAuth2授权服务器在通知中设置它时总是丢弃您自己的到期GrantResourceOwnerCredentials
(其他Grant*
通知也受到影响):https://github.com/jchannon/katanaproject/blob /master/src/Microsoft.Owin.Security.OAuth/OAuthAuthorizationServerHandler.cs#L386
解决方法是设置AuthenticationTokenProvider.CreateAsync
(您使用的类OAuthAuthorizationServerOptions.AccessTokenProvider
)的到期日期
:
只需设置context.Ticket.Properties.ExpiresUtc
您选择的到期日期,它应该按照预期的方式工作:
public class AccessTokenProvider : AuthenticationTokenProvider
{
public override void Create(AuthenticationTokenCreateContext context)
{
context.Ticket.Properties.ExpiresUtc = // set the appropriate expiration date.
context.SetToken(context.SerializeTicket());
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以查看AspNet.Security.OpenIdConnect.Server
OWIN/Katana提供的OAuth2授权服务器的分支,它本身支持设置过期日期GrantResourceOwnerCredentials
:https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree的/ dev