Membership.ValidateUser(String, String) Método

Definição

Verifica se o nome de usuário e a senha fornecidos são válidos.Verifies that the supplied user name and password are valid.

public:
 static bool ValidateUser(System::String ^ username, System::String ^ password);
public static bool ValidateUser (string username, string password);
static member ValidateUser : string * string -> bool
Public Shared Function ValidateUser (username As String, password As String) As Boolean

Parâmetros

username
String

O nome do usuário a ser validado.The name of the user to be validated.

password
String

A senha do usuário especificado.The password for the specified user.

Retornos

Boolean

true se o nome de usuário e a senha fornecidos forem válidos; caso contrário, false.true if the supplied user name and password are valid; otherwise, false.

Exemplos

O exemplo de código a seguir mostra a página de logon de um aplicativo ASP.NET configurado para usar a autenticação de formulários e a Membership classe.The following code example shows the login page for an ASP.NET application configured to use forms authentication and the Membership class. Se as credenciais de usuário fornecidas forem inválidas, uma mensagem será exibida para o usuário.If the supplied user credentials are invalid, a message is displayed to the user. Caso contrário, o usuário será redirecionado para a URL solicitada originalmente pelo RedirectFromLoginPage método.Otherwise, the user is redirected to the originally requested URL by the RedirectFromLoginPage method.

Importante

Este exemplo contém uma caixa de texto que aceita a entrada do usuário, que é uma possível ameaça à segurança.This example contains a text box that accepts user input, which is a potential security threat. Por padrão, as páginas da Web do ASP.NET validam que a entrada do usuário não inclui elementos de script ou HTML.By default, ASP.NET Web pages validate that user input does not include script or HTML elements. Para obter mais informações, consulte Visão geral de explorações de script.For more information, see Script Exploits Overview.

<%@ 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 Login_OnClick(object sender, EventArgs args)
{
   if (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text))
      FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked);
   else
     Msg.Text = "Login failed. Please check your user name and password and try again.";
}


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Login</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Login</h3>

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

  Username: <asp:Textbox id="UsernameTextbox" runat="server" /><br />
  Password: <asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /><br />
 
  <asp:Button id="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" />
  <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
  Check here if this is <span style="text-decoration:underline">not</span> a public computer.

</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 Login_OnClick(sender As Object, args As  EventArgs)

   If (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text)) Then
      FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked)
   Else
     Msg.Text = "Login failed. Please check your user name and password and try again."
   End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Login</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Login</h3>

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

  Username: <asp:Textbox id="UsernameTextbox" runat="server" /><br />
  Password: <asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /><br />
 
  <asp:Button id="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" />
  <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
  Check here if this is <span style="text-decoration:underline">not</span> a public computer.

</form>

</body>
</html>

Comentários

ValidateUser fornece uma maneira fácil de verificar um nome de usuário e uma senha da fonte de dados.ValidateUser provides an easy way to verify a user name and password from the data source. Observe que, se o username parâmetro estiver vazio ou null , um HttpException será lançado.Note that, if the username parameter is empty or null, an HttpException is thrown.

Aplica-se a