[英]Custom Implementation ASP.NET Identity
I having a little bit of problem implementing ASP.NET identity using repository pattern. 使用存储库模式实现ASP.NET身份时,我遇到一些问题。 When I call
UserManager.FindAsync(model.UserName, model.Password)
it always return nothing 当我调用
UserManager.FindAsync(model.UserName, model.Password)
它始终不返回任何内容
Please see account controller : 请参阅帐户管理员:
' POST: /Account/Login
<HttpPost>
<AllowAnonymous>
<ValidateAntiForgeryToken>
Public Async Function Login(model As LoginViewModel, returnUrl As String) As Task(Of ActionResult)
If ModelState.IsValid Then
' Validate the password
If UserManager Is Nothing Then
UserManager = New UserManager(Of ApplicationUser, Guid)(New We.Security.UserStore(model.DomainName))
End If
model.Password = UserManager.PasswordHasher.HashPassword(model.Password)
Dim appUser = Await UserManager.FindAsync(model.UserName, model.Password)
If appUser IsNot Nothing Then
Await SignInAsync(appUser, model.RememberMe)
Return RedirectToLocal(returnUrl)
Else
ModelState.AddModelError("", "Invalid username or password.")
End If
End If
' If we got this far, something failed, redisplay form
Return View(model)
End Function
Private Function AuthenticationManager() As IAuthenticationManager
Return HttpContext.GetOwinContext().Authentication
End Function
Private Async Function SignInAsync(user As ApplicationUser, isPersistent As Boolean) As Task
AuthenticationManager.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie)
Dim identity = Await UserManager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie)
AuthenticationManager.SignIn(New AuthenticationProperties() With {.IsPersistent = isPersistent}, identity)
End Function
Private Function RedirectToLocal(returnUrl As String) As ActionResult
If Url.IsLocalUrl(returnUrl) Then
Return Redirect(returnUrl)
Else
Return RedirectToAction("Index", "Home")
End If
End Function
Please see store code: 请查看商店代码:
Public Class UserStore
Implements IUserStore(Of ApplicationUser, Guid)
Implements IUserPasswordStore(Of ApplicationUser, Guid)
Private repo As GenericRepository
Private _CompanyName As String
Public Sub New(companyName As String)
_CompanyName = companyName
repo = New GenericRepository(companyName)
End Sub
Private ReadOnly Property CompanyName As String
Get
Return _CompanyName
End Get
End Property
Public Function CreateAsync(user As ApplicationUser) As Task Implements IUserStore(Of ApplicationUser, Guid).CreateAsync
Throw New NotImplementedException
End Function
Public Function DeleteAsync(user As ApplicationUser) As Task Implements IUserStore(Of ApplicationUser, Guid).DeleteAsync
Throw New NotImplementedException
End Function
Public Function FindByIdAsync(userId As Guid) As Task(Of ApplicationUser) Implements IUserStore(Of ApplicationUser, Guid).FindByIdAsync
Dim user As User = repo.GetSingleOrDefault(Of User)(userId)
Dim appUser As ApplicationUser = Nothing
If user IsNot Nothing Then
appUser = New ApplicationUser(user.Id) With {.UserName = user.Username, .PasswordHash = user.PasswordHash}
End If
Return Task(Of ApplicationUser).FromResult(appUser)
End Function
Public Function FindByNameAsync(userName As String) As Task(Of ApplicationUser) Implements IUserStore(Of ApplicationUser, Guid).FindByNameAsync
Dim user As User = repo.GetSingleOrDefault(Of User)(New Core.Specification.Specification(Of User)(Function(x) x.Username = userName))
Dim appUser As ApplicationUser = Nothing
If user IsNot Nothing Then
appUser = New ApplicationUser(user.Id) With {.UserName = user.Username, .PasswordHash = user.PasswordHash}
End If
Return Task(Of ApplicationUser).FromResult(appUser)
End Function
Public Function UpdateAsync(user As ApplicationUser) As Task Implements IUserStore(Of ApplicationUser, Guid).UpdateAsync
Throw New NotImplementedException
End Function
Public Function GetPasswordHashAsync(user As ApplicationUser) As Task(Of String) Implements IUserPasswordStore(Of ApplicationUser, Guid).GetPasswordHashAsync
Dim paswordHash = repo.GetDetail(Of User)(user.Id).PasswordHash
Return Task(Of String).FromResult(paswordHash)
End Function
Public Function HasPasswordAsync(user As ApplicationUser) As Task(Of Boolean) Implements IUserPasswordStore(Of ApplicationUser, Guid).HasPasswordAsync
Dim paswordHash = repo.GetDetail(Of User)(user.Id).PasswordHash
Dim hasPassword As Boolean = Not String.IsNullOrEmpty(paswordHash)
Return Task(Of Boolean).FromResult(hasPassword)
End Function
Public Function SetPasswordHashAsync(user As ApplicationUser, passwordHash As String) As Task Implements IUserPasswordStore(Of ApplicationUser, Guid).SetPasswordHashAsync
user.PasswordHash = passwordHash
Return Task.FromResult(Of Object)(Nothing)
End Function
End Class
我在登录后方法执行此模型期间发现了这个问题。Password= UserManager.PasswordHasher.HashPassword(model.Password)我删除了这一行,它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.