2011年9月30日 星期五

使用.NET的會員系統建立使用者忘記密碼

Partial Public Class AddUser
    Inherits System.Web.UI.Page

#Region "Event Handles"
       Protected Sub btnCreatedUser_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreatedUser.Click
        Dim CreateStatus As MembershipCreateStatus
        Dim qr As String = UserName.Text & "1234"

        Membership.CreateUser(UserName.Text, Password.Text, Email.Text, qr, qr, cbCheckNewUser.Checked, CreateStatus)

        Select Case CreateStatus
            Case MembershipCreateStatus.Success
                Dim pf As MyWebProfile = MyWebProfile.GetProfile(UserName.Text)
                pf.SetPropertyValue("Name", txtName.Text)

                pf.Save()
                If cbSeandEmail.Checked Then
                    Dim sb As New StringBuilder
                    sb.Append("已成功加入「資訊管理整合系統」以下是您的系統登入資訊:")
                    sb.Append("使用者名稱:" & UserName.Text & "")
                    sb.Append("密碼:" & Password.Text & "")
                    sb.Append("電子郵件:" & Email.Text & "")
                    sb.Append("註冊日期:" & Date.Now & "")
                    sb.Append("登入網址:http://" & Request.Url.Host & "")
                    objTPC.SendMailList(Email.Text, sb.ToString)
                End If

                Response.Redirect("~/Admin/Users/UserMain.aspx")
            Case MembershipCreateStatus.DuplicateUserName
                objTPC.ShowMsg("此使用者名稱已有註冊!", Me.Page)
            Case MembershipCreateStatus.DuplicateEmail
                objTPC.ShowMsg("此電子郵件已有註冊!", Me.Page)
            Case Else

        End Select
    End Sub
#End Region


End Class




然後在忘記密碼頁 按Button1重設密碼,寄信通知給使用者,不過這頁要做權限控制
Public Partial Class PasswordRecovery
    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Title = "忘記密碼"
        If Not IsPostBack Then
            If Request("un") IsNot Nothing Then
                Dim user As MembershipUser = Membership.GetUser(Request("un"))
                user.UnlockUser()
                Dim oldpass As String = user.PasswordQuestion
                Dim newpass As String = Request("un") & "1234"

                labShowEmail.Text = user.Email
                labShowNewPass.Text = newpass
                labUaerName.Text = user.UserName
            End If
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim user As MembershipUser = Membership.GetUser(Request("un"))
        user.UnlockUser()
        Dim oldpass As String = user.PasswordQuestion
        Dim newpass As String = Request("un") & "1234"
        ChangeUserPassword(user, oldpass, newpass)
        Response.Redirect("~/Admin/Users/UserMain.aspx")
    End Sub
    Private Sub ChangeUserPassword(ByVal u As MembershipUser, ByVal oldstr As String, ByVal newstr As String)
        If u.ChangePassword(u.ResetPassword(oldstr), newstr) Then
            If cbSeandEmail.Checked Then
                Dim sb As New StringBuilder
                sb.Append("已成功加入「系統」以下是您的系統登入資訊:")
                sb.Append("使用者名稱:" & u.UserName & "")
                sb.Append("新密碼:" & newstr & "")
                sb.Append("登入網址:" & Request.Url.Host & " ")
                objtpc.SendMailList(u.Email, sb.ToString, " - 密碼重設")
                sb.Length = 0
            End If
        Else
            objtpc.ShowMsg("Change password error !", Me.Page)
        End If
    End Sub
End Class


如果有用ASP.NET會員系統的Profile功能時要在Web.config加入下列,在這會員加入時有用到Profile的Name




原始文章來自於此







沒有留言:

張貼留言