ChangePassword.DisplayUserName Propiedad

Definición

Obtiene o establece un valor que indica si el control ChangePassword debe mostrar el control y la etiqueta UserName.

public:
 virtual property bool DisplayUserName { bool get(); void set(bool value); };
public virtual bool DisplayUserName { get; set; }
member this.DisplayUserName : bool with get, set
Public Overridable Property DisplayUserName As Boolean

Valor de propiedad

Es true si el control ChangePassword debe mostrar UserName; en caso contrario, es false. De manera predeterminada, es false.

Ejemplos

En el ejemplo de código siguiente se muestra cómo establecer la DisplayUserName propiedad para mostrar el UserName control a los usuarios que no han iniciado sesión en el sitio web.

<%@ page language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  void Page_Load(object sender, EventArgs e )
  {
    if (Context.User.Identity.IsAuthenticated)
    {
      Changepassword1.DisplayUserName = false;
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ChangePassword.DisplayUserName sample.</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      User's login status: <asp:loginstatus id="status" runat="server" /><br />
      <asp:changepassword id="Changepassword1" runat="server" displayusername="true" />
    </div>
    </form>
</body>
</html>
<%@ page language="VB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Context.User.Identity.IsAuthenticated Then
      changepassword1.DisplayUserName = False
    End If
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ChangePassword.DisplayUserName sample.</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      User's login status: <asp:loginstatus id="status" runat="server" /><br />
      <asp:changepassword id="Changepassword1" runat="server" displayusername="true" />
    </div>
    </form>
</body>
</html>

En el ejemplo de código siguiente se muestra cómo usar una página de ASP.NET que usa un ChangePassword control e incluye un controlador para el ChangingPassword evento denominado ChangingPassword. El código del ChangingPassword controlador compara la contraseña antigua almacenada en la CurrentPassword propiedad con la nueva contraseña almacenada en NewPassword. Si las dos contraseñas son las mismas, se produce un error al cambiar la contraseña.

El ChangePassword control establece la DisplayUserName propiedad en true para permitir que el usuario escriba su nombre de usuario. Esto significa que el usuario no tiene que iniciar sesión para ver la página.

En el ejemplo de código se supone que el sitio web de ASP.NET se ha configurado para usar ASP.NET autenticación de formularios y pertenencia, y que se ha creado un usuario cuyo nombre y contraseña se conocen. Para obtener más información, vea Cómo: Implementar la autenticación de formularios simples.

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    //Manually register the event-handling methods.
    ChangePassword1.ChangingPassword += new LoginCancelEventHandler(this._ChangingPassword);
  }

  void _ChangingPassword(Object sender, LoginCancelEventArgs e)
  {
    if (ChangePassword1.CurrentPassword.ToString() == ChangePassword1.NewPassword.ToString())
    {
      Message1.Visible = true;
      Message1.Text = "Old password and new password must be different.  Please try again.";
      e.Cancel = true;
    }
    else
    {
      //This line prevents the error showing up after a first failed attempt.
      Message1.Visible = false;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true" 
      OnChangingPassword="_ChangingPassword"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!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 PageLoad(ByVal Sender As Object, ByVal e As EventArgs)
    'Manually register the event-handling methods.
    AddHandler ChangePassword1.ChangingPassword, AddressOf Me._ChangingPassword
  End Sub

  Public Sub _ChangingPassword(ByVal Sender As Object, ByVal e As LoginCancelEventArgs)
    If (ChangePassword1.CurrentPassword.ToString() = ChangePassword1.NewPassword.ToString) Then
      Message1.Visible = True
      Message1.Text = "Old password and new password must be different.  Please try again."
      e.Cancel = True
    Else
      'This line prevents the error showing up after a first failed attempt.
      Message1.Visible = False
    End If
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true" 
      OnChangingPassword="_ChangingPassword"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>

Comentarios

Para cambiar su contraseña, el proveedor de pertenencia debe autenticar a los usuarios. Para permitir que los usuarios que no han iniciado sesión cambien su contraseña o que el proveedor de pertenencia autentique con otra cuenta de usuario y, a continuación, cambien la contraseña de esa cuenta, el ChangePassword control puede mostrar un TextBox control para aceptar el nombre de usuario.

Debe establecer la DisplayUserName propiedad true en si el ChangePassword control se mostrará a los usuarios que no han iniciado sesión; de lo contrario, el usuario no podrá especificar un nombre de usuario.

Se aplica a

Consulte también