转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
ASP.NET 安全性         ★★★★

ASP.NET 安全性

作者:闵涛 文章来源:闵涛的学习笔记 点击数:6750 更新时间:2009/4/23 10:44:53
返回页首

Windows 身份验证

如果应用程序用户使用可由服务器进行身份验证的 Windows 帐户(例如,在 Intranet 方案中),请使用 Windows 身份验证。

如果将 ASP.NET 配置为使用 Windows 身份验证,则 IIS 使用配置的 IIS 身份验证机制执行用户身份验证。 4 显示了这种情况。

f08sn04

4. ASP.NET Windows 身份验证使用 IIS 验证调用者的身份

已验证调用方(如果将 IIS 配置为使用匿名身份验证,则它可能是匿名 Internet 用户帐户)的访问令牌可供 ASP.NET 应用程序使用。请注意以下方面:

这将允许 ASP.NET FileAuthorizationModule 使用原调用者的访问令牌,对请求的 ASP.NET 文件执行访问检查。

重要 ASP.NET 文件授权只对映射到 Aspnet_isapi.dll 的文件类型执行访问检查。

文件授权不要求使用模拟。如果启用了模拟,则应用程序执行的所有资源访问均使用被模拟的调用者的标识。在这种情况下,请确保附加到资源上的 ACL 包含一个访问控制项 (ACE),它可授予原调用者标识至少“读”访问权限。

标识已验证身份的用户

ASP.NET 将 WindowsPrincipal 对象与当前的 Web 请求关联起来。这包含已验证身份的 Windows 用户的标识以及该用户所属的角色列表。在 Windows 身份验证中,角色列表是由用户所属的 Windows 组集组成的。

以下代码显示如何获取已验证 Windows 用户的标识,以及如何执行简单的角色授权测试。

WindowsPrincipal user = User as WindowsPrincipal;
if (null != user)
{
  string username = user.Identity.Name;
  // Perform a role check
  if ( user.IsInRole(@"DomainName\Manager") )
  {
    // User is authorized to perform manager functionality
  }
}
else
{
  // Throw security exception as we don''''t have a WindowsPrincipal
}  
返回页首返回页首

窗体身份验证

当使用窗体身份验证时,如果未验证身份的用户试图访问受保护的文件或资源(其中,URL 授权拒绝用户访问),该用户所触发的事件顺序如 5 所示。

f08sn05

5. 窗体身份验证的事件顺序

以下描述 5 所示的事件顺序:

1.

用户发出一个针对 Default.aspx 的 Web 请求。

由于启用了匿名访问,所以 IIS 允许此类请求。ASP.NET 检查 <authorization> 元素并查找 <denyusers=?" > 元素。

2.

将用户重定向到由 <forms> 元素的 loginUrl 属性指定的登录页 (Login.aspx)。

3.

用户提供凭证并提交登录窗体。

4.

根据数据存储(SQL Server 或 Active Directory)检验凭证,并检索角色(可选)。如果要使用基于角色的授权,则必须检索角色列表。

5.

使用 FormsAuthenticationTicket 创建一个 Cookie 并将其发回客户端。将角色存储到票证中。通过在票证中存储角色列表,可以避免为相同用户的每个后续 Web 请求访问数据库重新检索列表。

6.

客户端重定向将用户重定向到原请求页 (Default.aspx)。

7.

Application_AuthenticateRequest 事件处理程序(位于 Global.asax)中,使用票证创建 IPrincipal 对象并将其存储在 HttpContext.User 中。

ASP.NET 检查 <authorization> 元素并查找 <denyusers=?" > 元素。但此时对用户进行身份验证。

ASP.NET 检查 <authorization> 元素以确保用户在 <allow> 元素中。

授予用户访问 Default.aspx 的权限。

窗体身份验证的开发步骤

以下列表重点介绍实现窗体身份验证时必须执行的主要步骤:

1.

将 IIS 配置为使用匿名访问。

2.

将 ASP.NET 配置为使用窗体身份验证。

3.

创建登录 Web 窗体并检验所提供的凭证。

4.

从自定义数据存储中检索角色列表。

5.

创建窗体身份验证票证(在票证中存储角色)。

6.

创建一个 IPrincipal 对象。

7.

IPrincipal 对象放到当前 HTTP 上下文中。

8.

基于用户名/角色成员身份对用户授权。

将 IIS 配置为使用匿名访问

必须在 IIS 中将应用程序的虚拟目录配置为使用匿名访问。

将 IIS 配置为使用匿名访问的操作步骤

1.

启动 Internet 信息服务管理工具。

2.

选择应用程序的虚拟目录,右击,然后单击“属性”。

3.

单击“安全目录”。

4.

在“匿名访问”和“身份验证控制”组中,单击“编辑”。

5.

选择“匿名访问”。

将 ASP.NET 配置为使用窗体身份验证

下面显示一个配置示例。

<authentication mode="Forms">
  <forms name="MyAppFormsAuth" 
       loginUrl="login.aspx" 
       protection="Encryption"
       timeout="20" 
       path="/" >
  </forms>
</authentication>

创建登录 Web 窗体并验证提供的凭证

根据 SQL Server 数据库或 Active Directory 检验凭证。

更多信息

请参见“How To Use Forms Authentication with SQL Server 2000”。

请参见“How To Use Forms Authentication with Active Directory”。

从自定义数据存储中检索角色列表

从 SQL Server 数据库表中获取角色,或从在 Active Directory 内配置的组/分发列表中获取角色。详细信息请参考前面的资源。

创建窗体身份验证票证

在票证中存储检索到的角色。以下代码演示了此过程。

// This event handler executes when the user clicks the Logon button
// having supplied a set of credentials
private void Logon_Click(object sender, System.EventArgs e)
{
  // Validate credentials against either a SQL Server database
  // or Active Directory
  bool isAuthenticated = IsAuthenticated( txtUserName.Text, 
                                          txtPassword.Text );
  if (isAuthenticated == true )
  {
    // Retrieve the set of roles for this user from the SQL Server
    // database or Active Directory. The roles are returned as a
    // string that contains pipe separated role names
    // for example "Manager|Employee|Sales|"
    // This makes it easy to store them in the authentication ticket

    string roles = RetrieveRoles( txtUserName.Text, txtPassword.Text );

    // Create the authentication ticket and store the roles in the
    // custom UserData property of the authentication ticket
    FormsAuthenticationTicket authTicket = new 
       FormsAuthenticationTicket(
                    1,                          // version
                    txtUserName.Text,           // user name
                    DateTime.Now,               // creation
                    DateTime.Now.AddMinutes(20),// Expiration
                    false,                      // Persistent
                    roles );                    // User data
     // Encrypt the ticket.
     string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
     // Create a cookie and add the encrypted ticket to the 
     // cookie as data.
     HttpCookie authCookie = 
               new HttpCookie(FormsAuthentication.FormsCookieName,
                              encryptedTicket);

     // Add the cookie to the outgoing cookies collection. 
     Response.Cookies.Add(authCookie); 
     // Redirect the user to the originally requested page
     Response.Redirect( FormsAuthentication.GetRedirectUrl(
                        txtUserName.Text, 
                        false ));
  }
}

创建一个 IPrincipal 对象

在位于 Global.asax 的 Application_AuthenticationRequest 事件处理程序中创建 IPrincipal 对象。除非您需要基于角色的扩展功能,否则,请使用 GenericPrincipal 类。在本范例中创建一个实现 IPrincipal 的自定义类。

将 IPrincipal 对象放到当前的 HTTP 上下文中

GenericPrincipal 对象的创建过程如下所示。

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
  // Extract the forms authentication cookie
  string cookieName = FormsAuthentication.FormsCookieName;
  HttpCookie authCookie = Context.Request.Cookies[cookieName];
  if(null == authCookie)
  {
    // There is no authentication cookie.
    return;
  } 
  FormsAuthenticationTicket authTicket = null;
  try
  {
    authTicket = FormsAuthentication.Decrypt(authCookie.Value);
  }
  catch(Exception ex)
  {
    // Log exception details (omitted for simplicity)
    return;
  }
  if (null == authTicket)
  {
    // Cookie failed to decrypt.
    return; 
  }
  // When the ticket was created, the UserData property was assigned a
  // pipe delimited st

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ...  下一页 >> 


[C语言系列]NET 中C#的switch语句的语法  [系统软件]托拽Explore中的文件到VB.net的窗口
[系统软件]Boost库在XP+Visual C++.net中的安装  [常用软件]新配色面板:Paint.Net3.0RC1官方下载
[常用软件]用内建的“Net Meeting”聊天  [VB.NET程序]Henry的VB.NET之旅(三)—共享成员
[VB.NET程序]Henry的VB.NET之旅(二)—构造与析构  [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体
[VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET)  [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台