WebPartManager.IsAuthorized 方法

定义

确定能否将 WebPart 或其他服务器控件添加到页中。

重载

IsAuthorized(WebPart)

执行确定控件是否被授权添加到页中的初始步骤。

IsAuthorized(Type, String, String, Boolean)

执行确定控件是否已经过授权可添加至页的最后步骤。

注解

Web 部件功能灵活性的一部分在于能够在运行时将服务器控件添加到网页。 在很多常见方案中,可以添加服务器控件 (可以是自定义 WebPart 控件、自定义服务器控件、用户控件或 ASP.NET 控件) 。

在以下常见方案中,Web 部件控件集尝试将服务器控件添加到页面,并 IsAuthorized 调用 方法来授权它们:

  • 通过在区域中网页的标记中声明服务器控件来添加服务器 WebPartZoneBase 控件时。

  • 以编程方式将服务器控件添加到区域时。

  • 当用户将服务器控件导入控件的 Web 部件目录时。

  • 从个性化数据存储加载现有服务器控件时。

  • 将服务器控件添加到 DeclarativeCatalogPart 控件以使其在服务器控件目录中可用时。

在添加控件的每个方案中, IsAuthorized 都会调用 方法,以确保满足所有授权条件以允许添加控件。 当控件获得授权时,会像没有筛选方案一样正常添加控件。 当控件未获得授权时,Web 部件控件集可以通过多种方式响应,具体取决于上下文。 如果不需要通知用户) ,控件集可能会以无提示方式无法添加未经授权的部件 (,它可以显示错误消息,或者可以添加类的 UnauthorizedWebPart 实例作为占位符。 此占位符对象在页面上不可见,但在页面源代码中可见,以指示排除了未经授权的控件。

决定控件是否获得授权的决定因素是授权筛选器。 授权筛选器是 Web 部件控件集中的一项功能,使开发人员能够从页面中排除任何不符合指定条件的控件。

若要创建筛选方案,开发人员必须执行两项操作。 首先,他们必须分配一个字符串值, (该值可以是任意) 到 AuthorizationFilter 他们计划在方案中使用的每个 WebPart 控件的 属性。 它们还可以为不是 WebPart 控件的其他类型的服务器控件分配一个值,因为如果这些控件放置在 WebPartZoneBase 区域中,则此类控件在运行时会用 控件 GenericWebPart 包装,并且此控件继承 属性 AuthorizationFilter

创建筛选方案的第二个必要步骤是替代 IsAuthorized(Type, String, String, Boolean) 方法,或为 AuthorizeWebPart 事件创建事件处理程序。 在这些方法中,开发人员可以检查 AuthorizationFilter 属性,如果值指示不应授权控件,开发人员将确保IsAuthorized该方法返回值 false

注意

有关代码示例以及如何使用 方法设置自定义筛选方案 IsAuthorized 的说明,请参阅 方法重载的主题。

IsAuthorized(WebPart)

执行确定控件是否被授权添加到页中的初始步骤。

public:
 bool IsAuthorized(System::Web::UI::WebControls::WebParts::WebPart ^ webPart);
public bool IsAuthorized (System.Web.UI.WebControls.WebParts.WebPart webPart);
member this.IsAuthorized : System.Web.UI.WebControls.WebParts.WebPart -> bool
Public Function IsAuthorized (webPart As WebPart) As Boolean

参数

webPart
WebPart

正在接受授权检查的 WebPart 或其他服务器控件。

返回

一个布尔值,指示能否将 webPart 添加到页中。

例外

webPartnull

示例

下面的代码示例演示如何从代码调用 IsAuthorized(WebPart) 方法,以确定控件是否有权添加到页面。

该代码示例包含三个部分:

此代码示例使用重写重载方法的IsAuthorized(Type, String, String, Boolean)自定义WebPartManager控件来提供 属性的AuthorizationFilter自定义处理。 此控件检查 属性值 admin ,如果存在该值,则对控件进行授权。 如果控件具有不同的值,则它未获得授权;不带属性值的控件也已获得授权,因为假定它们不是筛选方案的一部分。

若要运行此代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的“App_Code”文件夹中,并在运行时对其进行动态编译。 此代码示例使用动态编译方法。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{
  public class MyManagerAuthorize : WebPartManager
  {
    public override bool IsAuthorized(Type type, string path, string authorizationFilter, bool isShared)
    {
      if (!String.IsNullOrEmpty(authorizationFilter))
      {
        if (authorizationFilter == "admin")
          return true;
        else
          return false;
      }
      else
            {
                return true;
            }
        }
  }
}
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class MyManagerAuthorize
    Inherits WebPartManager

    Public Overrides Function IsAuthorized(ByVal type As Type, _
      ByVal path As String, ByVal authorizationFilter As String, _
      ByVal isShared As Boolean) As Boolean

      If Not String.IsNullOrEmpty(authorizationFilter) Then
        If authorizationFilter = "admin" Then
          Return True
        Else
          Return False
        End If
      Else
        Return True
      End If

    End Function

  End Class

End Namespace

代码示例的第二部分创建一个筛选器,该筛选器可能会排除控件。 以下网页在 元素中包含三个 <asp:webpartzone> ASP.NET 服务器控件。 请注意,第一个和第二个控件的属性 AuthorizationFilter 设置为不同的值,而第三个控件不分配属性。 可以在运行时检查此授权值,如果筛选器与开发人员设置的条件匹配,则可以将控件添加到页面。 另请注意,在 方法中 Page_Load ,代码调用 IsAuthorized(WebPart) 方法以确定每个控件是否获得授权,如果是,则设置每个控件的 ExportMode 属性。

<%@ Page Language="C#" %>
<%@ Register Namespace="Samples.AspNet.CS.Controls" 
    TagPrefix="aspSample"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  protected void Page_Load(object sender, EventArgs e)
  {
    foreach (WebPart part in mgr1.WebParts)
    {
      if (mgr1.IsAuthorized(part))
        part.ExportMode = WebPartExportMode.All;
    }
    
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <aspSample:MyManagerAuthorize ID="mgr1" runat="server"  />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            ID="BulletedList1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links"
            AuthorizationFilter="admin">
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>
          <asp:Label ID="Label1" runat="server" 
            Text="Hello World"
            AuthorizationFilter="user" />
          <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register Namespace="Samples.AspNet.VB.Controls" 
    TagPrefix="aspSample"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Dim part As WebPart
    For Each part In mgr1.WebParts
      If mgr1.IsAuthorized(part) Then
        part.ExportMode = WebPartExportMode.All
      End If
    Next
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <aspSample:MyManagerAuthorize ID="mgr1" runat="server"  />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            ID="BulletedList1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links"
            AuthorizationFilter="admin">
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>
          <asp:Label ID="Label1" runat="server" 
            Text="Hello World"
            AuthorizationFilter="user" />
          <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

请注意,为了使代码示例正常工作,必须在 Web.config 文件中添加设置,以启用导出 Web 部件说明文件。 确保此代码示例的网页位于同一目录中的 Web.config 文件。 在 节中<system.web>,确保存在属性<webParts>设置为 true的 元素enableExport,如以下标记所示。

<webParts enableExport="true">

...

</webParts>

在浏览器中加载页面后,请注意显示第一个控件,因为它与重写方法中的条件匹配。 第二个控件不会添加到页面,因为它被筛选器排除。 还添加了第三个控件,因为它没有设置其 AuthorizationFilter 属性。 请注意,如果单击任一控件标题栏中的谓词菜单图标,则可以导出它们,因为它们各自的 ExportMode 属性值已分配。

注解

方法IsAuthorized是由设置为检查控件授权WebPart的 Web 部件控件调用的初始方法。 它接受 webPart 作为参数,并开始一个最终确定是否将控件添加到页面的过程。 需要确定给定控件是否获得授权时,直接从代码中调用此方法。

此方法执行初始任务,即确定控件是继承自 WebPartGenericWebPart 还是控件,如果是,则确定它包含哪种类型的子控件。 为了完成授权任务,它会调用 IsAuthorized(Type, String, String, Boolean) 重载方法。

调用方说明

此方法直接从代码调用。 如果想要以编程方式更好地控制授权过程,可以重写 IsAuthorized(Type, String, String, Boolean) 重载方法。

另请参阅

适用于

IsAuthorized(Type, String, String, Boolean)

执行确定控件是否已经过授权可添加至页的最后步骤。

public:
 virtual bool IsAuthorized(Type ^ type, System::String ^ path, System::String ^ authorizationFilter, bool isShared);
public virtual bool IsAuthorized (Type type, string path, string authorizationFilter, bool isShared);
abstract member IsAuthorized : Type * string * string * bool -> bool
override this.IsAuthorized : Type * string * string * bool -> bool
Public Overridable Function IsAuthorized (type As Type, path As String, authorizationFilter As String, isShared As Boolean) As Boolean

参数

type
Type

被检查授权情况的控件的 Type

path
String

被授权的控件的源文件的相对应用程序路径(如果该控件为用户控件)。

authorizationFilter
String

赋予 AuthorizationFilter 控件的 WebPart 属性的任意字符串值,用于授权是否可将控件添加至页中。

isShared
Boolean

指示被检查授权情况的控件是否为共享控件,共享意味着它对应用程序的许多用户或所有用户可见,并且其 IsShared 属性值设为 true

返回

一个布尔值,指示控件是否已经过授权可添加至页中。

例外

typenull

type 为用户控件,且 pathnull 或空字符串 ("")。

- 或 -

type 不是用户控件,且 path 已赋值。

示例

下面的代码示例演示如何重写 方法, IsAuthorized 以确定控件是否有权添加到页面。

第一步是创建可能排除控件的筛选器。 以下网页在 元素中包含三个 <asp:webpartzone> ASP.NET 服务器控件。 请注意,第一个和第二个控件的属性 AuthorizationFilter 设置为不同的值,而第三个控件不分配属性。 可以在运行时检查此授权值,如果筛选器与开发人员设置的条件匹配,则可以将控件添加到页面。

<%@ Page Language="C#" %>
<%@ Register Namespace="Samples.AspNet.CS.Controls" 
    TagPrefix="aspSample"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  protected void Page_Load(object sender, EventArgs e)
  {
    foreach (WebPart part in mgr1.WebParts)
    {
      if (mgr1.IsAuthorized(part))
        part.ExportMode = WebPartExportMode.All;
    }
    
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <aspSample:MyManagerAuthorize ID="mgr1" runat="server"  />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            ID="BulletedList1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links"
            AuthorizationFilter="admin">
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>
          <asp:Label ID="Label1" runat="server" 
            Text="Hello World"
            AuthorizationFilter="user" />
          <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register Namespace="Samples.AspNet.VB.Controls" 
    TagPrefix="aspSample"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Dim part As WebPart
    For Each part In mgr1.WebParts
      If mgr1.IsAuthorized(part) Then
        part.ExportMode = WebPartExportMode.All
      End If
    Next
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <aspSample:MyManagerAuthorize ID="mgr1" runat="server"  />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            ID="BulletedList1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links"
            AuthorizationFilter="admin">
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>
          <asp:Label ID="Label1" runat="server" 
            Text="Hello World"
            AuthorizationFilter="user" />
          <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

第二步是重写 IsAuthorized(Type, String, String, Boolean) 方法,并为授权筛选器创建自定义处理。 请注意,代码首先检查属性是否具有值,以便自动添加任何未分配该属性的 AuthorizationFilter 控件。 如果控件具有筛选器,则仅当筛选器值等于 admin时,代码才返回 true 。 这演示了一种简单机制,你可以使用该机制向某些用户显示某些控件,具体取决于用户的角色。 虽然使用角色的完整示例超出了本主题的范围,但你可以使用与本代码示例中重写的方法相同的逻辑,但可以检查当前用户是否处于与授权筛选器值匹配的角色,然后仅添加该用户的控件。 这将使你能够创建页面,其中一些用户将看到所有控件,而其他用户将只看到选定的控件。 如果使用角色,检查筛选器的逻辑可能如下所示:

If Roles.IsUserInRole(Page.User.Identity.Name, authorizationFilter) Then  
  return True  
Else  
  return False  
End If  
if(Roles.IsUserInRole(Page.User.Identity.Name, authorizationFilter))  
    return true;  
else  
    return false;  

若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的“App_Code”文件夹中,并在运行时对其进行动态编译。 此代码示例使用动态编译方法。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{
  public class MyManagerAuthorize : WebPartManager
  {
    public override bool IsAuthorized(Type type, string path, string authorizationFilter, bool isShared)
    {
      if (!String.IsNullOrEmpty(authorizationFilter))
      {
        if (authorizationFilter == "admin")
          return true;
        else
          return false;
      }
      else
            {
                return true;
            }
        }
  }
}
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class MyManagerAuthorize
    Inherits WebPartManager

    Public Overrides Function IsAuthorized(ByVal type As Type, _
      ByVal path As String, ByVal authorizationFilter As String, _
      ByVal isShared As Boolean) As Boolean

      If Not String.IsNullOrEmpty(authorizationFilter) Then
        If authorizationFilter = "admin" Then
          Return True
        Else
          Return False
        End If
      Else
        Return True
      End If

    End Function

  End Class

End Namespace

在浏览器中加载页面后,请注意显示第一个控件,因为它与重写方法中的条件匹配。 第二个控件不会添加到页面,因为它的筛选器值被排除。 添加了第三个控件,因为它没有设置其 AuthorizationFilter 属性。 如果更改第二个控件上的属性值以匹配第一个控件的属性值,然后再次运行页面,则还会添加第二个控件。

注解

IsAuthorized(Type, String, String, Boolean) 载方法执行最后的步骤,以确定控件是否有权添加到页面。 方法确保 type 是有效的类型,并且 path 仅在被检查的控件是用户控件时具有 值。 然后,它会调用引发 事件的关键 OnAuthorizeWebPart 方法 AuthorizeWebPart

继承者说明

如果要在检查授权时提供其他处理,可以通过从 WebPartManager 类继承来重写此方法。 你可能希望重写 方法,以便为 参数中的authorizationFilter某些值检查,并根据值返回一个布尔值,该值确定是否将控件添加到页面。

对于还想要为授权筛选器检查并提供自定义处理的页面开发人员,可以选择在 .aspx 页或代码隐藏文件中内联执行此操作,而无需从任何类继承。 可以在页中为 OnAuthorizeWebPart(WebPartAuthorizationEventArgs) 控件的 WebPartManager 方法声明备用事件处理程序。 有关更多详细信息和示例,请参阅 OnAuthorizeWebPart(WebPartAuthorizationEventArgs) 方法。

另请参阅

适用于