Custom Security for Web Parts 

Tags: MOSS, STSDev 2008

Many web parts require access to SPSite, SPWeb, etc. type items.  Generally in order to get those web parts to work that requires a deployment to the GAC, or the painful creating of custom security policy files.  However, there is a much simpiler method.

First, rather than deploying web parts as web parts deploy them to SharePoint as Solutions.  This is very simple to do and provides a greate deal of control.  Below is a Manifest file I have used to deploy web parts.

Web Part Manifest File:

<?xml version="1.0" encoding="utf-8" ?>

<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">

  <Assemblies>

    <Assembly FileName="MicroLink.HelloWorldWebPart.dll">

      <ClassResources>

      </ClassResources>

      <SafeControls>

        <SafeControl Namespace="MicroLink.HelloWorld" TypeName="*">

        </SafeControl>

      </SafeControls>

    </Assembly>

  </Assemblies>

  <DwpFiles>

    <DwpFile FileName="HelloWorldWebPart.dwp">

    </DwpFile>

  </DwpFiles>

</WebPartManifest>

   

Solution Manifest File: (Note the Solution ID MUST be unique for each solution you create)

<Solution SolutionId="b379a73b-1b4d-4a7b-abaa-8e04c1c32844" xmlns="http://schemas.microsoft.com/sharepoint/" >

  <Assemblies>

    <Assembly Location="MicroLink.HelloWorldWebPart.dll">

      <SafeControls>

        <SafeControl Namespace="MicroLink.HelloWorld" TypeName="*" Safe="True" />

      </SafeControls>

    </Assembly>

  </Assemblies>

  <DwpFiles>

    <DwpFile FileName="HelloWorldWebPart.dwp" Location="HelloWorldWebPart.dwp"/>

  </DwpFiles>

</Solution>

   

Next using the Solution Manifest File above add the following code inside the Solution Node to enable specialized permissions.

<CodeAccessSecurity>

    <PolicyItem>

      <PermissionSet class="NamedPermissionSet" version="1" Description="My webpart's permission set">

        <IPermission class="AspNetHostingPermission" version="1" Level="Minimal"/>

        <IPermission class="SecurityPermission" version="1" Flags="Execution" />

        <IPermission version="1" Unrestricted="True"

                    class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

      </PermissionSet>

      <Assemblies>

        <Assembly Name="MicroLink.HelloWorldWebPart"/>

      </Assemblies>

    </PolicyItem>

  </CodeAccessSecurity>

   

Finally, all you have to do now is use stsadm solution commands to add, deploy, upgrade, retract, and removed the solution. 

 As an added benefit you can manage your deployment, retraction, and removal from Central Admin.

 
Posted by David McWee on 17-Oct-07
0 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
 
Name:
URL:
Email:
Comments: