ASP.NET 中的 authentication(验证)与authorization(授权)

时间:2023-03-08 20:42:43

这两个东西很绕口,也绕脑袋。

一般来说,了解authentication(验证)的用法即可,用于自定义的用户验证。

authorization(授权)主要通过计算机信息来控制。

“*”:所有用户;

“?”:匿名用户;

官方说明如下:

http://msdn.microsoft.com/ZH-CN/library/532aee0e%28v=VS.110,d=hv.2%29.aspx
http://msdn.microsoft.com/ZH-CN/library/8d82143t%28v=VS.110,d=hv.2%29.aspx

authorization 元素

配置 Web 应用程序的授权,以控制客户端对 URL 资源的访问。

<authorization>

<allow .../>

<deny .../>

</authorization>
特性和元素

以下几节描述了特性、子元素和父元素。

子元素

子标记

说明

allow

向授权规则映射添加一个规则,该规则允许对资源进行访问。

deny

向授权规则映射添加一条拒绝对资源的访问的授权规则。

父元素

元素

说明

configuration

指定公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中均需要的根元素。

system.web

指定配置文件中 ASP.NET 配置设置的根元素,并包含用于配置 ASP.NET Web 应用程序和控制应用程序行为方式的配置元素。

authorization element configures the authorization for a Web application, controlling client access to URL resources. "> authorization 元素为 Web 应用程序配置授权,以控制对 URL 资源的客户端访问。

allow and deny elements, starting at the most local configuration file, until the authorization module finds the first access rule that fits a particular user account."> 运行时,授权模块从最本地的配置文件开始,循环访问 allow 和 deny 元素,直到它找到适合特定用户帐户的第一个访问规则。 allow or a deny rule.">然后,该授权模块根据找到的第一个访问规则是 allow 还是 deny 规则来允许或拒绝对 URL 资源的访问。 &lt;allow users="*"/&gt;.">默认的授权规则为 <allow users="*"/>。 因此,默认情况下允许访问,除非另外配置。

为了便于部署,支持表示当前计算机的句点 (.) 简便表示法。 这使您能够用句点-反斜杠序列 (.\) 为每个用户或角色加上前缀,如下所示:

<allow roles=".\roleName"/>

<allow users=".\userName"/>
运行时,句点-反斜杠序列会替换为 "本地计算机名\" 序列。 只有当 Microsoft Windows 标识用于请求时,才会进行此替换。 这是为了避免在句点-反斜杠序列用于具有自定义主体的任意规则时产生冲突。 

authorization element does not represent a collection, there are no clear or remove child elements.">因为 authorization 元素不表示集合,所以没有 clear 或 remove 子元素。 Clear() or Remove(AuthorizationRule) method.">若要以编程方式清除授权规则映射,请使用 Clear()Remove(AuthorizationRule) 方法。

默认配置

authorization element is configured in the root Web.config file in the .NET Framework version 2.0, and configured in the Machine.config file in the .NET Framework versions 1.1 and 1.0.">下面的默认 authorization 元素在 .Net Framework 2.0 版的根 Web.config 文件中配置,在 .NET Framework 1.1 和 1.0 版的 Machine.config 文件中配置。

<authorization>

  <allow users="*" />

</authorization>

下面的代码示例演示如何允许所有 Admins 角色成员进行访问以及如何拒绝所有 users 角色成员进行访问。

<configuration>

  <system.web>

    <authorization>

      <allow roles="Admins"/>

      <deny users="*"/>

    </authorization>

  </system.web>

</configuration>

authentication 元素

配置 ASP.NET 身份验证方案,该方案用于识别查看 ASP.NET 应用程序的用户。

<authentication mode="[Windows|Forms|Passport|None]"> 

   <forms>...</forms>

   <passport/>

</authentication>

以下几节描述了特性、子元素和父元素。

特性

特性

说明

mode

必需的特性。

指定应用程序的默认身份验证模式。 此特性可以为下列值之一。

Windows 将 Windows 验证指定为默认的身份验证模式。 将它与以下任意形式的 Microsoft Internet 信息服务 (IIS) 身份验证结合起来使用:基本、摘要、集成 Windows 身份验证 (NTLM/Kerberos) 或证书。 在这种情况下,您的应用程序将身份验证责任委托给基础 IIS。
Forms 将 ASP.NET 基于窗体的身份验证指定为默认身份验证模式。
Passport 将 Microsoft Passport Network 身份验证指定为默认身份验证模式。
None 不指定身份验证。 您的应用程序仅期待匿名用户,否则它将提供自己的身份验证。

Windows."> Windows.">默认值为 WindowsWindows.">。Windows.">

子元素

元素

说明

forms

配置 ASP.NET 应用程序以执行基于窗体的自定义身份验证。

passport

指定要重定向到的页(如果该页要求身份验证,而用户尚未通过 Microsoft Passport Network 身份验证注册)。

父元素

元素

说明

configuration

指定公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中均需要的根元素。

system.web

指定配置文件中 ASP.NET 配置设置的根元素,并包含用于配置 ASP.NET Web 应用程序和控制应用程序行为方式的配置元素。

authentication element configures the ASP.NET authentication scheme for an ASP.NET application."> authentication 元素为 ASP.NET 应用程序配置 ASP.NET 身份验证方案。 身份验证方案确定如何识别要查看 ASP.NET 应用程序的用户。 mode attribute specifies the authentication scheme."> mode 特性指定身份验证方案。 ASP.NET Authentication.">有关可用的身份验证方案的更多信息,请参见 ASP.NET Authentication

默认配置

authentication element is not explicitly configured in the Machine.config or the root Web.config file."> Machine.config 或根 Web.config 文件中未显式配置下面的默认 authentication 元素。 但它是 .NET Framework 2.0 版中应用程序返回的默认配置。

<authentication mode="Windows">

   <forms 

      name=".ASPXAUTH" 

      loginUrl="login.aspx" 

      defaultUrl="default.aspx" 

      protection="All" 

      timeout="30" 

      path="/" 

      requireSSL="false" 

      slidingExpiration="true" 

      cookieless="UseDeviceProfile" domain="" 

      enableCrossAppRedirects="false">

      <credentials passwordFormat="SHA1" />

   </forms>

   <passport redirectUrl="internal" />

</authentication>

示例


下面的代码示例演示如何为基于窗体的身份验证配置站点、指定传输来自客户端的登录信息的 Cookie 的名称以及指定当初始身份验证失败时使用的登录页的名称。 authorization section to require forms authentication by all users and to deny anonymous users access to the site.">必须将 authorization 节包含在内才能要求对所有用户进行 Forms 身份验证,并拒绝匿名用户访问站点。

<configuration>

   <system.web>

      <authentication mode="Forms">

         <forms name="401kApp" loginUrl="/login.aspx"/>

      </authentication>

      <authorization>

         <deny users="?"/>

      </authorization>

   </system.web>

</configuration>