Exemplo n.º 1
0
        private async Task <bool> TwoFactorClientRememberedAsync(UserIdentifier userIdentifier,
                                                                 AuthenticateModel authenticateModel)
        {
            if (!await SettingManager.GetSettingValueAsync <bool>(AbpZeroSettingNames.UserManagement.TwoFactorLogin
                                                                  .IsRememberBrowserEnabled))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(authenticateModel.TwoFactorRememberClientToken))
            {
                return(false);
            }

            try
            {
                var validationParameters = new TokenValidationParameters
                {
                    ValidAudience    = _configuration.Audience,
                    ValidIssuer      = _configuration.Issuer,
                    IssuerSigningKey = _configuration.SecurityKey
                };

                foreach (var validator in _jwtOptions.Value.SecurityTokenValidators)
                {
                    if (validator.CanReadToken(authenticateModel.TwoFactorRememberClientToken))
                    {
                        try
                        {
                            SecurityToken validatedToken;
                            var           principal = validator.ValidateToken(authenticateModel.TwoFactorRememberClientToken,
                                                                              validationParameters, out validatedToken);
                            var useridentifierClaim = principal.FindFirst(c => c.Type == UserIdentifierClaimType);
                            if (useridentifierClaim == null)
                            {
                                return(false);
                            }

                            return(useridentifierClaim.Value == userIdentifier.ToString());
                        }
                        catch (Exception ex)
                        {
                            Logger.Debug(ex.ToString(), ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.ToString(), ex);
            }

            return(false);
        }