我正在尝试使用电子邮件以asp.net身份进行登录身份验证,因此,我以注册形式创建了一个用户名字段,但是当我尝试登录时,出现错误的无效登录尝试。因此,在asp.net用户表中,我复制了输入的电子邮件并将其放入用户名列,这样我就成功登录了here is my user table
因此,它在用户名栏中查找电子邮件,如何解决此问题? 这是我的登录方法
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:
return RedirectToLocal(returnUrl);
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 async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Name, Email = model.Email,};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
答案 0 :(得分:0)
已编辑 如果用户可以输入用户名或电子邮件,那么我们将使用@字符作为验证标准。这是要实现的流程:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (model.Email.IndexOf('@') > -1)
{
//Validate email format
string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(emailRegex);
if (!re.IsMatch(model.Email))
{
ModelState.AddModelError("Email", "Email is not valid");
}
}
else
{
//validate Username format
string emailRegex = @"^[a-zA-Z0-9]*$";
Regex re = new Regex(emailRegex);
if (!re.IsMatch(model.Username))
{
ModelState.AddModelError("Username", "Username is not valid");
}
}
if (ModelState.IsValid)
{
var userName = model.Email;
if (userName.IndexOf('@') > -1)
{
var user = await _userManager.FindByEmailAsync(model.Email);
if (user == null)
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
}
var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, lockoutOnFailure: false);
}
}
public class LoginViewModel
{
[Required]
[Display(Name = "Username")]
public string Username{ get; set; }
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
答案 1 :(得分:0)
该方法是在搜索用户名而不是电子邮件,因为您输入错误。
这些是PasswordSignInAsync
的重载。
超载
PasswordSignInAsync(String, String, Boolean, Boolean)
尝试将指定的用户名和密码组合作为异步操作登录。
PasswordSignInAsync(TUser, String, Boolean, Boolean)
尝试将指定的用户名和密码组合作为异步操作登录。
因此,在您的情况下,您只是在使用第一个。
修改 要使用第二个重载,只需使用该记录搜索用户。
var user=await userManager.FindByEmailAsync(vm.Email);
var result=await signInManager.PasswordSignInAsync(user,vm.Password,false,false);
if(result.Succeeded){
//Do some other things.
}