[英]UserManager.CreateAsync .Success always returns false
I'm trying to add users programmatically to the AspNetUsers table in my ASP.Net MVC 5.0 app using the following function: 我试图使用以下功能以编程方式将用户添加到ASP.Net MVC 5.0应用程序中的AspNetUsers表中:
private async Task AddUser(DataImportMember member)
{
var user = new ApplicationUser
{
UserName = member.Email,
Email = member.Email,
UserType = UserType.IsMember
};
var password = RandomPassword.Generate();
var result = await this.UserManager.CreateAsync(user, password);
if (result.Succeeded)
{
await this.UserManager.AddToRoleAsync(user.Id, MyApp.IsMember);
}
}
But whenever I call this.UserManager.CreateAsync
, result.Succeed
is always false. 但是每当我调用
this.UserManager.CreateAsync
, result.Succeed
始终为false。 I've checked the AspNetUsers table and as expected, the user is not added. 我已经检查了AspNetUsers表,并且按预期方式,未添加用户。
Any ideas how I can figure out what's wrong and how to resolve it? 有什么想法可以找出问题所在以及如何解决吗?
Thanks. 谢谢。
UserManager.CreateAsync
returns an IdentityResult
that would also contain any errors why the action was not successful. UserManager.CreateAsync
返回一个IdentityResult
,该结果还将包含导致操作失败的任何错误。 You should check the result error messages to find out why the action failed. 您应该检查结果错误消息,以找出导致操作失败的原因。
var result = await this.UserManager.CreateAsync(user, password);
if (result.Succeeded) {
await this.UserManager.AddToRoleAsync(user.Id, MyApp.IsMember);
} else {
var errors = result.Errors;
var message = string.Join(", ", errors);
ModelState.AddModelError("", message);
}
You can put try catch for checking the actual error. 您可以使用try catch来检查实际错误。
try{
var result = await this.UserManager.CreateAsync(user, password);
}
catch(exception ex)
{
through ex;
}
After getting the exact error it will be better to remove the try catch block. 得到确切的错误后,最好删除try catch块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.