有没有办法让Asp.Net Identity对电子邮件地址和用户名不区分大小写?
目前,如果我打电话给“FindByEmailAsync(电子邮件)”,那么只有在电子邮件地址的输入完全符合其输入时才会有效(区分大小写)
答案 0 :(得分:4)
您可以更改用户的注册方式,以便将用户名设置为小写以及登录时。
用于在AccountController中注册用户
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.Email.ToLowerInvariant(), Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
登录:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.Email.ToLowerInvariant(), model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}