跳轉到內容

ASP.NET/角色

來自華夏公益教科書,開放的書籍,開放的世界

角色是一組使用者,他們根據授予的特權進行唯一標識。一個典型的網站有三種角色

  • 管理員
  • 高階使用者
  • 使用者

管理員對特定網站擁有所有許可權,而高階使用者和普通使用者則沒有。

角色的建立

[編輯 | 編輯原始碼]

角色可以透過多種方式建立。典型的角色建立過程涉及在 Web.config 中使用角色管理器標籤。

示例

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="Data Source=localhost;Initial Catalog=aspnetdb;Integrated Security=SSPI;" />
  </connectionStrings>

  <system.web>
    <authentication mode="Forms" >
      <forms loginUrl="login.aspx"
      name=".ASPXFORMSAUTH" />
    </authentication>

    <authorization>
      <deny users="?" />
      <allow roles="Administrators" />
      <deny users="*" />
    </authorization>

    <membership defaultProvider="AspNetSqlProvider" userIsOnlineTimeWindow="15">
    </membership>

    <roleManager defaultProvider="SqlProvider" 
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieRequireSSL="true"
      cookieSlidingExpiration="true"
      cookieProtection="All" >

      <providers>
        <clear />
        <add
          name="SqlProvider"
          type="System.Web.Security.SqlRoleProvider"
          connectionStringName="SqlServices"
          applicationName="SampleApplication" />
        </providers>

    </roleManager>
  </system.web>
</configuration>

也可以使用 Role 類以程式設計方式分配或修改角色。

使用網站管理工具

[編輯 | 編輯原始碼]

可以使用 ASP.NET 網站管理工具在 .NET 2.0 中建立角色。此工具在解決方案資源管理器工具欄中可用。

該工具允許使用者更改

  • 安全設定
  • 應用程式配置
  • 提供程式配置

以下是該工具的一些快照。這些快照是不言自明的。

參考文獻

[編輯 | 編輯原始碼]

C# 中的 Role 類,MSDN 網站

華夏公益教科書