gpt4 book ai didi

c# - 使用中的错误案例列表 _userManager.CreateAsync(user, password)

转载 作者:太空狗 更新时间:2023-10-30 00:18:15 25 4
gpt4 key购买 nike

UserManager.CreateAsync Method (TUser, String)没有提到错误。

在 Controller 中,我只是编辑如下内容:

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'

我的问题:如何检查其他情况?

最佳答案

在 ASP.NET Identity 中定义的错误代码位于 https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/Resources.Designer.cs - 我已将它们提取到此列表中:

  • 默认错误
  • 重复邮件
  • 重复姓名
  • 存在外部登录
  • 无效的邮箱
  • 无效 token
  • 无效的用户名
  • LockoutNotEnabled
  • NoTokenProvider
  • NoTwoFactorProvider
  • 密码不匹配
  • 密码需要数字
  • 密码要求较低
  • 密码需要非字母或数字
  • 密码要求上限
  • 密码太短
  • 属性太短
  • 未找到角色
  • StoreNotIQueryableRoleStore
  • StoreNotIQueryableUserStore
  • StoreNotIUserClaimStore
  • StoreNotIUserConfirmationStore
  • StoreNotIUserEmailStore
  • StoreNotIUserLockoutStore
  • StoreNotIUserLoginStore
  • StoreNotIUserPasswordStore
  • StoreNotIUserPhoneNumberStore
  • StoreNotIUserRoleStore
  • StoreNotIUserSecurityStampStore
  • StoreNotIUserTwoFactorStore
  • 用户已有密码
  • UserAlreadyInRole
  • 未找到用户名
  • 找不到用户名
  • UserNotInRole

ASP.NET Core Identity 定义了这些代码:

  • 默认错误
  • 并发失败
  • 密码不匹配
  • 无效 token
  • LoginAlreadyAssociated
  • 无效的用户名
  • 无效的邮箱
  • 重复用户名
  • 重复邮件
  • 无效角色名
  • DuplicateRoleName
  • 用户已有密码
  • UserLockoutNotEnabled
  • UserAlreadyInRole
  • UserNotInRole
  • 密码太短
  • 密码需要非字母数字
  • 密码需要数字
  • 密码要求较低
  • 密码要求上限

因此,可能并非所有以前的错误代码都会实际显示在 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)
{
}

等等。

关于c# - 使用中的错误案例列表 _userManager.CreateAsync(user, password),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40583707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com