[英]ASP.NET Identity CreateAsync returning Error “Name cannot be null or empty.”
我已经尝试了所有可以找到的答案。 我正在尝试将身份添加到我的ASP.NET MVC 5项目中。 我首先使用数据库。 已经存在一个数据库。 我使用来自该GitHub的脚本在数据库中创建身份表。
https://gist.github.com/jeroenheijmans/8fa79427abc25a864cb055616644172f
这是我的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")
});
}
这是我的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")
});
}
这是我的注册动作:
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);
}
我的角色班
public partial class AspNetRole : IdentityRole
{
public AspNetRole(string name) { Name = name; }
}
我的用户类别
public partial class AspNetUser : IdentityUser
{
}
我的DbContext
public partial class AppDbContext: IdentityDbContext<AspNetUser>
{
public AppDbContext(string connectionString)
: base(connectionString)
{}
}
检查model.Email
值。 似乎它为空/空。 为了确保这一点,请将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.