您的位置:首页 > 编程语言 > ASP

如何:实现简单的 Forms 身份验证(asp.net)

2007-12-19 10:52 1031 查看
ASP.NET
如何:实现简单的 Forms 身份验证

本主题中的示例演示了 ASP.NET Forms 身份验证的简单实现。该示例旨在阐释关于如何使用 Forms 身份验证来允许用户登录到 ASP.NET 应用程序的基础知识。


注意
一种使用 Forms 身份验证的方便途径是使用 ASP.NET 成员资格和 ASP.NET 登录控件。ASP.NET 成员资格提供了存储和管理用户信息的方式,并包含对用户进行身份验证的方法。ASP.NET 登录控件使用 ASP.NET 成员资格,并封装提示用户输入凭据、验证用户、恢复或替换密码等操作所需的逻辑。实际上,ASP.NET 成员资格和 ASP.NET 登录控件在 Forms 身份验证之上提供了一个抽象层,从而取代了要使用 Forms 身份验证通常必须完成的大部分甚至所有工作。有关更多信息,请参见使用成员资格管理用户ASP.NET 登录控件概述

在该示例的方案中,用户请求一个受保护的资源,即名为 Default.aspx 的页。只有一个用户可以访问此受保护的资源:jchen@contoso.com,其密码为“37Yj*99P”。该用户名和密码已硬编码到 Logon.aspx 文件中。该示例需要三个文件:Web.config 文件、名为 Logon.aspx 的页以及名为 Default.aspx 的页。这些文件位于应用程序根目录中。

配置应用程序使用 Forms 身份验证

如果应用程序的根目录中有 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 页收集用户凭据(电子邮件地址和密码)并对它们进行身份验证。如果用户成功通过身份验证,登录页会将用户重定向到他们最初所请求的页。在本示例中,有效凭据被硬编码到页代码中。


安全注意
该示例包含一个文本框,用于接受用户输入,这是一个潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述

创建登录页

在应用程序根文件夹中创建一个名为 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>


此页显示用户的已通过身份验证的标识,该标识是由 FormsAuthentication 类设置的,并作为 Context.User.Identity.Name 属性在 ASP.NET 页中提供。“注销”按钮的 Click 处理程序包含具有如下作用的代码:调用 SignOut 方法以清除用户标识并移除身份验证票证 (Cookie)。然后将用户重定向到登录页。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: