0

私が見つけたすべての例はC#であり、VBの場合は1つ必要です。以下のコードを変更して、すべてのメンバーシッププロバイダー機能を継承するにはどうすればよいですか?

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.NETMVCのカスタムログインの実装を読んだ記事です。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です。

そうすることで、他のすべてをそのままにしておくことができます(標準のインターネットMVCテンプレートを使用)

指示: http: //blog.gauffin.org/2011/09/a-more-structured-membershipprovider/

于 2012-08-28T13:15:24.603 に答える