2016-11-14 45 views
1

UserManager.CreateAsync Method (TUser, String)沒有提及錯誤。使用中的錯誤情況列表_userManager.CreateAsync(用戶,密碼)

在控制器,我僅僅指剛編輯是這樣的:

public async Task<ObjectResult> Register(RegisterViewModel model, string returnUrl = null) 
{ 
    IDictionary<string, object> value = new Dictionary<string, object>(); 
    value["Success"] = false; 
    value["ReturnUrl"] = returnUrl; 

    if (ModelState.IsValid) 
    { 
     var user = new ApplicationUser { Id = Guid.NewGuid().ToString(), UserName = model.Email, Email = model.Email }; 

     var result = await _userManager.CreateAsync(user, model.Password); 

     if (result.Succeeded) 
     { 
      await _signInManager.SignInAsync(user, isPersistent: false); 
      value["Success"] = true; 
     } 
     else 
     { 
      value["ErrorMessage"] = result.Errors; 
     }  
    } 

    return new ObjectResult(value); 
} 

在客戶端:

$scope.registerForm_submit = function ($event, account) { 
    $event.preventDefault(); 

    if (registerForm.isValid(account)) { 

     // registerForm.collectData returns new FormData() which contains 
     // email, password, confirmpassword, agreement, returnurl... 

     let formData = registerForm.collectData(account),     
      xhttp = new XMLHttpRequest(); 

     xhttp.onreadystatechange = function() { 
      if (xhttp.readyState === XMLHttpRequest.DONE) { 
       let data = JSON.parse(xhttp.responseText); 

       if (data['Success']) { 
        window.location = '/' 
       } else {       
        if (data['ErrorMessage'][0]['code'] === 'DuplicateUserName') { 
         let li = angular.element('<li/>').text(`Email ${account['email']} already exists.`); 
         angular.element('div[data-valmsg-summary=true] ul').html(li); 
        } 
       } 
      } 
     } 

     xhttp.open('POST', '/account/register'); 
     xhttp.send(formData); 
    } 
}; 

我已經試過與現有電子郵件註冊新帳戶,並獲得代碼:

data['ErrorMessage'][0]['code'] === 'DuplicateUserName' 

我的問題:如何檢查其他情況?

+2

我假設,如果您使用的是ASP.NET Identity的模板化版本,那麼您需要的是:https://github.com/aspnet/Identity/blob/dev/src/Microsoft。 AspNetCore.Identity/IdentityErrorDescriber.cs –

+0

@TiesonT。正是我在找什麼。非常感謝! –

+1

實際上,之前的鏈接是針對ASP.NET Core Identity的,而不是_Exactly_與ASP.NET Identity相同。看起來後者的錯誤代碼在這裏定義:https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/Resources.Designer.cs - 它們看起來是相同的,但。 –

回答

1

在ASP中定義的錯誤代碼。NET身份在https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/Resources.Designer.cs發現 - 我已經提取出來到此列表:

  • DefaultError
  • DuplicateEmail
  • DuplicateName
  • ExternalLoginExists
  • InvalidEmail
  • InvalidToken
  • InvalidUserName
  • LockoutNotEnabled
  • NoTokenProvider
  • NoTwoFactorProvider
  • PasswordMismatch
  • PasswordRequireDigit
  • PasswordRequireLower
  • PasswordRequireNonLetterOrDigit
  • PasswordRequireUpper
  • PasswordTooShort
  • PropertyTooShort
  • RoleNotFound
  • StoreNotIQueryableRoleStore
  • StoreNotIQueryableUserStore
  • StoreNotIUserClaimStore
  • StoreNotIUserConfirmationStore
  • StoreNotIUserEmailStore
  • StoreNotIUserLockoutStore
  • StoreNotIUserLoginStore
  • StoreNotIUserPasswordStore
  • 申通reNotIUserPhoneNumberStore
  • StoreNotIUserRoleStore
  • StoreNotIUserSecurityStampStore
  • StoreNotIUserTwoFactorStore
  • UserAlreadyHasPassword
  • UserAlreadyInRole
  • UserIdNotFound
  • UserNameNotFound
  • UserNotInRole

ASP。NET核心身份這些代碼定義:

  • DefaultError
  • ConcurrencyFailure
  • PasswordMismatch
  • InvalidToken
  • LoginAlreadyAssociated
  • InvalidUserName
  • InvalidEmail
  • DuplicateUserName
  • DuplicateEmail
  • InvalidRoleName
  • DuplicateRoleName
  • UserAlreadyHasPassword
  • UserLockoutNotEnabled
  • UserAlreadyInRole
  • UserNotInRole
  • PasswordTooShort
  • PasswordRequiresNonAlphanumeric
  • PasswordRequiresDigit
  • PasswordRequiresLower
  • PasswordRequiresUpper

因此,它可能不是所有的前錯誤代碼實際上將在IdentityResult出現。我也不使用,所以這就是我通過瀏覽可用的源代碼而得到的結果。買者自負......

看起來這應該被記錄的地方...

我喜歡這種性質在一個地方定義字符串,所以我通常做類似:

public class IdentityErrorCodes 
{ 
    public const string DefaultError     = "DefaultError"; 
    public const string ConcurrencyFailure    = "ConcurrencyFailure"; 
    public const string PasswordMismatch    = "PasswordMismatch"; 
    public const string InvalidToken     = "InvalidToken"; 
    public const string LoginAlreadyAssociated   = "LoginAlreadyAssociated"; 
    public const string InvalidUserName     = "InvalidUserName"; 
    public const string InvalidEmail     = "InvalidEmail"; 
    public const string DuplicateUserName    = "DuplicateUserName"; 
    public const string DuplicateEmail     = "DuplicateEmail"; 
    public const string InvalidRoleName     = "InvalidRoleName"; 
    public const string DuplicateRoleName    = "DuplicateRoleName"; 
    public const string UserAlreadyHasPassword   = "UserAlreadyHasPassword"; 
    public const string UserLockoutNotEnabled   = "UserLockoutNotEnabled"; 
    public const string UserAlreadyInRole    = "UserAlreadyInRole"; 
    public const string UserNotInRole     = "UserNotInRole"; 
    public const string PasswordTooShort    = "PasswordTooShort"; 
    public const string PasswordRequiresNonAlphanumeric = "PasswordRequiresNonAlphanumeric"; 
    public const string PasswordRequiresDigit   = "PasswordRequiresDigit"; 
    public const string PasswordRequiresLower   = "PasswordRequiresLower"; 
    public const string PasswordRequiresUpper   = "PasswordRequiresUpper"; 

    public static string[] All = { 
     "DefaultError", 
     "ConcurrencyFailure", 
     "PasswordMismatch", 
     "InvalidToken", 
     "LoginAlreadyAssociated", 
     "InvalidUserName", 
     "InvalidEmail", 
     "DuplicateUserName", 
     "DuplicateEmail", 
     "InvalidRoleName", 
     "DuplicateRoleName", 
     "UserAlreadyHasPassword", 
     "UserLockoutNotEnabled", 
     "UserAlreadyInRole", 
     "UserNotInRole", 
     "PasswordTooShort", 
     "PasswordRequiresNonAlphanumeric", 
     "PasswordRequiresDigit", 
     "PasswordRequiresLower", 
     "PasswordRequiresUpper" 
    }; 
} 

這樣可以使您在查找中使用的鍵保持一致,並且最後一個字段All爲您提供了一個可以枚舉的數組(如有必要)。

使用你的代碼,你可以這樣做:

if(data['ErrorMessage'][0]['code'] == IdentityErrorCodes.DuplicateUserName) 
{ 
} 

等。

+0

在我的例子中丟失了1例:'DuplicateUserName'。 –

+0

您是否使用ASP.NET Core?我確實添加了我在那裏找到的錯誤代碼,其中包括DuplicateUserName。否則,UserManager可能會在某處定義一些魔術字符串... –

+0

是的。我正在使用asp.net核心。更新是匹配的情況。謝謝! –

相關問題