I've tried every answer I could find on SO. I am trying to add Identity to my ASP.NET MVC 5 project. 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.
https://gist.github.com/jeroenheijmans/8fa79427abc25a864cb055616644172f
Here is my Startup.cs file
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:
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
public partial class AppDbContext: IdentityDbContext<AspNetUser>
{
public AppDbContext(string connectionString)
: base(connectionString)
{}
}
Check model.Email
value. 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:
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.