Nan*_*ndo 5 c# oauth owin asp.net-web-api2
要授予或撤消对我的webapi的访问权限,我使用OAuth密码和tokenrefreshworkflow。
如果我正确理解所有工作流程,则工作流程应如下所示:
到目前为止,上述工作进展顺利。我的问题是,在身份验证请求之后,我没有使过期时间超出用户原则。因此,如果我使用Stateles Webclients,则即使用户令牌有效,我也需要在每次请求中都更新令牌以获取新的到期日期:/
我想要的是/ api / session / information服务之类的东西,它提供有关已认证用户当前会话的一般信息。
如何获取我的到期日期=)
[HttpGet]
[ActionName("information")]
public HttpResponseMessage Information(BaseRequest request)
{
var p = Request.GetRequestContext().Principal;
/* here i need help =) */
}
Run Code Online (Sandbox Code Playgroud)
只是为了扩大Henrik N.的答案。如果您使用的是C#,则可以JWTSecurityTokenHandler在System.IdentityModel.Tokens.Jwt(Nuget:)中使用Install-Package System.IdentityModel.Tokens.Jwt该标记来读取令牌,并且生成的JwtSecurityToken对象为您提供一些方便的属性,其中之一是ValidTo将exp声明转换为DateTime适合您的对象,例如:
var tokenString = GetTokenString(); // Arbitrary method to get the token
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadToken(tokenString) as JwtSecurityToken;
var tokenExpiryDate = token.ValidTo;
// If there is no valid `exp` claim then `ValidTo` returns DateTime.MinValue
if(tokenExpiryDate == DateTime.MinValue) throw new Exception("Could not get exp claim from token");
// If the token is in the past then you can't use it
if(tokenExpiryDate < DateTime.UtcNow) throw new Exception($"Token expired on: {tokenExpiryDate}");
// Token is valid
Run Code Online (Sandbox Code Playgroud)
小智 6
您的访问令牌(JWT?)应该包含到期声明。在JWT中,它是“ exp”,表示自1970-1-1以来的秒数。在javascript中,您可以像这样获取日期:
new Date(<exp> * 1000);
Run Code Online (Sandbox Code Playgroud)
在.Net / C#中,您可以执行以下操作:
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(<exp>);
Run Code Online (Sandbox Code Playgroud)
那是您要找的东西吗?否则让我知道。乐意效劳 :-)
| 归档时间: |
|
| 查看次数: |
9910 次 |
| 最近记录: |