Ejemplo n.º 1
0
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var clientid = context.Ticket.Properties.Dictionary[OAuthServerProvider.ClientIdKey];

            if (string.IsNullOrEmpty(clientid))
            {
                return;
            }

            var refreshTokenId = Guid.NewGuid().ToString("n");

            using (var dbContext = new AppDatabaseContext())
            {
                var  repo = new RefreshTokenRepo(dbContext);
                var  refreshTokenLifeTime = context.OwinContext.Get <string>(OAuthServerProvider.Client_RefreshTokenLifeTimeKey);
                long userId = -1;
                long.TryParse(context.Ticket.Identity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value, out userId);

                var token = new RefreshToken()
                {
                    Id           = Encryptor.GetHash(refreshTokenId), // stored hashed values only
                    ClientId     = clientid,
                    UserId       = userId,
                    IssuedOnUtc  = DateTime.UtcNow,
                    ExpiresOnUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
                };

                context.Ticket.Properties.IssuedUtc  = token.IssuedOnUtc;
                context.Ticket.Properties.ExpiresUtc = token.ExpiresOnUtc;

                token.ProtectedTicket = context.SerializeTicket();

                repo.Insert(token);
                repo.SaveChanges();
                context.SetToken(refreshTokenId);
            }

            await Task.FromResult <object>(null);
        }