Example #1
0
        public async Task <AppWrapper <TokenSet> > GenerateTokenSetAsync(string username)
        {
            try
            {
                var expiration = GetTokenExpiration();
                var token      = Jwt.GenerateJwt(username, this._secretKey, expiration);

                var refreshExpiration = GetRefreshExpiration();
                var refreshToken      = Jwt.GenerateRefreshToken();

                var tokenRecord = new JwtToken
                {
                    Username             = username,
                    Token                = token,
                    TokenExpirationUtc   = expiration,
                    RefreshToken         = refreshToken,
                    RefreshExpirationUtc = refreshExpiration,
                    DateIssuedUtc        = DateTime.UtcNow
                };

                var saveResult = await _repo.SaveJwtTokenAsync(tokenRecord);

                if (!saveResult.Success)
                {
                    return(new AppWrapper <TokenSet>(saveResult.Exception, saveResult.Message));
                }

                return(new AppWrapper <TokenSet>(new TokenSet
                {
                    Token = token,
                    RefreshToken = refreshToken,
                    ExpirationUtc = expiration.ToISOString()
                }));
            }
            catch (Exception ex)
            {
                return(new AppWrapper <TokenSet>(ex, "Unable to GenerateTokenSetAsync"));
            }
        }