Bewerken

Delen via


WebPartManager.AuthorizeWebPart Event

Definition

Occurs when the IsAuthorized method is called to determine whether a WebPart or server control can be added to a page.

public:
 event System::Web::UI::WebControls::WebParts::WebPartAuthorizationEventHandler ^ AuthorizeWebPart;
public event System.Web.UI.WebControls.WebParts.WebPartAuthorizationEventHandler AuthorizeWebPart;
member this.AuthorizeWebPart : System.Web.UI.WebControls.WebParts.WebPartAuthorizationEventHandler 
Public Custom Event AuthorizeWebPart As WebPartAuthorizationEventHandler 

Event Type

Examples

The following code example demonstrates how to set a custom event handler for the AuthorizeWebPart event, which automatically overrides the default OnAuthorizeWebPart method.

The code in the mgr1_AuthorizeWebPart method checks whether the controls on the page have their respective AuthorizationFilter property values set to user and, if so, returns true, which means that they will be authorized and added to the page. This assumes the default approach is to allow users to view controls with a page in user personalization scope. Notice, however, that in the example one of the controls has its AuthorizationFilter property value set to admin. Developers might place this filter on a specialized control that was designed for only administrative users to see. This control will fail the authorization check during the AuthorizeWebPart event, and will not be displayed. Note that controls that do not have the property set are displayed as well; they are assumed not to be part of a filtering scenario because their AuthorizationFilter properties are not set.

<%@ Page Language="C#" %>

<!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 mgr1_AuthorizeWebPart(object sender, 
    WebPartAuthorizationEventArgs e)
  {
    if (!String.IsNullOrEmpty(e.AuthorizationFilter))
    {
      if (e.AuthorizationFilter == "user")
        e.IsAuthorized = true;
      else
        e.IsAuthorized = false;
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr1" runat="server"
        OnAuthorizeWebPart="mgr1_AuthorizeWebPart" />
      <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"
            Title="Filter Test"
            AuthorizationFilter="admin" />
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar"/>
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>

<!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 mgr1_AuthorizeWebPart(ByVal sender As Object, _
    ByVal e As WebPartAuthorizationEventArgs)
    
    If Not String.IsNullOrEmpty(e.AuthorizationFilter) Then
      If e.AuthorizationFilter = "user" Then
        e.IsAuthorized = True
      Else
        e.IsAuthorized = False
      End If
    End If

  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr1" runat="server" 
        OnAuthorizeWebPart="mgr1_AuthorizeWebPart" />
      <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"
            Title="Filter Test"
            AuthorizationFilter="admin" />
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar"/>
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    </form>
</body>
</html>

Remarks

The AuthorizeWebPart event occurs whenever a WebPart control is being added to a page. There are a number of common scenarios where a control can be added to a page. For a full description of these, see the Remarks section for the IsAuthorized method. When a control is added, it must be checked to see whether its AuthorizationFilter property has been set and, if so, whether the control is authorized to be added to the page.

Developers can create event handlers for the AuthorizeWebPart event, to provide filtering for controls. If a control's AuthorizationFilter property value does not meet the criteria in the event handler code, the control is not added to the page.

Applies to

See also