0

我发现的所有示例都在 C# 中,如果是 VB,我需要一个。如何将下面的代码转换为继承所有 Membership 提供程序功能?

Imports System.Data.Entity
Imports MyBlog

Namespace MyBlog

    Public Class EmployeeController
        Inherits System.Web.Mvc.Controller

        Private db As EmployeeDbContext = New EmployeeDbContext

        '
        ' GET: /Employee/LogOn

        Public Function LogOn() As ActionResult
            Return View()
        End Function

    End Class

End Namespace

这是我是否阅读过自定义成员资格的文章,为 ASP.NET MVC 实现自定义登录。我似乎不能在 VB 中继承多个类(不经常使用继承或实现或接口)。

4

2 回答 2

4

您需要编写一个继承自MembershipProvider的类并覆盖您感兴趣的方法:

Public Class MyCustomMembershipProvider
    Inherits System.Web.Security.MembershipProvider

    Public Overrides Property ApplicationName As String
        Get

        End Get
        Set(value As String)

        End Set
    End Property

    Public Overrides Function ChangePassword(username As String, oldPassword As String, newPassword As String) As Boolean

    End Function

    Public Overrides Function ChangePasswordQuestionAndAnswer(username As String, password As String, newPasswordQuestion As String, newPasswordAnswer As String) As Boolean

    End Function

    Public Overrides Function CreateUser(username As String, password As String, email As String, passwordQuestion As String, passwordAnswer As String, isApproved As Boolean, providerUserKey As Object, ByRef status As System.Web.Security.MembershipCreateStatus) As System.Web.Security.MembershipUser

    End Function

    Public Overrides Function DeleteUser(username As String, deleteAllRelatedData As Boolean) As Boolean

    End Function

    Public Overrides ReadOnly Property EnablePasswordReset As Boolean
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property EnablePasswordRetrieval As Boolean
        Get

        End Get
    End Property

    Public Overrides Function FindUsersByEmail(emailToMatch As String, pageIndex As Integer, pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection

    End Function

    Public Overrides Function FindUsersByName(usernameToMatch As String, pageIndex As Integer, pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection

    End Function

    Public Overrides Function GetAllUsers(pageIndex As Integer, pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection

    End Function

    Public Overrides Function GetNumberOfUsersOnline() As Integer

    End Function

    Public Overrides Function GetPassword(username As String, answer As String) As String

    End Function

    Public Overloads Overrides Function GetUser(providerUserKey As Object, userIsOnline As Boolean) As System.Web.Security.MembershipUser

    End Function

    Public Overloads Overrides Function GetUser(username As String, userIsOnline As Boolean) As System.Web.Security.MembershipUser

    End Function

    Public Overrides Function GetUserNameByEmail(email As String) As String

    End Function

    Public Overrides ReadOnly Property MaxInvalidPasswordAttempts As Integer
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property MinRequiredNonAlphanumericCharacters As Integer
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property MinRequiredPasswordLength As Integer
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property PasswordAttemptWindow As Integer
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property PasswordFormat As System.Web.Security.MembershipPasswordFormat
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property PasswordStrengthRegularExpression As String
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property RequiresQuestionAndAnswer As Boolean
        Get

        End Get
    End Property

    Public Overrides ReadOnly Property RequiresUniqueEmail As Boolean
        Get

        End Get
    End Property

    Public Overrides Function ResetPassword(username As String, answer As String) As String

    End Function

    Public Overrides Function UnlockUser(userName As String) As Boolean

    End Function

    Public Overrides Sub UpdateUser(user As System.Web.Security.MembershipUser)

    End Sub

    Public Overrides Function ValidateUser(username As String, password As String) As Boolean

    End Function
End Class

然后在 web.config 中注册您的自定义提供程序:

<membership defaultProvider="MyMembership">
    <providers>
        <clear />
        <add 
            name="MyMembership" 
            type="MvcApplication1.MyCustomMembershipProvider, MvcApplication1" enablePasswordRetrieval="false" 
        />
    </providers>
</membership>

现在从您的控制器中,您只需使用Membership该类。例如,在LogOn您创建项目时由默认模板生成的操作中,您绝对不需要更改任何内容:

<HttpPost()> _
Public Function LogOn(ByVal model As LogOnModel, ByVal returnUrl As String) As ActionResult
    If ModelState.IsValid Then
        If Membership.ValidateUser(model.UserName, model.Password) Then
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe)
            If Url.IsLocalUrl(returnUrl) AndAlso returnUrl.Length > 1 AndAlso returnUrl.StartsWith("/") _
               AndAlso Not returnUrl.StartsWith("//") AndAlso Not returnUrl.StartsWith("/\\") Then
                Return Redirect(returnUrl)
            Else
                Return RedirectToAction("Index", "Home")
            End If
        Else
            ModelState.AddModelError("", "The user name or password provided is incorrect.")
        End If
    End If

    ' If we got this far, something failed, redisplay form
    Return View(model)
End Function

现在,所有调用都Membership将使用您在 web.config 中注册的自定义成员资格提供程序。

于 2012-08-28T13:15:32.803 回答
1

我有一个更简单的解决方案。使用 nuget 安装griffin.mvccontrib. 然后像这样创建一个新类:

public class MyAccountRepository implements IAccountRepository
end class

按下CTRL+.界面,导入正确的命名空间。按下CTRL+.类名以获取所有方法及其描述。

然后只需使用您的EmployeeDBContext.

通过这样做,您可以保留其他所有内容(使用标准 Internet MVC 模板)

说明:http: //blog.gauffin.org/2011/09/a-more-structured-membershipprovider/

于 2012-08-28T13:15:24.603 回答