[英]ASP.NET Identity CreateAsync returning Error “Name cannot be null or empty.”
I've tried every answer I could find on SO. 我已经尝试了所有可以找到的答案。 I am trying to add Identity to my ASP.NET MVC 5 project. 我正在尝试将身份添加到我的ASP.NET MVC 5项目中。 I am using Database first. 我首先使用数据库。 There is already an existing database. 已经存在一个数据库。 I used the scripts from this GitHub to create the identity tables in my database. 我使用来自该GitHub的脚本在数据库中创建身份表。
https://gist.github.com/jeroenheijmans/8fa79427abc25a864cb055616644172f https://gist.github.com/jeroenheijmans/8fa79427abc25a864cb055616644172f
Here is my Startup.cs file 这是我的Startup.cs文件
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(() => new AppDbContext());
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<RoleManager<AspNetRole>>((options, context) =>
new RoleManager<AspNetRole>(
new RoleStore<AspNetRole>(context.Get<AppDbContext>())));
app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
Here is my ApplicationUserManager: 这是我的ApplicationUserManager:
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(() => new AppDbContext());
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<RoleManager<AspNetRole>>((options, context) =>
new RoleManager<AspNetRole>(
new RoleStore<AspNetRole>(context.Get<AppDbContext>())));
app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
Here is my register action: 这是我的注册动作:
public async Task<ActionResult> Register()
{
var model = (RegisterUserModel)Session["RegisterAccount"];
var user = new AspNetUser()
{
UserName = model.Email,
Email = model.Email
};
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
IdentityResult addUserResult = await userManager.CreateAsync(user, model.Password);
}
My Role class 我的角色班
public partial class AspNetRole : IdentityRole
{
public AspNetRole(string name) { Name = name; }
}
My User Class 我的用户类别
public partial class AspNetUser : IdentityUser
{
}
My DbContext 我的DbContext
public partial class AppDbContext: IdentityDbContext<AspNetUser>
{
public AppDbContext(string connectionString)
: base(connectionString)
{}
}
Check model.Email
value. 检查model.Email
值。 It seems that it is null/empty. 似乎它为空/空。 To make sure, put the Register
method into a try-catch
block and try to extract more details about the error/exception: 为了确保这一点,请将Register
方法放入try-catch
块中,并尝试提取有关错误/异常的更多详细信息:
public async Task<ActionResult> Register()
{
try
{
var model = (RegisterUserModel)Session["RegisterAccount"];
var user = new AspNetUser()
{
UserName = model.Email,
Email = model.Email
};
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
IdentityResult addUserResult = await userManager.CreateAsync(user, model.Password);
if (!addUserResult.Succeeded)
{
//loop over addUserResult.Errors for more detail
}
}
catch (Exception exception)
{
//here, check the exception for more details
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.