Ejemplo n.º 1
0
        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>(OAuthServerProvider.Client_AllowedOriginKey);

            if (allowedOrigin == null)
            {
                allowedOrigin = "*";                        // TODO: investigate why this value is null.
            }
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

            string hashedTokenId = Encryptor.GetHash(context.Token);

            using (var dbContext = new AppDatabaseContext())
            {
                var repo = new RefreshTokenRepo(dbContext);
                {
                    var refreshToken = repo.GetById(hashedTokenId);
                    if (refreshToken != null)
                    {
                        //Get protectedTicket from refreshToken class
                        context.DeserializeTicket(refreshToken.ProtectedTicket);
                        repo.Delete(hashedTokenId);
                        repo.SaveChanges();
                    }
                }
            }

            await Task.FromResult <object>(null);
        }
Ejemplo n.º 2
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);
        }