Tân Nguyễn Tân Nguyễn - 22 days ago 9
ASP.NET (C#) Question

List of error cases in use _userManager.CreateAsync(user, password)

UserManager.CreateAsync Method (TUser, String) doesn't mention about errors.

In controller, I jsut edit something like:

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);
}


In client:

$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);
}
};


I've tried to register new account with an existing email and got the code:

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


My question: how to check other cases?

Answer

The error codes defined in ASP.NET Identity are found at https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/Resources.Designer.cs - I've extracted them out to this list:

  • 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
  • StoreNotIUserPhoneNumberStore
  • StoreNotIUserRoleStore
  • StoreNotIUserSecurityStampStore
  • StoreNotIUserTwoFactorStore
  • UserAlreadyHasPassword
  • UserAlreadyInRole
  • UserIdNotFound
  • UserNameNotFound
  • UserNotInRole

ASP.NET Core Identity has these codes defined:

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

So, it's possible that not all of the former error codes will actually show up in an IdentityResult. I don't use either, so this is just what I gather from skimming the available source code. Caveat emptor...

Seems like this should be documented somewhere...

Comments