惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security @ Cisco Blogs
The Last Watchdog
The Last Watchdog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
B
Blog RSS Feed
小众软件
小众软件
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Microsoft Azure Blog
Microsoft Azure Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
T
Tor Project blog
A
Arctic Wolf
Jina AI
Jina AI
Hacker News: Ask HN
Hacker News: Ask HN
F
Fortinet All Blogs
Cloudbric
Cloudbric
S
Secure Thoughts
L
LINUX DO - 热门话题
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
P
Privacy International News Feed
AWS News Blog
AWS News Blog
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
AI
AI
O
OpenAI News
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
T
Tenable Blog
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
S
SegmentFault 最新的问题

博客园 - dyq

ASP.NET架构分析 ASP.NET 页面对象模型 基于aspnet Forms身份验证基本原理 - dyq - 博客园 利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆 ASP.NET 中处理页面“回退”的方法 建立一个使用.Net 2.0 MemberShip功能的标准例程(二)——配置篇 GridView 72般绝技 SQL Server 连接字符串和身份验证 .NET程序中常用的代码 - dyq - 博客园 分析ASP.NET服务器控件开发-控件生命周期 收藏网站制作常用经典ajax.prototype.javascript实例打包下载 asp.net中<%%>形式的用法(原创) .NET模板引擎 EFPlatform.CodeGenerator (代码生成器 静态页生成 生成HTML 模板) 深入浅出之正则表达式(一) 正则表达式学习 常用的正则表达式 ASP.NET URL Rewrite. URL重写 ASP.NET 生成静态页 .NET生成静态页面并分页
实现简单的 Forms 身份验证
dyq · 2008-02-17 · via 博客园 - dyq

转自:http://www.cnblogs.com/wayne-ivan/archive/2007/12/19/1005329.html

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

Note注意

一种使用 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 身份验证

  1. 如果应用程序的根目录中有 Web.config 文件,请打开该文件。

  2. 如果应用程序的根文件夹中没有 Web.config 文件,请创建一个名为 Web.config 的文本文件,并在其中添加下列元素:

    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.web>

    </system.web>
    </configuration>

  3. system.web 元素中,创建一个 authentication 元素,并将它的 mode 属性设置为 Forms,如下面的示例所示:

    <system.web>
    <authentication mode="Forms">
    </authentication>
    </system.web>
  4. 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>
  5. system.web 元素中,创建一个 authorization 元素。

    <system.web>
    <authentication mode="Forms">
    <forms loginUrl="Logon.aspx" name=".ASPXFORMSAUTH">
    </forms>
    </authentication>
    <authorization>
    </authorization>
    </system.web>
  6. authorization 元素中,创建一个 deny 元素,并将其 users 属性设置为“?”。这是指定将拒绝未通过身份验证的用户(由“?”表示)访问该应用程序中的资源。

    <system.web>
    <authentication mode="Forms">
    <forms loginUrl="logon.aspx" name=".ASPXFORMSAUTH">
    </forms>
    </authentication>
    <authorization>
    <deny users="?" />
    </authorization>
    </system.web>
  7. 保存并关闭 Web.config 文件。

创建登录页

当用户从网站请求任何页时,如果他们此前未通过身份验证,将被重定向到名为 Logon.aspx 的页。您之前在 Web.config 文件中指定了该文件名。

Logon.aspx 页收集用户凭据(电子邮件地址和密码)并对它们进行身份验证。如果用户成功通过身份验证,登录页会将用户重定向到他们最初所请求的页。在本示例中,有效凭据被硬编码到页代码中。

Security note安全注意

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

创建登录页

  1. 在应用程序根文件夹中创建一个名为 Logon.aspx 的 ASP.NET 页。

  2. 将下面的标记和代码复制到该页中:

    <%@ 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>

    <%@ 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)。

创建默认页

  1. 在应用程序根文件夹中创建一个名为 Default.aspx 的 ASP.NET 页。

  2. 将下面的标记和代码复制到该页中:

    <%@ 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>

    <%@ 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)。然后将用户重定向到登录页。

把你不需要验证的所有页放在一个目录下面,但是不用在那个目录下面的WEB.CONFG中对FROMS验证模式进行设置。只要在最上层的WEB.CONFIG中统一设置就可以了.比如下面的例子:
一、设置所有页面都需要验证
<system web>
<authentication mode="Forms">
    <forms  loginUrl = "Lonin.aspx" name = ".ASPXFORMSAUTH"/>
</authentication> 
</system web>
二、再特别设置对某个目录下的页面不需要验证(NoAuto为不需要验证的页面所在的目录)
<location path="NoAuto">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
</location>