SqlMembershipProvider.UpdateUser(MembershipUser) 方法

定义

更新 SQL Server 成员资格数据库中用户的信息。Updates information about a user in the SQL Server membership database.

public:
 override void UpdateUser(System::Web::Security::MembershipUser ^ user);
public override void UpdateUser (System.Web.Security.MembershipUser user);
override this.UpdateUser : System.Web.Security.MembershipUser -> unit
Public Overrides Sub UpdateUser (user As MembershipUser)

参数

user
MembershipUser

一个 MembershipUser 对象,表示要更新的用户及其更新信息。A MembershipUser object that represents the user to update and the updated information for the user.

异常

usernulluser is null.

- 或 --or- userUserName 属性为 nullThe UserName property of user is null.

-or- userEmail 属性为 nullRequiresUniqueEmail 设置为 trueThe Email property of user is null and RequiresUniqueEmail is set to true.

userUserName 属性为空字符串 ("")、包含一个逗号或长度超过 256 个字符。The UserName property of user is an empty string (""), contains a comma, or is longer than 256 characters.

- 或 --or- userEmail 属性的长度超过 256 个字符。The Email property of user is longer than 256 characters.

-or- userEmail 属性是一个空字符串且 RequiresUniqueEmail 设置为 trueThe Email property of user is an empty string and RequiresUniqueEmail is set to true.

数据库中未找到 userUserName 属性。The UserName property of user was not found in the database.

- 或 --or- userEmail 属性等于数据库中的某个现有电子邮件地址且 RequiresUniqueEmail 设置为 true。The Email property of user was equal to an existing email address in the database and RequiresUniqueEmail is set to true.

-or- 用户更新失败。The user update failed.

示例

下面的代码示例将更新用户的电子邮件地址。The following code example updates the email address for a user.

备注

此示例使用MembershipdefaultProvider在 web.config 文件中SqlMembershipProvider调用指定的。This example uses the Membership class to call the SqlMembershipProvider specified as the defaultProvider in the Web.config file. 如果需要以类型SqlMembershipProvider访问默认提供程序,则可以Provider强制转换Membership类的属性。If you need to access the default provider as the type SqlMembershipProvider, you can cast the Provider property of the Membership class. 若要以特定的提供程序类型访问其他已配置的提供程序,可以使用Providers Membership类的属性访问这些提供程序,并将其转换为特定的提供程序类型。To access other configured providers as a specific provider type, you can access them by their configured name with the Providers property of the Membership class and cast them as the specific provider type.

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

MembershipUser u;

public void Page_Load(object sender, EventArgs args)
{
  u = Membership.GetUser(User.Identity.Name);

  if (!IsPostBack)
  {
    EmailTextBox.Text = u.Email; 
  }
}

public void UpdateEmailButton_OnClick(object sender, EventArgs args)
{
  try
  {
    u.Email = EmailTextBox.Text;

    Membership.UpdateUser(u);
  
    Msg.Text = "User email updated.";
  }
  catch (System.Configuration.Provider.ProviderException e)
  {
    Msg.Text = e.Message;
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Update User E-Mail</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Update E-Mail Address for <%=User.Identity.Name%></h3>

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Email Address:</td>
      <td><asp:TextBox id="EmailTextBox" MaxLength="128" Columns="30" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                    ControlToValidate="EmailTextBox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="UpdateEmailButton" 
                      Text="Update Email" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

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

Dim u As MembershipUser

Public Sub Page_Load(sender As Object, args As EventArgs)

  u = Membership.GetUser(User.Identity.Name)

  If Not IsPostBack Then EmailTextBox.Text = u.Email

End Sub

Public Sub UpdateEmailButton_OnClick(sender As Object, args As EventArgs)

  Try
    u.Email = EmailTextBox.Text

    Membership.UpdateUser(u)
  
    Msg.Text = "User email updated."
  Catch e As System.Configuration.Provider.ProviderException
    Msg.Text = e.Message
  End Try

End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Update User E-Mail</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Update E-Mail Address for <%=User.Identity.Name%></h3>

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Email Address:</td>
      <td><asp:TextBox id="EmailTextBox" MaxLength="128" Columns="30" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                    ControlToValidate="EmailTextBox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="UpdateEmailButton" 
                      Text="Update Email" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

注解

此方法由Membership类调用,以更新 ASP.NET 应用程序的配置文件(web.config)中指定的 SQL Server 数据库中用户的用户信息。This method is called by the Membership class to update user information for a user in the SQL Server database specified in the ASP.NET application's configuration file (Web.config). Email指定的IsApproved成员LastLoginDate资格用户LastActivityDate更新了、 Comment、、和属性值。The Email, Comment, IsApproved, LastLoginDate, and LastActivityDate property values are updated for the specified membership user.

UserName属性的最大长度为256个字符。The maximum length for the UserName property is 256 characters. Email属性的最大长度为256个字符。The maximum length for the Email property is 256 characters.

成员资格用户的密码不能使用UpdateUser方法进行更新。The password for a membership user cannot be updated using the UpdateUser method. 若要更新成员资格用户的密码,请使用ChangePassword MembershipUser类的方法。To update the password for a membership user, use the ChangePassword method of the MembershipUser class.

适用于

另请参阅