C# ASP.NET Identity在用户表中为1个用户创建2行
我正在ASP.NET MVC#中创建一个应用程序。我已经选择了Invidual用户帐户选项并安装了Identity软件包。我在AccountController中配置了注册和登录功能,以执行一些额外的操作。我添加了在用户注册时添加角色的功能,如C# ASP.NET Identity在用户表中为1个用户创建2行,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在ASP.NET MVC#中创建一个应用程序。我已经选择了Invidual用户帐户选项并安装了Identity软件包。我在AccountController中配置了注册和登录功能,以执行一些额外的操作。我添加了在用户注册时添加角色的功能,如UserManager.AddToRole(user.Id,Student)所示。我还将其设置为,它会自动在用户表中创建一个新行来存储信息 注册: [HttpPost] [AllowAnonymous] [ValidateA
UserManager.AddToRole(user.Id,Student)
所示。我还将其设置为,它会自动在用户表中创建一个新行来存储信息
注册:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
UserManager.AddToRole(user.Id, "Student"); //Will automatically add users as students once registered
Service.IService Service = new Service.Service();
Data.User newUser = new Data.User
{
UserId = user.Id,
Username = model.Name,
Email = model.Email
}; //Will insert a new row in the User class with the attributes listed
Service.AddUser(newUser);
Session.Add("UserId", user.Id); //Adds User to the session
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("AddUser", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
Service.IService Service =
new Service.Service();
Data.User loginUser = Service.GetUserEmail(model.Email);
Session.Add("UserId", loginUser.UserId);
var um = new UserManager<ApplicationUser>(
new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(new ApplicationDbContext()));
IList<string> roles = um.GetRoles(loginUser.UserId);
Session.Add("Roles", roles);
return RedirectToAction("GetUser", "Home", new { id = loginUser.UserId });
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
public User GetUserEmail(string email, MyContext context) //Retrieves user information by querying both tables to find matching rows
{
IQueryable<User> user = from u in context.Users where u.Email == email select u;
return user.ToList().First();
}
当我查看用户表时,每个用户有2行。第一行包含他们在注册时创建的用户标识,然后是另一行,其中包含从Identity创建的哈希用户标识。这就是它崩溃的地方,它试图检索错误的用户ID。我有一个运行良好的应用程序,只在用户表中插入1行,为什么会发生这种情况
在
寄存器
操作中,此行创建一个用户:
var result = await UserManager.CreateAsync(user, model.Password);
然后在下面再次插入用户:
Service.AddUser(newUser);
如果您想在用户插入数据库后更新该用户,假设您使用的是实体框架
,那么您应该能够使用上下文。更新(用户)代码>而是创建一个newUser
对象并添加它。然后调用context.saveChangesSync()代码>
如果它不是实体框架,并且是一个自定义服务,具有您自己的数据库读写方法,那么只需从数据库中获取用户对象,更新值,并调用您的更新方法。您已经使用UserManager CreateAncy方法创建了用户,那么为什么要再次使用service.AddUser(newUser)添加用户?