Roles.Providers 属性

定义

获取 ASP.NET 应用程序的角色提供程序的集合。

public:
 static property System::Web::Security::RoleProviderCollection ^ Providers { System::Web::Security::RoleProviderCollection ^ get(); };
public static System.Web.Security.RoleProviderCollection Providers { get; }
static member Providers : System.Web.Security.RoleProviderCollection
Public Shared ReadOnly Property Providers As RoleProviderCollection

属性值

包含为 ASP.NET 应用程序配置的角色提供程序的 RoleProviderCollection

例外

未启用角色管理。

示例

下面的代码示例列出了为应用程序启用的提供程序及其各自的类型。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Configuration.Provider" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>List Enabled Providers</title>
</head>
<body>

<%
foreach (RoleProvider p in Roles.Providers)
  Response.Write(p.Name + ", " + p.GetType() + "<br />");
%>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Configuration.Provider" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>List Enabled Providers</title>
</head>
<body>

<%
For Each p As RoleProvider In Roles.Providers
  Response.Write(p.Name & ", " & p.GetType().ToString() & "<br />")
Next
%>

</body>
</html>

注解

属性 Providers 引用为应用程序启用的所有角色提供程序,包括在 Web.config 文件中添加的任何提供程序。 可以使用应用程序的 providers Web.config 文件中 roleManager 部分的 元素来控制应用程序可用的角色提供程序。

以下示例演示 了一个 roleManager 部分,该部分删除任何现有提供程序 (,例如Machine.config文件) 中指定的提供程序,并将实例 SqlRoleProvider 添加为应用程序的角色提供程序。

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

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

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

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

可以通过按名称为角色提供程序编制索引并将其强制转换为所需类型,从 Providers 集合中获取对提供程序的强类型引用。

可以使用 属性获取对应用程序 Provider 的默认提供程序的引用。

适用于

另请参阅