请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
Roles 类
Roles 属性
 Provider 属性
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Roles.Provider 属性

注意:此属性在 .NET Framework 2.0 版中是新增的。

获取应用程序的默认角色提供程序。

命名空间:System.Web.Security
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public Shared ReadOnly Property Provider As RoleProvider
Visual Basic(用法)
Dim value As RoleProvider

value = Roles.Provider
C#
public static RoleProvider Provider { get; }
C++
public:
static property RoleProvider^ Provider {
    RoleProvider^ get ();
}
J#
/** @property */
public static RoleProvider get_Provider ()
JScript
public static function get Provider () : RoleProvider

属性值

应用程序的默认角色提供程序,作为继承 RoleProvider 抽象类的类公开。
异常类型条件

System.Configuration.Provider.ProviderException

未启用角色管理。

Provider 属性使您能够直接引用应用程序的默认角色提供程序。这通常用于访问角色提供程序的不属于 RoleProvider 抽象类的自定义成员。

例如,WindowsTokenRoleProvider 类包含 IsUserInRole 方法的重载,使您能够通过使用 WindowsBuiltInRole 枚举值来确定用户是否属于常见的 Windows 角色。对应用程序的 WindowsTokenRoleProvider 类的引用可通过使用 Provider 属性来获得,并且可将此引用强制转换为 WindowsTokenRoleProvider,以便引用 IsUserInRole 重载。

如果为应用程序配置了多个角色提供程序,则可以使用 Providers 集合访问不同的角色提供程序。

下面的代码示例演示的是:将默认角色提供程序强制转换为 WindowsTokenRoleProvider,然后在允许当前登录的用户查看应用程序的角色设置之前,检查其角色是否为 Administrators。有关启用角色管理的 Web.config 文件的示例,请参见 WindowsTokenRoleProvider

Visual Basic
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

Dim rolesArray() As String

Public Sub Page_Load()
  Msg.Text = ""

  Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)

  If Not provider.IsUserInRole(User.Identity.Name, _
                               System.Security.Principal.WindowsBuiltInRole.Administrator) Then
    Msg.Text = "You are not authorized to view user roles."
    Return
  End If


  ' Bind roles to GridView.

  Try
    rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try

  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
End Sub

</script>
<html>
<head>
<title>Sample: View User Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><BR>

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                         <Columns>
                           <asp:TemplateField HeaderText="Roles" runat="server" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>
C#
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<script runat="server">

string[] rolesArray;

public void Page_Load()
{
  Msg.Text = "";

  WindowsPrincipal p = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;

  if (!p.IsInRole(WindowsBuiltInRole.Administrator))
  {
    Msg.Text = "You are not authorized to view user roles.";
    return;
  }


  // Bind roles to GridView.

  try
  {
    rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }

  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</script>
<html>
<head>
<title>Sample: View User Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><BR>

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                         <Columns>
                           <asp:TemplateField HeaderText="Roles" runat="server" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker