IwbRefreshTokenProvider.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Threading.Tasks;
  4. using Abp.Dependency;
  5. using Microsoft.Owin.Security;
  6. using Microsoft.Owin.Security.Infrastructure;
  7. namespace WeEngine.Api.Providers
  8. {
  9. public class IwbRefreshTokenProvider : IAuthenticationTokenProvider, ITransientDependency
  10. {
  11. private static ConcurrentDictionary<string, AuthenticationTicket> _refreshTokens = new ConcurrentDictionary<string, AuthenticationTicket>();
  12. public Task CreateAsync(AuthenticationTokenCreateContext context)
  13. {
  14. var guid = Guid.NewGuid().ToString("N");
  15. // maybe only create a handle the first time, then re-use for same client
  16. // copy properties and set the desired lifetime of refresh token
  17. var refreshTokenProperties = new AuthenticationProperties(context.Ticket.Properties.Dictionary)
  18. {
  19. IssuedUtc = context.Ticket.Properties.IssuedUtc,
  20. ExpiresUtc = DateTime.UtcNow.AddYears(1)
  21. };
  22. var refreshTokenTicket = new AuthenticationTicket(context.Ticket.Identity, refreshTokenProperties);
  23. //_refreshTokens.TryAdd(guid, context.Ticket);
  24. _refreshTokens.TryAdd(guid, refreshTokenTicket);
  25. // consider storing only the hash of the handle
  26. context.SetToken(guid);
  27. return Task.FromResult<object>(null);
  28. }
  29. public Task ReceiveAsync(AuthenticationTokenReceiveContext context)
  30. {
  31. AuthenticationTicket ticket;
  32. if (_refreshTokens.TryRemove(context.Token, out ticket))
  33. {
  34. context.SetTicket(ticket);
  35. }
  36. return Task.FromResult<object>(null);
  37. }
  38. public void Create(AuthenticationTokenCreateContext context)
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public void Receive(AuthenticationTokenReceiveContext context)
  43. {
  44. throw new NotImplementedException();
  45. }
  46. }
  47. }