自定义实现ASP.NET标识

时间:2014-04-13 04:13:18

标签: asp.net asp.net-mvc vb.net

我在使用存储库模式实现ASP.NET身份时遇到了一些问题。当我致电UserManager.FindAsync(model.UserName, model.Password)时,它总是不返回任何内容

请参阅帐户管理员:

' 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

请参阅商店代码:

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

1 个答案:

答案 0 :(得分:0)

我发现了这个问题,在登录post方法中我正在做这个模型。密码= UserManager.PasswordHasher.HashPassword(model.Password)我删除了这一行,它运行正常。