コード例 #1
0
ファイル: TokenLogic.cs プロジェクト: 04mayank/MyIdentity
        // Method to Create New JWT and Refresh Token
        private async Task <TokenResponseModel> GenerateNewToken(TokenRequestModel model)
        {
            // check if there's an user with the given username
            var user = await _userManager.FindByEmail(model.UserName);

            // Validate credentials
            if (user != null && await _userManager.CheckPassword(user, model.Password))
            {
                //If the user has confirmed his email
                if (!await _userManager.IsEmailConfirmedAsync(user))
                {
                    return(null);
                }

                // username & password matches: create the refresh token
                var newRtoken = CreateRefreshToken(_appSettings.ClientId, user.Id);

                // first we delete any existing old refreshtokens
                await RemoveTokensById(user.Id);

                // Add new refresh token to Database
                await _dalLayer.AddToken(newRtoken);

                // Create & Return the access token which contains JWT and Refresh Token
                TokenResponseModel accessToken = await CreateAccessToken(user, newRtoken.Value);

                return(accessToken);
            }
            return(null);
        }