gpt4 book ai didi

asp.net - OAuth2 Web Api token 到期

转载 作者:行者123 更新时间:2023-12-03 21:18:49 25 4
gpt4 key购买 nike

我正在尝试动态设置 token 到期时间,但它似乎一直默认为 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);

}

这是我的 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);
}

这是我的 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;
}

即使在那之后, token 也会返回:
{
"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"
}

有什么想法为什么我不能在我现在的位置设置到期时间?该服务器将采用具有不同指定到期时间的各种不同客户端,因此我认为这是执行此操作的地方。还有其他地方我应该这样做吗?谢谢!

最佳答案

我们有类似的情况,不同的客户端具有不同的 token 超时,因此我们希望能够相应地设置到期时间。在我们实现的 AuthenticationTokenProvider 中,我们设置了到期时间,但它在 token 签署时被覆盖。

我们最终满意的解决方案是覆盖 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);
}

*编辑以解决竞争条件。

关于asp.net - OAuth2 Web Api token 到期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com