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'
我的問題:如何檢查其他情況?
我假設,如果您使用的是ASP.NET Identity的模板化版本,那麼您需要的是:https://github.com/aspnet/Identity/blob/dev/src/Microsoft。 AspNetCore.Identity/IdentityErrorDescriber.cs –
@TiesonT。正是我在找什麼。非常感謝! –
實際上,之前的鏈接是針對ASP.NET Core Identity的,而不是_Exactly_與ASP.NET Identity相同。看起來後者的錯誤代碼在這裏定義:https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/Resources.Designer.cs - 它們看起來是相同的,但。 –