SqlMembershipProvider.GetUser 方法

定义

从数据源获取成员资格用户的信息。

重载

GetUser(String, Boolean)

从 SQL Server 成员资格数据库返回用户的信息,并提供更新用户的上次活动日期/时间戳的可选功能。

GetUser(Object, Boolean)

从数据源获得与指定的唯一标识符关联的成员资格用户的信息,并更新该用户的上次活动日期/时间戳(如果指定)。

GetUser(String, Boolean)

从 SQL Server 成员资格数据库返回用户的信息,并提供更新用户的上次活动日期/时间戳的可选功能。

public:
 override System::Web::Security::MembershipUser ^ GetUser(System::String ^ username, bool userIsOnline);
public override System.Web.Security.MembershipUser GetUser (string username, bool userIsOnline);
override this.GetUser : string * bool -> System.Web.Security.MembershipUser
Public Overrides Function GetUser (username As String, userIsOnline As Boolean) As MembershipUser

参数

username
String

要为其获取信息的用户的名称。

userIsOnline
Boolean

设置为 true 则更新用户的上次活动日期/时间戳;设置为 false 则返回用户信息而不更新用户的上次活动日期/时间戳。

返回

一个 MembershipUser 对象,它表示指定的用户。 如果在数据库中没有找到指定的 username 值所对应的用户,则返回 null

例外

username 的长度超过 256 个字符。

- 或 -

username 包含一个逗号。

usernamenull

示例

下面的代码示例使用 GetUser 方法在检索用户的密码之前确定用户是否存在。

注意

此示例使用 Membership 类调用 SqlMembershipProvider 指定为 defaultProvider Web.config 文件中的 。 如果需要访问类型 为 SqlMembershipProvider的默认提供程序,则可以强制转换 Provider 类的 Membership 属性。 若要以特定提供程序类型的形式访问其他已配置的提供程序,可以使用 类的 Membership 属性按配置的名称Providers对其进行访问,并将其强制转换为特定的提供程序类型。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Net.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 enter a user name.";
  }
  else
  {
    VerifyUsername();
  }
}


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

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

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


public void EmailPassword_OnClick(object sender, EventArgs args)
{
  // Note: Returning a password in clear text using email is not recommended for
  // sites that require a high level of security.

  try
  {
    string password = Membership.Provider.GetPassword(UsernameTextBox.Text, AnswerTextBox.Text);
    MembershipUser u = Membership.GetUser(UsernameTextBox.Text);
    EmailPassword(u.Email, password);
    Msg.Text = "Your password was sent via email.";
  }
  catch (MembershipPasswordException e)
  {
    Msg.Text = "The password answer is incorrect. Please check the value and try again.";
  }
  catch (System.Configuration.Provider.ProviderException e)
  {
    Msg.Text = "An error occurred retrieving your password. Please check your values " +
               "and try again.";
  }
}


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

    SmtpClient SmtpMail = new SmtpClient("SMTPSERVER");
    SmtpMail.Send(Message);
  }
  catch 
  {
    Msg.Text = "An exception occurred while 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.Net.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(ByVal sender As Object, ByVal args As EventArgs)

    If Not Membership.EnablePasswordRetrieval Then
      FormsAuthentication.RedirectToLoginPage()
    End If

    Msg.Text = ""

    If Not IsPostBack Then
      Msg.Text = "Please enter a user name."
    Else
      VerifyUsername()
    End If

  End Sub


  Private Sub VerifyUsername()

    Dim user As MembershipUser = Membership.GetUser(UsernameTextBox.Text, False)

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

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

  End Sub


  Public Sub EmailPassword_OnClick(ByVal sender As Object, ByVal args As EventArgs)

    ' Note: Returning a password in clear text using email is not recommended for
    ' sites that require a high level of security.

    Try
      Dim password As String = Membership.Provider.GetPassword(UsernameTextBox.Text, AnswerTextBox.Text)
      Dim u As MembershipUser = Membership.GetUser(UsernameTextBox.Text)
      EmailPassword(u.Email, password)
      Msg.Text = "Your password was sent via email."
    Catch e As MembershipPasswordException
      Msg.Text = "The password answer is incorrect. Please check the value and try again."
    Catch e As System.Configuration.Provider.ProviderException
      Msg.Text = "An error occurred retrieving your password. Please check your values " & _
                 "and try again."
    End Try

  End Sub


  Private Sub EmailPassword(ByVal email As String, ByVal password As String)

    Try
      Dim Message As MailMessage = New MailMessage("administrator", email)
      Message.Subject = "Your Password"
      Message.Body = "Your password is: " & Server.HtmlEncode(password)
      
      Dim SmtpMail As SmtpClient = New SmtpClient("SMTPSERVER")
      SmtpMail.Send(Message)
    Catch
      Msg.Text = "An exception occurred while 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>

注解

类调用 Membership 此方法,以从 ASP.NET 应用程序的配置文件 (Web.config) 中指定的 SQL Server 数据库中检索用户信息。

如果 userIsOnlinetrue,则用户的上次活动日期/时间戳将更新为当前日期和时间。 这反映在 LastActivityDateIsOnline 属性以及 返回 GetNumberOfUsersOnline的值中。

删除 username 参数值的前导和尾随空格。

另请参阅

适用于

GetUser(Object, Boolean)

从数据源获得与指定的唯一标识符关联的成员资格用户的信息,并更新该用户的上次活动日期/时间戳(如果指定)。

public:
 override System::Web::Security::MembershipUser ^ GetUser(System::Object ^ providerUserKey, bool userIsOnline);
public override System.Web.Security.MembershipUser GetUser (object providerUserKey, bool userIsOnline);
override this.GetUser : obj * bool -> System.Web.Security.MembershipUser
Public Overrides Function GetUser (providerUserKey As Object, userIsOnline As Boolean) As MembershipUser

参数

providerUserKey
Object

用户的唯一标识符。

userIsOnline
Boolean

如果为 true,则更新指定用户的上次活动日期/时间戳;否则为 false

返回

一个 MembershipUser 对象,它表示与指定的唯一标识符关联的用户。 如果在数据库中没有找到指定的 providerUserKey 值所对应的用户,则返回 null

例外

providerUserKeynull

providerUserKey 的类型不是 Guid

注解

GetUser 从数据源中检索用户信息,并创建一个 MembershipUser 用返回的数据填充的对象。 使用 参数指定 providerUserKey 的唯一标识符来标识用户。

另请参阅

适用于