2012-08-28 58 views
0

我發現的所有例子都是用C#編寫的,而且如果使用VB,我需要一個例子。如何將下面的代碼轉爲繼承所有的Membership提供者函數?VB ASP .NET MVC自定義會員供應商

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 

這裏是我讀過Custom membership or notImplementing custom login for ASP.NET MVC的文章。我似乎無法繼承VB中的多個類(不經常使用繼承或實現或接口)。

+1

類不是接口,.NET不支持多繼承 – jgauffin

+1

爲什麼你想讓你的控制器繼承Membership類?通常情況下,您只需從您的控制器方法中調用靜態方法,如「Membership.ValidateUser(...)」。 –

+0

這就是我正在收集的東西。我如何創建會員供應商?我必須省略system.web.mvc.controller嗎?或者,我應該在單獨的文件中創建一個成員資格提供者,然後將其導入到我的控制器中?這更像是一個設計問題。 – user1477388

回答

4

你需要編寫從MembershipProvider繼承一個類並重寫你的方法興趣:在web.config中

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 

然後您註冊自定義提供:

現在從您的控制器中,您只需使用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中。

+0

在MVC中,班級應該去哪裏?那麼,如何在我的控制器中使用該類來執行諸如LogOn之類的操作(就像「股票」成員資格提供者一樣)? – user1477388

+1

你可以把這個類放在你項目的子文件夾中。例如'Providers'。從你的控制器,你只需使用例如'會員。ValidateUser(「用戶名」,「密碼」)'。我已經更新了我的回答來說明。 –

+0

謝謝,我希望這不會太困難。 ApplicationName是否需要覆蓋或我可以忽略它? – user1477388

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/

+0

謝謝你,jgauffin。作爲承包商,當我完成工作時,我正在交付該系統,所以我寧願儘可能簡單。這看起來是一個很好的解決方案,但我認爲Darin提出的另一個解決方案對於將來的代碼管理員來說更容易理解。 – user1477388

+1

正確實施會員供應商並不是微不足道的。祝你好運。 – jgauffin