ASP.NET 2.0 中的Windows身份验证

 我来答
玄雨螺A
2022-11-08 · TA获得超过2879个赞
知道小有建树答主
回答量:627
采纳率:80%
帮助的人:79.1万
展开全部

  本教程阐释在 ASP NET 版中 IIS 集成 Windows 身份验证以及 ASP NET 的Windows 身份验证的工作机制 同时 阐释 NTLM 和 Kerberos 身份验证的工作机制 此外 本教程还阐释 WindowsAuthenticationModule 类如何构造 WindowsPrincipal 和 WindowsIdentity 对象 然后将这些对象附加到当前的 ASP NET Web 请求以表示经过身份验证的用户

  概述

  身份验证是一个验证客户端身份的过程 通常采用指定的第三方授权方式 客户端可能是最终用户 计算机 应用程序或服务 客户端的标识称为安全原则 为了使用服务器应用程序进行验证 客户端提供某种形式的凭据来允许服务器验证客户端的标识 确认了客户端的标识后 应用程序可以授予执行操作和访问资源的原则

  如果应用程序使用 Active Directory 用户存储 则应该使用集成 Windows 身份验证 对 ASP NET 应用程序使用集成 Windows 身份验证时 最好的方法是使用 ASP NET 的 Windows 身份验证提供程序附带的 Internet 信息服务 (IIS) 身份验证方法 使用该方法 将自动创建一个 WindowsPrincipal 对象(封装一个 WindowsIdentity 对象)来表示经过身份验证的用户 您无需编写任何身份验证特定的代码

  ASP NET 还支持使用 Windows 身份验证的自定义解决方案(避开了 IIS 身份验证) 例如 可以编写一个根据 Active Directory 检查用户凭据的自定义 ISAPI 筛选器 使用该方法 必须手动创建一个 WindowsPrincipal 对象

  ASP NET 身份验证

  IIS 向 ASP NET 传递代表经过身份验证的用户或匿名用户帐户的令牌 该令牌在一个包含在 IPrincipal 对象中的 IIdentity 对象中维护 IPrincipal 对象进而附加到当前 Web 请求线程 可以通过 HttpContext User 属性访问 IPrincipal 和 IIdentity 对象 这些对象和该属性由身份验证模块设置 这些模块作为 HTTP 模块实现并作为 ASP NET 管道的一个标准部分进行调用 如图 所示

  

  图 ASP NET 管道

  ASP NET 管道模型包含一个 HttpApplication 对象 多个 HTTP 模块对象 以及一个 HTTP 处理程序对象及其相关的工厂对象 HttpRuntime 对象用于处理序列的开头 在整个请求生命周期中 HttpContext 对象用于传递有关请求和响应的详细信息

  有关 ASP NET 请求生命周期的详细信息 请参阅 ASP NET Life Cycle 网址是 (en US VS ) aspx

  身份验证模块

  ASP NET 在计算机级别的 nfig 文件中定义一组 HTTP 模块 其中包括大量身份验证模块 如下所示

  

   <Modules>  <add name= WindowsAuthentication     type= System Web Security WindowsAuthenticationModule />  <add name= FormsAuthentication     type= System Web Security FormsAuthenticationModule />  <add name= PassportAuthentication     type= System Web Security PassportAuthenticationModule /></Modules>

  只加载一个身份验证模块 这取决于该配置文件的 authentication 元素中指定了哪种身份验证模式 该身份验证模块创建一个 IPrincipal 对象并将它存储在 HttpContext User 属性中 这是很关键的 因为其他授权模块使用该 IPrincipal 对象作出授权决定

  当 IIS 中启用匿名访问且 authentication 元素的 mode 属性设置为 none 时 有一个特殊模块将默认的匿名原则添加到 HttpContext User 属性中 因此 在进行身份验证之后 HttpContext User 绝不是一个空引用(在 Visual Basic 中为 Nothing)

  WindowsAuthenticationModule

  如果 nfig 文件包含以下元素 则激活 WindowsAuthenticationModule 类

  

  <authentication mode= Windows />

  WindowsAuthenticationModule 类负责创建 WindowsPrincipal 和 WindowsIdentity 对象来表示经过身份验证的用户 并且负责将这些对象附加到当前 Web 请求

  对于 Windows 身份验证 遵循以下步骤

  WindowsAuthenticationModule 使用从 IIS 传递到 ASP NET 的 Windows 访问令牌创建一个 WindowsPrincipal 对象 该令牌包装在 HttpContext 类的 WorkerRequest 属性中 引发 AuthenticateRequest 事件时 WindowsAuthenticationModule 从 HttpContext 类检索该令牌并创建 WindowsPrincipal 对象 HttpContext User 用该 WindowsPrincipal 对象进行设置 它表示所有经过身份验证的模块和 ASP NET 页的经过身份验证的用户的安全上下文

  WindowsAuthenticationModule 类使用 P/Invoke 调用 Win 函数并获得该用户所属的 Windows 组的列表 这些组用于填充 WindowsPrincipal 角色列表

  WindowsAuthenticationModule 类将 WindowsPrincipal 对象存储在 HttpContext User 属性中 随后 授权模块用它对经过身份验证的用户授权

lishixinzhi/Article/program/net/201311/13618

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式