在asp.net 2.0中,新增加的membership provider功能,以及结合功能强大的一系列注册,登陆控件,可以很方便地对用户的登陆和权限等进行管理(参见
<<asp.net 2.0登陆控件简介>>一文(http://www.yesky.com/466/1870966.shtml)。<!----><o:p></o:p>
但是,可能大家会发现,asp.net 2.0自带的这些登陆控件和membership的管理功能,默认的是和sql server 2005 express搭配使用的,那么,如何改成使用sql server 2000或者是其他的数据源,如access,oracle等呢?如果自己想在应用中,另外重新写一个对登陆用户或者用户权限等管理的应用,要如何修改呢?在本文中,将给出在asp.net 2.0中使用自定义provider,以配合登陆控件来实现一个简单的登陆过程。<o:p></o:p>
为了明白asp.net 2.0中的provider是如何工作的,首先看下下面的结构图:Public Class ModifiedSqlMembershipProvider Inherits SqlMembershipProvider Public Overrides Function CreateUser (...) ... End Function ... End Class |
Imports Microsoft.VisualBasic Imports System.Data Public Class AccessMembershipProvider Inherits MembershipProvider End Class |
<system.web> <authentication mode="Forms"/> <membership defaultProvider="AccessMembershipProvider" > <providers> <add name="AccessMembershipProvider" type="AccessMembershipProvider" requiresQuestionAndAnswer="true" connectionString="Provider=Microsoft.Jet. OLEDB.4.0;Data Source=C:\NewMembershipProvider\ App_Data\Members.mdb;Persist Security Info=False" /> </providers> </membership> </system.web> |
Private connStr As String Private comm As New OleDb.OleDbCommand Private _requiresQuestionAndAnswer As Boolean Private _minRequiredPasswordLength As Integer |
Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection) ’===retrives the attribute values set in ’web.config and assign to local variables=== If config("requiresQuestionAndAnswer") = "true" Then _ _requiresQuestionAndAnswer = True connStr = config("connectionString") MyBase.Initialize(name, config) End Sub |
Public Overrides ReadOnly Property _ RequiresQuestionAndAnswer() _ As Boolean Get If _requiresQuestionAndAnswer = True Then Return True Else Return False End If End Get End Property |
Public Overrides Function CreateUser(ByVal username As String, ByVal password As String, ByVal email As String, ByVal passwordQuestion As String, ByVal passwordAnswer As String, ByVal isApproved As Boolean, ByVal providerUserKey As Object, ByRef status As System.Web.Security.MembershipCreateStatus) As System.Web.Security.MembershipUser Dim conn As New OleDb.OleDbConnection(connStr) Try conn.Open() Dim sql As String = "INSERT INTO Membership VALUES (" & _ "@username, @password, @email, " & _ " @passwordQuestion, @passwordAnswer )" Dim comm As New OleDb.OleDbCommand(sql, conn) comm.Parameters.AddWithValue("@username", username) comm.Parameters.AddWithValue("@password", password) comm.Parameters.AddWithValue("@email", email) comm.Parameters.AddWithValue("@passwordQuestion", passwordQuestion) comm.Parameters.AddWithValue("@passwordAnswer", passwordAnswer) Dim result As Integer = comm.ExecuteNonQuery() conn.Close() status = MembershipCreateStatus.Success Dim user As New MembershipUser("AccessMembershipProvider", username, Nothing, email, passwordQuestion, Nothing, True, False, Now, Nothing, Nothing, Nothing, Nothing) Return user Catch ex As Exception status = MembershipCreateStatus.UserRejected Return Nothing End Try End Function |
Dim user As New MembershipUser("AccessMembershipProvider", username, Nothing, email, passwordQuestion, Nothing, True, False, Now, Nothing, Nothing, Nothing, Nothing) |
Public Overrides Function ValidateUser( _ ByVal username As String, _ ByVal password As String) As Boolean Dim conn As New OleDb.OleDbConnection(connStr) Try conn.Open() Dim sql As String = _ "Select * From Membership WHERE " & _ "username=@username AND password=@password" Dim comm As New OleDb.OleDbCommand(sql, conn) comm.Parameters.AddWithValue("@username", _ username) comm.Parameters.AddWithValue("@password", _ password) Dim reader As OleDb.OleDbDataReader = _ comm.ExecuteReader If reader.HasRows Then Return True Else Return False End If conn.Close() Catch ex As Exception Console.Write(ex.ToString) Return False End Try End Function |