Membership.EnablePasswordReset Propiedad
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene un valor que indica si se configura el proveedor de pertenencia actual para permitirles a los usuarios restablecer sus contraseñas.
public:
static property bool EnablePasswordReset { bool get(); };
public static bool EnablePasswordReset { get; }
static member EnablePasswordReset : bool
Public Shared ReadOnly Property EnablePasswordReset As Boolean
Valor de propiedad
true
si el proveedor de pertenencia admite el restablecimiento de contraseña; de lo contrario, false
.
Ejemplos
En el ejemplo de código siguiente se muestra el elemento membership de la system.web
sección del archivo Web.config para una aplicación de ASP.NET. Especifica que la aplicación usa una instancia de y habilita el restablecimiento de SqlMembershipProvider contraseña.
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
<providers>
<add name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="SqlServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
passwordFormat="Hashed"
applicationName="MyApplication" />
</providers>
</membership>
En el ejemplo de código siguiente se comprueba primero que EnablePasswordReset es true
y, a continuación, se restablece la contraseña de un usuario y se devuelve la nueva contraseña generada automáticamente.
<%@ 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">
public void Page_Load(object sender, EventArgs args)
{
if (!Membership.EnablePasswordReset)
{
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 reenter your user name.";
QuestionLabel.Text = "";
QuestionLabel.Enabled = false;
AnswerTextBox.Enabled = false;
ResetPasswordButton.Enabled = false;
}
else
{
QuestionLabel.Text = user.PasswordQuestion;
QuestionLabel.Enabled = true;
AnswerTextBox.Enabled = true;
ResetPasswordButton.Enabled = true;
}
}
public void ResetPassword_OnClick(object sender, EventArgs args)
{
string newPassword = "";
try
{
newPassword = Membership.Provider.ResetPassword(UsernameTextBox.Text, AnswerTextBox.Text);
}
catch (NotSupportedException e)
{
Msg.Text = "An error has occurred resetting your password: " + e.Message + "." +
"Please check your values and try again.";
}
catch (MembershipPasswordException e)
{
Msg.Text = "Invalid password answer. Please reenter the answer and try again.";
return;
}
catch (System.Configuration.Provider.ProviderException e)
{
Msg.Text = "The specified user name does not exist. Please check your value and try again.";
}
if (newPassword != "")
{
Msg.Text = "Password reset. Your new password is: " + Server.HtmlEncode(newPassword);
}
else
{
Msg.Text = "Password reset failed. Please reenter your values and try again.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Reset Password</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Reset 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="ResetPasswordButton" Text="Reset Password"
OnClick="ResetPassword_OnClick" runat="server" Enabled="false" />
</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">
Public Sub Page_Load(sender As Object, args As EventArgs)
If Not Membership.EnablePasswordReset Then
FormsAuthentication.RedirectToLoginPage()
End If
Msg.Text = ""
If Not IsPostBack Then
Msg.Text = "Please enter a user name."
Else
VerifyUsername()
End If
End Sub
Public 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 reenter your user name."
QuestionLabel.Text = ""
QuestionLabel.Enabled = False
AnswerTextBox.Enabled = False
ResetPasswordButton.Enabled = False
Else
QuestionLabel.Text = user.PasswordQuestion
QuestionLabel.Enabled = True
AnswerTextBox.Enabled = True
ResetPasswordButton.Enabled = True
End If
End Sub
Public Sub ResetPassword_OnClick(sender As Object, args As EventArgs)
Dim newPassword As String = ""
Try
newPassword = Membership.Provider.ResetPassword(UsernameTextBox.Text, AnswerTextBox.Text)
Catch e As NotSupportedException
Msg.Text = "An error has occurred resetting your password: " & e.Message & "." & _
"Please check your values and try again."
Catch e As MembershipPasswordException
Msg.Text = "Invalid password answer. Please reenter the answer and try again."
Return
Catch e As System.Configuration.Provider.ProviderException
Msg.Text = "The specified user name does not exist. Please check your value and try again."
End Try
If newPassword <> "" Then
Msg.Text = "Password reset. Your new password is: " & Server.HtmlEncode(newPassword)
Else
Msg.Text = "Password reset failed. Please reenter your values and try again."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Reset Password</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Reset 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="ResetPasswordButton" Text="Reset Password"
OnClick="ResetPassword_OnClick" runat="server" Enabled="False" />
</form>
</body>
</html>
Comentarios
El restablecimiento de contraseña es la capacidad de ASP.NET pertenencia para reemplazar la contraseña actual de un nombre de usuario por una contraseña nueva generada aleatoriamente cuando un usuario ha olvidado su contraseña o la contraseña actual ya no es válida. Esto resulta especialmente útil cuando el formato de contraseña se establece Hasheden , ya que los usuarios no pueden recuperar valores de contraseña con hash.