Asp.net mvc 5 如何更改UserManager逻辑,以便用户在注册之前必须存在于数据库中
我正在自定义MVC5注册过程,以便用户在注册时必须输入两个自定义字段“MyNewField1”和“MyNewField2”,然后根据用户上下文进行检查,以确保它们是否存在,在这种情况下,可以通过更新当前用户成功注册Asp.net mvc 5 如何更改UserManager逻辑,以便用户在注册之前必须存在于数据库中,asp.net-mvc-5,token,dbcontext,two-factor-authentication,usermanager,Asp.net Mvc 5,Token,Dbcontext,Two Factor Authentication,Usermanager,我正在自定义MVC5注册过程,以便用户在注册时必须输入两个自定义字段“MyNewField1”和“MyNewField2”,然后根据用户上下文进行检查,以确保它们是否存在,在这种情况下,可以通过更新当前用户成功注册 public async Task<ActionResult> CustomRegister(CustomRegisterViewModel model) { if (ModelState.IsValid) {
public async Task<ActionResult> CustomRegister(CustomRegisterViewModel model)
{
if (ModelState.IsValid)
{
var context = new ApplicationDbContext();
ApplicationUser user = context.Users.Where(a => a.MyNewField1== model.MyNewField1& a.MyNewField2== a.MyNewField2).SingleOrDefault();
if(user != null)
{
var emailCheck = await UserManager.FindByNameAsync(model.Email);
if (emailCheck == null)
{
//We have found a user and email address has not been already assigned to another
//assign the email entered for this user in place of the username and email place
//holders and update the user before saving to the database
user.UserName = model.Email;
user.Email = model.Email;
var hasher = new PasswordHasher();
user.PasswordHash = hasher.HashPassword(model.Password);
context.SaveChanges();
var 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, "Budget Energy Email Verification", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
ViewBag.Message = "Check your email and confirm your account, you must be confirmed before you can log in.";
return View("Info");
}
else
{
//This email address is already assigned to a user
return View(model);
}
}
else
{
//No user exists with these details so redisplay form
return View(model);
}
}
}
公共异步任务);
Link=callbackUrl;
ViewBag.Message=“检查您的电子邮件并确认您的帐户,您必须先确认,然后才能登录。”;
返回视图(“信息”);
}
其他的
{
//此电子邮件地址已分配给用户
返回视图(模型);
}
}
其他的
{
//不存在具有这些详细信息的用户,因此重新显示表单
返回视图(模型);
}
}
}
此方法正在成功传递,我被告知已发送电子邮件,但当我单击此电子邮件链接时,我被带到错误页面,错误为无效令牌。因为我已更改了此处的逻辑,我是否必须以其他方式创建令牌?我能够按如下方式解决此问题:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> BillpayRegister(BillpayRegisterViewModel model)
{
if (ModelState.IsValid)
{
var context = new ApplicationDbContext();
ApplicationUser customer = context.Users.Where(a => a.MyNewField1 == model.MyNewField1 & a.MyNewField2 == model.MyNewField2).SingleOrDefault();
if(customer != null)
{
var emailCheck = await UserManager.FindByNameAsync(model.Email);
if (emailCheck == null)
{
//We have found a user and email address has not been already assigned to another
//assign the email entered for this user in place of the username and email place
//holders and update the user before saving to the database
var user = UserManager.FindById(customer.Id);
user.UserName = model.Email;
UserManager.SetEmail(user.Id, model.Email);
string hashedNewPassword = UserManager.PasswordHasher.HashPassword(model.Password);
user.PasswordHash = hashedNewPassword;
var result = UserManager.Update(user);
if (result.Succeeded)
{
var 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, "Email Verification", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
ViewBag.Message = "Check your email and confirm your account, you must be confirmed before you can log in.";
return View("Info");
}
}
else
{
//This email address is already assigned to a user
return View(model);
}
}
else
{
//No user exists with these details so redisplay form
return View(model);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务);
Link=callbackUrl;
ViewBag.Message=“检查您的电子邮件并确认您的帐户,您必须先确认,然后才能登录。”;
返回视图(“信息”);
}
}
其他的
{
//此电子邮件地址已分配给用户
返回视图(模型);
}
}
其他的
{
//不存在具有这些详细信息的用户,因此重新显示表单
返回视图(模型);
}
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
有人对此有什么想法吗?如果你能帮我的话,我可以给你50英镑。问题不在于你的逻辑,而在于代币。你知道我拿代币做错了什么吗?你知道如何生成有效的令牌吗?我希望我的逻辑是合理的,但似乎找不到为电子邮件确认生成有效令牌的方法,我认为我可能需要使用与我调用密码哈希相关类类似的方法创建安全戳。您可以获取无效令牌的原因有几个。显示它生成的令牌和使用的url。如何根据我的逻辑方法生成有效的令牌?