FormsAuthentication.Authenticate(String, String) 方法

定义

注意

The recommended alternative is to use the Membership APIs, such as Membership.ValidateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.

对照存储在应用程序配置文件中的凭据来验证用户名和密码。

public:
 static bool Authenticate(System::String ^ name, System::String ^ password);
public static bool Authenticate (string name, string password);
[System.Obsolete("The recommended alternative is to use the Membership APIs, such as Membership.ValidateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.")]
public static bool Authenticate (string name, string password);
static member Authenticate : string * string -> bool
[<System.Obsolete("The recommended alternative is to use the Membership APIs, such as Membership.ValidateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.")>]
static member Authenticate : string * string -> bool
Public Shared Function Authenticate (name As String, password As String) As Boolean

参数

name
String

用户名。

password
String

用户的密码。

返回

Boolean

如果用户名和密码有效,则返回 true;否则返回 false

属性

示例

下面的代码示例演示存储在应用程序的Web.config文件中的用户凭据。 密码值已使用 HashPasswordForStoringInConfigFile 该方法进行哈希处理。

此示例使用 SHA1。 由于与 SHA1 冲突,Microsoft 建议使用 SHA256。

<authentication mode="Forms">

<forms loginUrl="login.aspx">

<credentials passwordFormat="SHA1">

<user name="user1" password="27CE4CA7FBF00685AF2F617E3F5BBCAFF7B7403C" />

<user name="user2" password="D108F80936F78DFDD333141EBC985B0233A30C7A" />

<user name="user3" password="7BDB09781A3F23885CD43177C0508B375CB1B7E9"/>

</credentials>

</forms>

</authentication>

下面的代码示例显示了一个登录页,该页使用 Authenticate 该方法验证用户凭据。

重要

此示例包含接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 (FormsAuthentication.Authenticate(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 FormsAuthentication.Authenticate(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>

注解

Authenticate 方法验证存储在应用程序配置文件的凭据部分中的用户 凭据 。 或者,可以使用 ASP.NET 成员身份来存储用户凭据并调用ValidateUser以验证凭据。 有关详细信息,请参阅 使用成员身份管理用户

为了提高安全性,可以使用该方法 HashPasswordForStoringInConfigFile 加密存储在应用程序的配置文件中的密码。

适用于

另请参阅