请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
FormsAuthentication 类

为 Web 应用程序管理 Forms 身份验证服务。无法继承此类。

命名空间:System.Web.Security
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public NotInheritable Class FormsAuthentication
Visual Basic(用法)
Dim instance As FormsAuthentication
C#
public sealed class FormsAuthentication
C++
public ref class FormsAuthentication sealed
J#
public final class FormsAuthentication
JScript
public final class FormsAuthentication

Forms 身份验证使不要求 Windows 身份验证的 Web 应用程序可以进行用户和密码验证。使用 Forms 身份验证时,用户信息存储在外部数据源中,例如 Membership 数据库,或存储在应用程序的配置文件中。在用户通过身份验证后,Forms 身份验证即会在 Cookie 或 URL 中维护一个身份验证票证,这样已通过身份验证的用户就无需在每次请求时都提供凭据了。

可通过将 authentication 配置元素的 mode 属性设置为 Forms 来启用 Forms 身份验证。通过使用 authorization 配置元素可要求所有对应用程序的请求均需包含有效的用户身份验证票证,从而拒绝任何未知用户的请求,如下面的示例所示。

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="login.aspx" />
  </authentication>
  <authorization>
    <deny user="?" />
  </authorization>
</system.web>

在上面的示例中,对作为应用程序的一部分的 ASP.NET 页的任何请求都需要 Forms 身份验证提供的有效用户名。如果用户名不存在,则该请求重定向到所配置的 LoginUrl

FormsAuthentication 类提供了相应的方法和属性,您可以在需对用户进行身份验证的应用程序中使用它们。RedirectToLoginPage 方法将浏览器重定向到已配置的 LoginUrl,以便用户可以登录到应用程序。RedirectFromLoginPage 方法将通过身份验证的用户重定向回最初请求的受保护 URL 或 DefaultUrl。如果需要,还可以使用其他方法来管理 Forms 身份验证票证。

TopicLocation
如何:使用 ASP.NET Login 控件的高级功能生成 ASP .NET Web 应用程序
如何:使用 ASP.NET Login 控件的高级功能在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:创建 ASP.NET 登录页生成 ASP .NET Web 应用程序
如何:创建 ASP.NET 登录页在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:实现简单的 Forms 身份验证生成 ASP .NET Web 应用程序
如何:实现简单的 Forms 身份验证在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用成员资格和用户登录创建网站 (Visual Studio)在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:创建具有成员资格和用户登录功能的网站使用 Visual Web Developer 生成应用程序

下面的代码示例演示了一个 ASP.NET 应用程序的 Web.config 文件,该应用程序将 ASP.NET 成员资格提供程序用于 Forms 身份验证并要求对所有用户进行身份验证。

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="Data Source=MySqlServer;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
  </connectionStrings>
  <system.web>
    <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="/" />
      </providers>
    </membership>
  </system.web>
</configuration>

下面的代码示例演示使用 Forms 身份验证和 ASP.NET 成员资格的 ASP.NET 应用程序的登录页。

Visual Basic
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>

<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>
<head>
  <title>Login</title>
</head>
<body>

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

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

  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 <u>not</u> a public computer.

</form>

</body>
</html>
C#
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>

<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>
<head>
  <title>Login</title>
</head>
<body>

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

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

  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 <u>not</u> a public computer.

</form>

</body>
</html>
System.Object
  System.Web.Security.FormsAuthentication
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker