コード例 #1
0
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var userId = context.Ticket.Properties.Dictionary["userId"];

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

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

            using (var repo = new SecurityDbContext())
            {
                var refreshTokenLifeTime = API.Utils.Configuration.RefreshTokenExpireTimeMinutes;
                var token = new RefreshToken()
                {
                    IssuedUtc     = DateTime.UtcNow,
                    ExpiresUtc    = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime)),
                    Token         = refreshTokenValue,
                    AspNetUsersId = userId
                };

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

                token.ProtectedTicket = context.SerializeTicket();

                var result = await repo.AddRefreshToken(token);

                if (result)
                {
                    context.SetToken(refreshTokenValue);
                }
            }
        }