Answered by:
Add time for Bearer Token ?

Question
-
User1690434716 posted
I use "bearer token" and i want once client post request to server include token,server will add "expires_in" time for this token.
EX : first "expires_in"=1800,after server get token from client, "expires_in_after"="expires_in"+1000.
So It can happen ?
Tuesday, February 23, 2016 10:39 AM
Answers
-
User36583972 posted
Hi Ken.N,
According to your description, I suggest you can try the following code.
/// <summary> /// Updating existing Token validity /// </summary> /// <param name="Token"></param> /// <returns></returns> public HttpResponseMessage UpdateTokenTime(string Token) { //First Token decryption AuthenticationTicket ticket = Startup.OAuthOptions.AccessTokenFormat.Unprotect(Token); if (ticket != null && (ticket.Properties != null && ticket.Properties.ExpiresUtc.HasValue )) { //Change the time - Increased 1000 seconds ticket.Properties.ExpiresUtc.Value.AddSeconds(1000); } //Token encryption protection string token = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket); //Back token, the client saved. return new HttpResponseMessage(HttpStatusCode.OK) { Content = new ObjectContent<object>(new { accessToken = token, expiresIn = (int)((ticket.Properties.ExpiresUtc.Value - ticket.Properties.IssuedUtc.Value).TotalSeconds), }, Configuration.Formatters.JsonFormatter) }; }
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 24, 2016 3:30 AM
All replies
-
User36583972 posted
Hi Ken.N,
According to your description, I suggest you can try the following code.
/// <summary> /// Updating existing Token validity /// </summary> /// <param name="Token"></param> /// <returns></returns> public HttpResponseMessage UpdateTokenTime(string Token) { //First Token decryption AuthenticationTicket ticket = Startup.OAuthOptions.AccessTokenFormat.Unprotect(Token); if (ticket != null && (ticket.Properties != null && ticket.Properties.ExpiresUtc.HasValue )) { //Change the time - Increased 1000 seconds ticket.Properties.ExpiresUtc.Value.AddSeconds(1000); } //Token encryption protection string token = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket); //Back token, the client saved. return new HttpResponseMessage(HttpStatusCode.OK) { Content = new ObjectContent<object>(new { accessToken = token, expiresIn = (int)((ticket.Properties.ExpiresUtc.Value - ticket.Properties.IssuedUtc.Value).TotalSeconds), }, Configuration.Formatters.JsonFormatter) }; }
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 24, 2016 3:30 AM -
User1690434716 posted
Thank you, Yohann .
But if i want to use"your code" in "OAuthAuthorizationServerOptions class" ? I means that i want read timeout in string token and add time for it.
Friday, February 26, 2016 12:17 AM -
User36583972 posted
Hi Ken.N,
You can Compare deadline and the current time as the following method.
if (ticket.Properties.ExpiresUtc.Value < DateTimeOffset.UtcNow) { //Add 1000 seconds }
Best Regards,
Yohann Lu
Friday, February 26, 2016 10:04 AM -
User1690434716 posted
Thank you so much , Yohann.
Saturday, February 27, 2016 12:48 AM