Proveedor de membresía personalizada VB ASP .NET MVC
Frecuentes
Visto 1,924 veces
0
All of the examples I've found are in C# and I need one if VB. How can I turn my code below to inherit all of the Membership provider functions?
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
Here are the articles that I've read Custom membership or not, Implementación de inicio de sesión personalizado para ASP.NET MVC. I can't seem to inherit more than one class in VB (don't often use inheritance or implement or interfaces).
2 Respuestas
4
You need to write a class that inherits from Proveedor de membresía and override the methods you are interested in:
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
And then you register your custom provider in web.config:
<membership defaultProvider="MyMembership">
<providers>
<clear />
<add
name="MyMembership"
type="MvcApplication1.MyCustomMembershipProvider, MvcApplication1" enablePasswordRetrieval="false"
/>
</providers>
</membership>
Now from within your controllers you simply use the Membership
class. For example in your LogOn
action that was generated by the default template when you created your project you don't need to change absolutely anything:
<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
Todas las llamadas a Membership
will now use your custom membership provider that you registered in web.config.
Respondido 28 ago 12, 14:08
In MVC, where should the class go? Then, how do I use the class back in my controller to perform actions such as LogOn (just like the "stock" membership provider)? - user1477388
You could place this class in a sub-folder of your project. For example Providers
. From your controller you simply use for example Membership.ValidateUser("username", "password")
. I have updated my answer to illustrate. - darin dimitrov
Thanks, I hope this won't be too difficult. Is the ApplicationName necessary to override or can I leave that out? - user1477388
You can leave that out. But throw a NotImplementedException
from all methods that you intend leaving out. It will be easier to debug problems later. Unfortunately Visual Studio didn't generate them for me when I implemented the abstract class, which is something it automatically does with C#. I don't know why it didn't do it with VB.NET but make sure you do it and don't leave those methods blank. - darin dimitrov
1
I got an easier solution. Use nuget to install griffin.mvccontrib
. Then create a new class like this:
public class MyAccountRepository implements IAccountRepository
end class
Prensa CTRL+.
on the interface to import the correct namespace. The press CTRL+.
on the class name to get all methods with their descriptions.
Then simply implement them using your EmployeeDBContext
.
by doing so, you can leave everything else as is (using the standard internet MVC template)
Instrucción: http://blog.gauffin.org/2011/09/a-more-structured-membershipprovider/
Respondido 28 ago 12, 14:08
Thank you, jgauffin. As a contractor, I am handing this system off when I'm done, so I would rather make it as simple as possible. This looks like a good solution, but I think the other solution by Darin will be easier to understand for future administrators of my code. - user1477388
Implementing a membership provider properly is not trivial. Good luck. - jgauffin
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net-mvc vb.net asp.net-mvc-3 asp.net-membership or haz tu propia pregunta.
Classes are not interfaces and .NET do not support multi-inheritance - jgauffin
Why would you want your controller to inherit the Membership class? Normally you'd just call the static methods such as "Membership.ValidateUser(...)" from within your controller methods. - Andrew Stephens
That is what I am gathering. How can I create a membership provider? Must I omit the system.web.mvc.controller? Or should I create a membership provider in a separate file and then import it on my controller? This is more of a question of design. - user1477388
It's not clear what you are asking and what problem you have encountered. You don't know how to write a class that derives from
MembershipProvider
and override its methods? You have problems with VB.NET syntax? - Darin Dimitrov