AccessMembershipProvider and ChangePassword event

Last post 07-04-2008 8:42 PM by ardilo. 11 replies.

Sort Posts:

  • AccessMembershipProvider and ChangePassword event

    06-06-2008, 12:53 PM
    • Loading...
    • ardilo
    • Joined on 12-29-2007, 5:50 AM
    • Posts 37

    Dear friends,

    Since my hosting account does not include SQL Server I use AccessMembershipProvider instead. But I can not use the PasswordChange control now. I put

    AccessMembershipProvider.vb in App_Code file and related part of the code is below:

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

    Return Membership.Provider.ChangePassword(username, oldPassword, newPassword) = True

    End Function

    When I enter the neccessary information I receive an error message saying that

    "Password incorrect or New Password invalid. New Password length minimum: {0}. Non-alphanumeric characters required: {1}."

    Thanks in advance for your support.

    Regards,

  • Re: AccessMembershipProvider and ChangePassword event

    06-06-2008, 1:57 PM

    How did you configure the membership provider in the web.config?  Can you post that? 

  • Re: AccessMembershipProvider and ChangePassword event

    06-06-2008, 5:55 PM
    • Loading...
    • ardilo
    • Joined on 12-29-2007, 5:50 AM
    • Posts 37

    This is the code of the web.config file:

    <membership defaultProvider="AccessMembershipProvider" >
          <providers>
            <add name="AccessMembershipProvider"
         type="AccessMembershipProvider"
         requiresQuestionAndAnswer="true"
              connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|xxx.mdb;Jet OLEDB:Database Password=xxxxxxx;Persist Security Info=False"
             
            enablePasswordRetrieval="true"
                    enablePasswordReset="false"
                   
                    applicationName="/"
                    requiresUniqueEmail="true"
                    minRequiredPasswordLength="1"
                    minRequiredNonalphanumericCharacters="0"
                    passwordFormat="Encrypted"
                    maxInvalidPasswordAttempts="5"
                    passwordAttemptWindow="10"
                    passwordStrengthRegularExpression=""
                   
         />
          </providers>
        </membership>
        <authentication mode="Forms" />

     

    And this is the App_Code:

     

    Imports Microsoft.VisualBasic

    Imports System.Data

    Public Class AccessMembershipProvider

    Inherits MembershipProvider

    '---for database access use---

    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 Property ApplicationName() As String

    Get

    End Get

    Set(ByVal value As String)

    End Set

    End Property

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

    Return Membership.Provider.ChangePassword(username, oldPassword, newPassword) = True

    End Function

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

    End Function

    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)

    '----perform checking all the relevant checks here

    ' and set the status of the error accordingly, e.g.:

    'status = MembershipCreateStatus.InvalidPassword

    'status = MembershipCreateStatus.InvalidAnswer

    'status = MembershipCreateStatus.InvalidEmail

    '---add the user to the database

    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

    '---failed; determine the reason why

    status = MembershipCreateStatus.UserRejected

    Return Nothing

    End Try

    End Function

    Public Overrides Function DeleteUser(ByVal username As String, ByVal 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(ByVal emailToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection

    End Function

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

    End Function

    Public Overrides Function GetAllUsers(ByVal pageIndex As Integer, ByVal 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(ByVal username As String, ByVal answer As String) As String

    End Function

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

    End Function

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

    End Function

    Public Overrides Function GetUserNameByEmail(ByVal 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 PasswordStrengthRegularBLOCKED EXPRESSION As String

    Get

    End Get

    End Property

    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 ReadOnly Property RequiresUniqueEmail() As Boolean

    Get

    End Get

    End Property

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

    End Function

    Public Overrides Function UnlockUser(ByVal userName As String) As Boolean

    End Function

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

    End Sub

    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

    End Class

  • Re: AccessMembershipProvider and ChangePassword event

    06-06-2008, 5:55 PM
    • Loading...
    • ardilo
    • Joined on 12-29-2007, 5:50 AM
    • Posts 37

    This is the code of the web.config file:

    <membership defaultProvider="AccessMembershipProvider" >
          <providers>
            <add name="AccessMembershipProvider"
         type="AccessMembershipProvider"
         requiresQuestionAndAnswer="true"
              connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|xxx.mdb;Jet OLEDB:Database Password=xxxxxxx;Persist Security Info=False"
             
            enablePasswordRetrieval="true"
                    enablePasswordReset="false"
                   
                    applicationName="/"
                    requiresUniqueEmail="true"
                    minRequiredPasswordLength="1"
                    minRequiredNonalphanumericCharacters="0"
                    passwordFormat="Encrypted"
                    maxInvalidPasswordAttempts="5"
                    passwordAttemptWindow="10"
                    passwordStrengthRegularExpression=""
                   
         />
          </providers>
        </membership>
        <authentication mode="Forms" />

     

    And this is the App_Code:

     

    Imports Microsoft.VisualBasic

    Imports System.Data

    Public Class AccessMembershipProvider Inherits MembershipProvider

    '---for database access use---

    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 Property ApplicationName() As String

    Get

    End Get

    Set(ByVal value As String)

    End Set

    End Property

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

    Return Membership.Provider.ChangePassword(username, oldPassword, newPassword) = True

    End Function

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

    End Function

    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)

    '----perform checking all the relevant checks here

    ' and set the status of the error accordingly, e.g.:

    'status = MembershipCreateStatus.InvalidPassword

    'status = MembershipCreateStatus.InvalidAnswer

    'status = MembershipCreateStatus.InvalidEmail

    '---add the user to the database

    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

    '---failed; determine the reason why

    status = MembershipCreateStatus.UserRejected

    Return Nothing

    End Try

    End Function

    Public Overrides Function DeleteUser(ByVal username As String, ByVal 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(ByVal emailToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection

    End Function

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

    End Function

    Public Overrides Function GetAllUsers(ByVal pageIndex As Integer, ByVal 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(ByVal username As String, ByVal answer As String) As String

    End Function

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

    End Function

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

    End Function

    Public Overrides Function GetUserNameByEmail(ByVal 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 PasswordStrengthRegularBLOCKED EXPRESSION As String

    Get

    End Get

    End Property

    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 ReadOnly Property RequiresUniqueEmail() As Boolean

    Get

    End Get

    End Property

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

    End Function

    Public Overrides Function UnlockUser(ByVal userName As String) As Boolean

    End Function

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

    End Sub

    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

    End Class

  • Re: AccessMembershipProvider and ChangePassword event

    06-06-2008, 5:55 PM