SqlRoleProvider 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
管理 ASP.NET 應用程式在 SQL Server 資料庫中的角色成員資訊儲存。
public ref class SqlRoleProvider : System::Web::Security::RoleProvider
public class SqlRoleProvider : System.Web.Security.RoleProvider
type SqlRoleProvider = class
inherit RoleProvider
Public Class SqlRoleProvider
Inherits RoleProvider
- 繼承
範例
以下範例展示了一個 ASP.NET 應用程式的 Web.config 檔案,該檔案設定為使用 SqlRoleProvider 物件及 SqlMembershipProvider。 該 authorization 元素設定為僅允許管理員角色中已驗證的使用者存取。
<configuration>
<connectionStrings>
<add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
</connectionStrings>
<system.web>
<authentication mode="Forms" >
<forms loginUrl="logincs.aspx"
name=".ASPXFORMSAUTH" />
</authentication>
<authorization>
<deny users="?" />
<allow roles="Administrators" />
<deny users="*" />
</authorization>
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
<providers>
<add
name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="SqlServices"
enablePasswordRetrieval="false"
enablePasswordReset="false"
requiresQuestionAndAnswer="false"
passwordFormat="Hashed"
applicationName="SampleApplication" />
</providers>
</membership>
<roleManager defaultProvider="SqlProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".ASPROLES"
cookieTimeout="30"
cookiePath="/"
cookieRequireSSL="true"
cookieSlidingExpiration="true"
cookieProtection="All" >
<providers>
<add
name="SqlProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="SqlServices"
applicationName="SampleApplication" />
</providers>
</roleManager>
</system.web>
</configuration>
備註
此類別由 Roles 和 RolePrincipal 類別使用,用於使用 SQL Server 資料庫為 ASP.NET 應用程式提供角色管理服務。 你可以使用角色管理來指定應用程式不同的授權層級。
要使用 SqlRoleProvider 類別,必須先建立 SqlRoleProvider 所使用的 SQL Server 資料庫。 要建立 SqlRoleProvider 類別所用的資料庫,執行位於 C:\WINDOWS\Microsoft.NET\Framework\ aspnet_regsql.exe 資料夾中的 versionNumber 執行檔,並指定 -Ar 選項(例如 aspnet_regsql.exe -Ar)。 所建立的資料庫稱為 Aspnetdb。 或者,執行 aspnet_regsql.exe 來開啟 GUI 設定模式,並選擇設定所有 ASP.NET 功能。
若角色提供者設定使用整合安全性的 連接字串,則 ASP.NET 應用程式的程序帳號必須擁有連接 SQL Server 資料庫的權限。
Machine.config 檔案設定了一個名為 AspNetSqlProvider 的 SqlRoleProvider 實例,該實例連接至本地機器上的SQL Server。 你可以使用這個提供者實例,或在 ASP.NET 應用程式的 Web.config 檔案中指定自己的。 要使用 AspNetSqlProvider 實例,請在你的設定中指定 AspNetSqlProvider defaultProvider 。
你可以設定 它們 SqlRoleProvider 使用與 相同的 SqlMembershipProvider 資料庫和使用者資訊,以便使用單一資料庫來進行認證與授權資訊。 若要使用相同的資料庫取得會員與角色資訊,請執行 aspnet_regsql.exe 執行檔並安裝會員功能。 接著,在你的設定中為 SqlRoleProvider 和 SqlMembershipProvider 實例指定相同的連接字串。 同時確保兩個提供者實例都設定相同的 ApplicationName。
建構函式
| 名稱 | Description |
|---|---|
| SqlRoleProvider() |
建立 SqlRoleProvider 類別的執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| ApplicationName |
取得或設定應用程式名稱,用於儲存及檢索角色資訊。 |
| Description |
取得簡短且易記的描述,適合顯示在系統管理工具或其他使用者介面中。 (繼承來源 ProviderBase) |
| Name |
取得組態期間用來參考提供者的易記名稱。 (繼承來源 ProviderBase) |
方法
| 名稱 | Description |
|---|---|
| AddUsersToRoles(String[], String[]) |
將指定的使用者名稱加入每個指定角色。 |
| CreateRole(String) |
在角色資料庫中新增一個角色。 |
| DeleteRole(String, Boolean) |
從角色資料庫中移除一個角色。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| FindUsersInRole(String, String) |
在一個角色中取得一組使用者名稱陣列,該角色中包含指定的使用者名稱以匹配。 |
| GetAllRoles() |
會列出申請中所有職缺。 |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetRolesForUser(String) |
會拿到使用者所處角色的清單。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| GetUsersInRole(String) |
會取得指定角色的使用者清單。 |
| Initialize(String, NameValueCollection) |
初始化 SQL Server 角色提供者,使用ASP.NET應用程式設定檔中指定的屬性值。 此方法並非直接從你的程式碼中使用。 |
| IsUserInRole(String, String) |
會取得一個值,表示指定的使用者是否處於指定角色。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| RemoveUsersFromRoles(String[], String[]) |
移除指定角色中指定的使用者名稱。 |
| RoleExists(String) |
會取得一個值,表示指定的角色名稱是否已存在於角色資料庫中。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |