MembershipUser.GetPassword 方法

定义

从成员资格数据存储中获取成员资格用户的密码。

重载

GetPassword()

从成员资格数据存储中获取成员资格用户的密码。

GetPassword(String)

从成员资格数据存储中获取成员资格用户的密码。

GetPassword()

从成员资格数据存储中获取成员资格用户的密码。

public:
 virtual System::String ^ GetPassword();
public virtual string GetPassword ();
abstract member GetPassword : unit -> string
override this.GetPassword : unit -> string
Public Overridable Function GetPassword () As String

返回

成员资格用户的密码。

例外

此方法不可用。 如果应用程序面向 .NET Framework 4 客户端配置文件,则可能会发生这种情况。 若要避免此异常,请重写该方法,或将应用程序更改为面向 .NET Framework 的完整版本。

示例

下面的代码示例调用 GetPassword 方法来检索指定用户名的密码。 密码将发送到用户的电子邮件地址。 请注意, RequiresQuestionAndAnswer 假定为 false

注意

对于需要高级别安全性的网站,不建议使用电子邮件以明文形式返回密码。 对于高安全性站点,建议使用加密(如 SSL)返回密码。

重要

此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Page_Load(object sender, EventArgs args)
{
  if (!Membership.EnablePasswordRetrieval)
  {
    FormsAuthentication.RedirectToLoginPage();
  }

  Msg.Text = "";

  if (!IsPostBack)
  {
    Msg.Text = "Please supply a username.";
  }
  else
  {
    VerifyUsername();
  }
}


public void VerifyUsername()
{
    MembershipUser u = Membership.GetUser(UsernameTextBox.Text, false);

    if (u == null)
    {
      Msg.Text = "Username " + Server.HtmlEncode(UsernameTextBox.Text) + " not found. Please check the value and re-enter.";

      EmailPasswordButton.Enabled = false;
    }
    else
    {
      EmailPasswordButton.Enabled = true;
    }
}


public void EmailPassword_OnClick(object sender, EventArgs args)
{
  MembershipUser u = Membership.GetUser(UsernameTextBox.Text, false);
  string password;

  if (u != null)
  {
    try
    {
      password = u.GetPassword();
    }
    catch (Exception e)
    {
      Msg.Text = "An exception occurred retrieving your password: " + Server.HtmlEncode(e.Message);
      return;
    }

    EmailPassword(u.Email, password);
    Msg.Text = "Password sent via email.";
  }
  else
  {
    Msg.Text = "User name is not valid. Please check the value and try again.";
  }
}


private void EmailPassword(string email, string password)
{
  try
  {
    MailMessage Message = new MailMessage();
    Message.To = email;
    Message.From = "administrator";
    Message.Subject = "Your Password";
    Message.Body = "Your password is: " + Server.HtmlEncode(password);

    SmtpMail.SmtpServer = "smarthost";
    SmtpMail.Send(Message);
  }
  catch 
  {
    Msg.Text = "An exception occurred sending your password. Please try again.";
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Retrieve Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Retrieve Password</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />

  Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" />
            <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
                                        ControlToValidate="UsernameTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  <asp:Button id="EmailPasswordButton" Text="Email My Password" 
              OnClick="EmailPassword_OnClick" runat="server" Enabled="false" />

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Page_Load(sender As Object, args As EventArgs)
  If Not Membership.EnablePasswordRetrieval Then
    FormsAuthentication.RedirectToLoginPage()
  End If

  Msg.Text = ""

  If Not IsPostBack Then
    Msg.Text = "Please supply a username."
  Else
    VerifyUsername()
  End If
End Sub


Public Sub VerifyUsername()
    Dim u As MembershipUser = Membership.GetUser(UsernameTextBox.Text, False)

    If u Is Nothing Then
      Msg.Text = "Username " & Server.HtmlEncode(UsernameTextBox.Text) & " not found. Please check the value and re-enter."

      EmailPasswordButton.Enabled = False
    Else
      EmailPasswordButton.Enabled = True
    End If
End Sub


Public Sub EmailPassword_OnClick(sender As Object, args As EventArgs)
  Dim u As MembershipUser = Membership.GetUser(UsernameTextBox.Text, False)
  Dim password As String

  If Not u Is Nothing Then
    Try
      password = u.GetPassword()
      Catch e As Exception
        Msg.Text = "An exception occurred retrieving your password: " & Server.HtmlEncode(e.Message)
        Return
    End Try

    EmailPassword(u.Email, password)
    Msg.Text = "Password sent via email."
  Else
    Msg.Text = "Password Answer is not valid. Please check the value and try again."
  End If
End Sub


Private Sub EmailPassword(email As String, password As String)
  Try
    Dim Message As MailMessage = New MailMessage()
    Message.To = email
    Message.From = "administrator"
    Message.Subject = "Your Password"
    Message.Body = "Your password is: " & Server.HtmlEncode(password)

    SmtpMail.SmtpServer = "smarthost"
    SmtpMail.Send(Message)
  Catch 
    Msg.Text = "An exception occurred sending your password. Please try again."
  End Try
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Retrieve Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Retrieve Password</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />

  Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" />
            <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
                                        ControlToValidate="UsernameTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  <asp:Button id="EmailPasswordButton" Text="Email My Password" 
              OnClick="EmailPassword_OnClick" runat="server" Enabled="false" />

</form>

</body>
</html>

注解

GetPassword MembershipProvider.GetPassword调用 由 ProviderName 属性引用的成员资格提供程序的 方法,以从成员资格数据存储中检索成员身份的密码。

如果 EnablePasswordRetrievalfalse,则成员资格提供程序将返回异常。 如果提供程序支持的密码为 PasswordFormatHashed则你将无法检索成员身份用户的密码,并且当用户忘记密码时,应考虑使用 ResetPassword 方法。

注意

ConfigurationException如果在 enablePasswordRetrieval ASP.NET 应用程序的 Web.config 文件中将 设置为 truepasswordFormat 设置为 Hashed ,则会引发 。

如果 RequiresQuestionAndAnswertrue,则必须使用 GetPassword 采用密码答案作为参数的 重载,并为成员身份用户提供密码答案。 如果需要密码答案,并且提供了不正确的密码答案, MembershipPasswordException 则成员资格提供程序会引发 。

另请参阅

适用于

GetPassword(String)

从成员资格数据存储中获取成员资格用户的密码。

public:
 virtual System::String ^ GetPassword(System::String ^ passwordAnswer);
public virtual string GetPassword (string passwordAnswer);
abstract member GetPassword : string -> string
override this.GetPassword : string -> string
Public Overridable Function GetPassword (passwordAnswer As String) As String

参数

passwordAnswer
String

成员资格用户的密码答案。

返回

成员资格用户的密码。

例外

此方法不可用。 如果应用程序面向 .NET Framework 4 客户端配置文件,则可能会发生这种情况。 若要避免此异常,请重写该方法,或将应用程序更改为面向 .NET Framework 的完整版本。

示例

下面的代码示例调用 GetPassword 方法以检索指定用户名的密码(如果提供了正确的密码答案)。 密码将发送到用户的电子邮件地址。

注意

对于需要高级别安全性的网站,不建议使用电子邮件以明文形式返回密码。 对于高安全性站点,建议使用加密(如 SSL)返回密码。

重要

此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Page_Load(object sender, EventArgs args)
{
  if (!Membership.EnablePasswordRetrieval)
  {
    FormsAuthentication.RedirectToLoginPage();
  }

  Msg.Text = "";

  if (!IsPostBack)
  {
    Msg.Text = "Please supply a username.";
  }
  else
  {
    VerifyUsername();
  }
}


public void VerifyUsername()
{
    MembershipUser u = Membership.GetUser(UsernameTextBox.Text, false);

    if (u == null)
    {
      Msg.Text = "Username " + Server.HtmlEncode(UsernameTextBox.Text) + " not found. Please check the value and re-enter.";

      QuestionLabel.Text = "";
      QuestionLabel.Enabled = false;
      AnswerTextBox.Enabled = false;
      EmailPasswordButton.Enabled = false;
    }
    else
    {
      QuestionLabel.Text = u.PasswordQuestion;
      QuestionLabel.Enabled = true;
      AnswerTextBox.Enabled = true;
      EmailPasswordButton.Enabled = true;
    }
}


public void EmailPassword_OnClick(object sender, EventArgs args)
{
  MembershipUser u = Membership.GetUser(UsernameTextBox.Text, false);
  string password;

  if (u != null)
  {
    try
    {
      password = u.GetPassword(AnswerTextBox.Text);
    }
    catch (Exception e)
    {
      Msg.Text = "An exception occurred retrieving your password: " + Server.HtmlEncode(e.Message);
      return;
    }

    EmailPassword(u.Email, password);
    Msg.Text = "Password sent via email.";
  }
  else
  {
    Msg.Text = "Password Answer is not valid. Please check the value and try again.";
  }
}


private void EmailPassword(string email, string password)
{
  try
  {
    MailMessage Message = new MailMessage();
    Message.To = email;
    Message.From = "administrator";
    Message.Subject = "Your Password";
    Message.Body = "Your password is: " + Server.HtmlEncode(password);

    SmtpMail.SmtpServer = "smarthost";
    SmtpMail.Send(Message);
  }
  catch 
  {
    Msg.Text = "An exception occurred sending your password. Please try again.";
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Retrieve Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Retrieve Password</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />

  Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" />
            <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
                                        ControlToValidate="UsernameTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  Password Question: <b><asp:Label id="QuestionLabel" runat="server" /></b><br />

  Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="false" />
          <asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
                                      ControlToValidate="AnswerTextBox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" Enabled="false" /><br />

  <asp:Button id="EmailPasswordButton" Text="Email My Password" 
              OnClick="EmailPassword_OnClick" runat="server" Enabled="false" />

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Page_Load(sender As Object, args As EventArgs)
  If Not Membership.EnablePasswordRetrieval Then
    FormsAuthentication.RedirectToLoginPage()
  End If

  Msg.Text = ""

  If Not IsPostBack Then
    Msg.Text = "Please supply a username."
  Else
    VerifyUsername()
  End If
End Sub


Public Sub VerifyUsername()
    Dim u As MembershipUser = Membership.GetUser(UsernameTextBox.Text, False)

    If u Is Nothing Then
      Msg.Text = "Username " & Server.HtmlEncode(UsernameTextBox.Text) & " not found. Please check the value and re-enter."

      QuestionLabel.Text = ""
      QuestionLabel.Enabled = False
      AnswerTextBox.Enabled = False
      EmailPasswordButton.Enabled = False
    Else
      QuestionLabel.Text = u.PasswordQuestion
      QuestionLabel.Enabled = True
      AnswerTextBox.Enabled = True
      EmailPasswordButton.Enabled = True
    End If
End Sub


Public Sub EmailPassword_OnClick(sender As Object, args As EventArgs)
  Dim u As MembershipUser = Membership.GetUser(UsernameTextBox.Text, False)
  Dim password As String

  If Not u Is Nothing Then
    Try
        password = u.GetPassword(AnswerTextBox.Text)
      Catch e As Exception
        Msg.Text = "An exception occurred retrieving your password: " & Server.HtmlEncode(e.Message)
        Return
    End Try

    EmailPassword(u.Email, password)
    Msg.Text = "Password sent via email."
  Else
    Msg.Text = "Password Answer is not valid. Please check the value and try again."
  End If
End Sub


Private Sub EmailPassword(email As String, password As String)
  Try
    Dim Message As MailMessage = New MailMessage()
    Message.To = email
    Message.From = "administrator"
    Message.Subject = "Your Password"
    Message.Body = "Your password is: " & Server.HtmlEncode(password)

    SmtpMail.SmtpServer = "smarthost"
    SmtpMail.Send(Message)
  Catch 
    Msg.Text = "An exception occurred sending your password. Please try again."
  End Try
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Retrieve Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Retrieve Password</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />

  Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" />
            <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
                                        ControlToValidate="UsernameTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  Password Question: <b><asp:Label id="QuestionLabel" runat="server" /></b><br />

  Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="false" />
          <asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
                                      ControlToValidate="AnswerTextBox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" Enabled="false" /><br />

  <asp:Button id="EmailPasswordButton" Text="Email My Password" 
              OnClick="EmailPassword_OnClick" runat="server" Enabled="false" />

</form>

</body>
</html>

注解

GetPassword MembershipProvider.GetPassword调用 由 ProviderName 属性引用的成员资格提供程序的 方法,以从成员资格数据存储检索成员资格用户的密码。 如果需要密码答案,并且提供了不正确的密码答案, MembershipPasswordException 则成员资格提供程序会引发 。

如果 EnablePasswordRetrievalfalse,则成员资格提供程序将返回异常。 如果提供程序支持的密码为 PasswordFormatHashed则你将无法检索成员身份用户的密码,并且当用户忘记密码时,应考虑使用 ResetPassword 方法。

注意

ConfigurationException如果在 enablePasswordRetrieval ASP.NET 应用程序的 Web.config 文件中将 设置为 truepasswordFormat 设置为 Hashed ,则会引发 。

如果 RequiresQuestionAndAnswerfalse,则可以为 answer 参数提供 null ,或使用GetPassword不采用任何参数的重载。

另请参阅

适用于