共用方式為


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 屬性。 若要以特定提供者類型存取其他已設定的提供者,您可以使用 類別 ProvidersMembership 屬性來存取這些提供者,並將其轉換成特定提供者類型。

<%@ 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 指定的唯一標識碼來識別使用者。

另請參閱

適用於