-
如果应用程序的根目录中有 Web.config 文件,请打开该文件。
-
如果应用程序的根文件夹中没有 Web.config 文件,请创建一个名为 Web.config 的文本文件,并在其中添加下列元素:
复制代码
<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.web> </system.web> </configuration>
-
在 system.web 元素中,创建一个 authentication 元素,并将它的 mode 属性设置为
Forms
,如下面的示例所示:复制代码
<system.web> <authentication mode="Forms"> </authentication> </system.web>
-
在 authentication 元素中,创建一个 forms 元素,并设置下列属性:
-
loginUrl 设置为“Logon.aspx”。Logon.aspx 是 ASP.NET 在找不到包含请求内容的身份验证 Cookie 的情况下进行重定向时所使用的 URL。
-
name 设置为“.ASPXFORMSAUTH”。这是为包含身份验证票证的 Cookie 的名称设置的后缀。
复制代码
<system.web> <authentication mode="Forms"> <forms loginUrl="Logon.aspx" name=".ASPXFORMSAUTH"> </forms> </authentication> </system.web>
-
-
在 system.web 元素中,创建一个 authorization 元素。
复制代码
<system.web> <authentication mode="Forms"> <forms loginUrl="Logon.aspx" name=".ASPXFORMSAUTH"> </forms> </authentication> <authorization> </authorization> </system.web>
-
在 authorization 元素中,创建一个 deny 元素,并将其 users 属性设置为“?”。这是指定将拒绝未通过身份验证的用户(由“?”表示)访问该应用程序中的资源。
复制代码
<system.web> <authentication mode="Forms"> <forms loginUrl="logon.aspx" name=".ASPXFORMSAUTH"> </forms> </authentication> <authorization> <deny users="?" /> </authorization> </system.web>
-
保存并关闭 Web.config 文件。
创建登录页
当用户从网站请求任何页时,如果他们此前未通过身份验证,将被重定向到名为 Logon.aspx 的页。您之前在 Web.config 文件中指定了该文件名。
Logon.aspx 页收集用户凭据(电子邮件地址和密码)并对它们进行身份验证。如果用户成功通过身份验证,登录页会将用户重定向到他们最初所请求的页。在本示例中,有效凭据被硬编码到页代码中。
创建登录页
-
在应用程序根文件夹中创建一个名为 Logon.aspx 的 ASP.NET 页。
-
将下面的标记和代码复制到该页中:
Visual Basic 复制代码
<%@ Page Language="VB" %> <%@ Import Namespace="System.Web.Security" %> <script runat="server"> Sub Logon_Click(ByVal sender As Object, ByVal e As EventArgs) If ((UserEmail.Text = "jchen@contoso.com") And _ (UserPass.Text = "37Yj*99Ps")) Then FormsAuthentication.RedirectFromLoginPage _ (UserEmail.Text, Persist.Checked) Else Msg.Text = "Invalid credentials. Please try again." End If End Sub </script> <html> <head id="Head1" runat="server"> <title>Forms Authentication - Login</title> </head> <body> <form id="form1" runat="server"> <h3> Logon Page</h3> <table> <tr> <td> E-mail address:</td> <td> <asp:TextBox ID="UserEmail" runat="server" /></td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="UserEmail" Display="Dynamic" ErrorMessage="Cannot be empty." runat="server" /> </td> </tr> <tr> <td> Password:</td> <td> <asp:TextBox ID="UserPass" TextMode="Password" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="UserPass" ErrorMessage="Cannot be empty." runat="server" /> </td> </tr> <tr> <td> Remember me?</td> <td> <asp:CheckBox ID="Persist" runat="server" /></td> </tr> </table> <asp:Button ID="Submit1" OnClick="Logon_Click" Text="Log On" runat="server" /> <p> <asp:Label ID="Msg" ForeColor="red" runat="server" /> </p> </form> </body> </html>
C# 复制代码
<%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Security" %> <script runat="server"> void Logon_Click(object sender, EventArgs e) { if ((UserEmail.Text == "jchen@contoso.com") && (UserPass.Text == "37Yj*99Ps")) { FormsAuthentication.RedirectFromLoginPage (UserEmail.Text, Persist.Checked); } else { Msg.Text = "Invalid credentials. Please try again."; } } </script> <html> <head id="Head1" runat="server"> <title>Forms Authentication - Login</title> </head> <body> <form id="form1" runat="server"> <h3> Logon Page</h3> <table> <tr> <td> E-mail address:</td> <td> <asp:TextBox ID="UserEmail" runat="server" /></td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="UserEmail" Display="Dynamic" ErrorMessage="Cannot be empty." runat="server" /> </td> </tr> <tr> <td> Password:</td> <td> <asp:TextBox ID="UserPass" TextMode="Password" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="UserPass" ErrorMessage="Cannot be empty." runat="server" /> </td> </tr> <tr> <td> Remember me?</td> <td> <asp:CheckBox ID="Persist" runat="server" /></td> </tr> </table> <asp:Button ID="Submit1" OnClick="Logon_Click" Text="Log On" runat="server" /> <p> <asp:Label ID="Msg" ForeColor="red" runat="server" /> </p> </form> </body> </html>
此页包含用于收集用户信息的 ASP.NET 服务器控件和一个复选框,当用户单击该复选框时,他们的登录凭据将保存下来。“登录”按钮的 Click 处理程序包含对照硬编码的值来检查用户的电子邮件地址和密码的代码。(该密码是强密码,包含各种非字母字符,且至少为八个字符长。)如果用户的凭据正确,代码将调用 FormsAuthentication 类的 RedirectFromLoginPage 方法,并传递用户名和一个来源于复选框的布尔值,该值指示是否将身份验证票证保存为 Cookie。此方法将用户重定向到最初所请求的页。如果用户的凭据不匹配,将显示一条错误信息。请注意,该页会导入包含 FormsAuthentication 类的 System.Web.Security 命名空间。
创建默认页
对于本示例,您将在应用程序根文件夹中创建一个 ASP.NET 页。由于您在配置文件中指定拒绝所有未通过身份验证的用户访问应用程序的 ASP.NET 资源(包括 .aspx 文件,但不包括静态文件,例如 HTML 文件或包括图像、音乐等在内的多媒体文件),因此,当用户请求该页时,Forms 身份验证将检查用户的凭据,并在必要的时候将用户重定向到登录页。您创建的页还将允许用户注销,以清除他们的已保存身份验证票证 (Cookie)。
创建默认页
-
在应用程序根文件夹中创建一个名为 Default.aspx 的 ASP.NET 页。
-
将下面的标记和代码复制到该页中:
Visual Basic 复制代码
<%@ Page Language="VB" %> <html> <head> <title>Forms Authentication - Default Page</title> </head> <script runat="server"> Sub Page_Load(ByVal Src As Object, ByVal e As EventArgs) Welcome.Text = "Hello, " & Context.User.Identity.Name End Sub Sub Signout_Click(ByVal sender As Object, ByVal e As EventArgs) FormsAuthentication.SignOut() Response.Redirect("Logon.aspx") End Sub </script> <body> <h3> Using Forms Authentication</h3> <asp:Label ID="Welcome" runat="server" /> <form id="Form1" runat="server"> <asp:Button ID="Submit1" OnClick="Signout_Click" Text="Sign Out" runat="server" /><p> </form> </body> </html>
C# 复制代码
<%@ Page Language="C#" %> <html> <head> <title>Forms Authentication - Default Page</title> </head> <script runat="server"> void Page_Load(object sender, EventArgs e) { Welcome.Text = "Hello, " + Context.User.Identity.Name; } void Signout_Click(object sender, EventArgs e) { FormsAuthentication.SignOut(); Response.Redirect("Logon.aspx"); } </script> <body> <h3> Using Forms Authentication</h3> <asp:Label ID="Welcome" runat="server" /> <form id="Form1" runat="server"> <asp:Button ID="Submit1" OnClick="Signout_Click" Text="Sign Out" runat="server" /><p> </form> </body> </html>