允许用户名或电子邮件登录的Asp.net标识系统
我在一个项目中,我已经设置了电子邮件确认,忘记了电子邮件的密码链接,并希望自定义登录结构,以启用用户名或密码登录。我已将帐户控制器更改为启用用户名,用户已成功创建,但无法使用电子邮件地址登录 是的,在挖掘了我以前的identity framework项目后,它可能只接受用户名登录,而这只允许电子邮件登录。如果其他人想要实现类似的功能,我可以将逻辑结合起来,给出我想要的功能。这是我的代码允许用户名或电子邮件登录的Asp.net标识系统,asp.net,asp.net-mvc,vb.net,linq,visual-studio-2013,Asp.net,Asp.net Mvc,Vb.net,Linq,Visual Studio 2013,我在一个项目中,我已经设置了电子邮件确认,忘记了电子邮件的密码链接,并希望自定义登录结构,以启用用户名或密码登录。我已将帐户控制器更改为启用用户名,用户已成功创建,但无法使用电子邮件地址登录 是的,在挖掘了我以前的identity framework项目后,它可能只接受用户名登录,而这只允许电子邮件登录。如果其他人想要实现类似的功能,我可以将逻辑结合起来,给出我想要的功能。这是我的代码 RegisterViewModel <Required> <EmailAddres
RegisterViewModel
<Required>
<EmailAddress>
<Display(Name:="Email")>
Public Property Email As String
<Required>
<Display(Name:="User Name")>
Public Property UserName As String
<Required>
<StringLength(100, ErrorMessage:="The {0} must be at least {2} characters long.", MinimumLength:=6)>
<DataType(DataType.Password)>
<Display(Name:="Password")>
Public Property Password As String
<DataType(DataType.Password)>
<Display(Name:="Confirm password")>
<Compare("Password", ErrorMessage:="The password and confirmation password do not match.")>
Public Property ConfirmPassword As String
LoginViewModel
<Required>
<Display(Name:="Username or Email")>
Public Property NameEmail As String
<Required>
<DataType(DataType.Password)>
<Display(Name:="Password")>
Public Property Password As String
<Display(Name:="Remember me?")>
Public Property RememberMe As Boolean
我的AccountController,登录Post方法
现在我有我的用户注册和确认他们的电子邮件和登录电子邮件或用户名
If ModelState.IsValid Then
Dim user = New ApplicationUser() With {
.UserName = model.UserName,
.Email = model.Email
}
If Not ModelState.IsValid Then
Return View(model)
End If
'Require the user to have a confirmed email before they can log on.
Dim user = Await UserManager.FindByNameAsync(model.NameEmail)
'If the user did not use his username search with his Email
If user Is Nothing Then
user = Await UserManager.FindByEmailAsync(model.NameEmail)
model.NameEmail = user.UserName
End If
If Not user Is Nothing Then
If Not Await UserManager.IsEmailConfirmedAsync(user.Id) Then
ViewBag.errorMessage = "You must have a confirmed email to log on."
Return View("Error")
End If
End If
' This doesn't count login failures towards account lockout
' To enable password failures to trigger account lockout, change to shouldLockout := True
Dim result = Await SignInManager.PasswordSignInAsync(model.NameEmail, model.Password, model.RememberMe, shouldLockout:=False)
Select Case result
Case SignInStatus.Success
Return RedirectToLocal(returnUrl)
Case SignInStatus.LockedOut
Return View("Lockout")
Case SignInStatus.RequiresVerification
Return RedirectToAction("SendCode", New With {
.ReturnUrl = returnUrl,
.RememberMe = model.RememberMe
})
Case Else
ModelState.AddModelError("", "Invalid login attempt.")
Return View(model)
End Select