Web Parts can be deployed in two different places:
- Deploy a Web Part library DLL inside a virtual server's \bin directory.
- Deploy a Web Part library DLL in the Global Assembly Cache (GAC). A Web Part library DLL in the GAC can be loaded into any virtual server on the hosting Web server machine.
The fact that Web Part Pages are stored inside the SharePoint content database poses a security risk. In order to overcome security threat, WSS protects itself by processing Web Part Pages in safe mode. The Web Part architecture only loads Web Parts and ASP.NET controls that have been explicitly configured as a safe control.
We can configure Web Parts and ASP.NET controls using the <SafeControls> section of the hosting virtual server's web.config file that look like this.
| <!– In web.config of hosting virtual server --> <configuration> <SharePoint> <SafeControls> <SafeControl Assembly="MyWebParts" Namespace="MyWebParts" TypeName="*" Safe="True" /> </SafeControls> </SharePoint> </configuration> |
The security layer provided by the Web Part architecture goes beyond requiring that Web Parts and ASP.NET controls are configured as safe controls. A Web Part library DLL running inside the \bin directory is further restricted in what actions it can perform by the <trust> element defined within the web.config file that look like this.
| <configuration> </system.web> <!-- set trust level to (1) WSS_Minimal (2) WSS_Medium or (3) Full --> <trust level="WSS_Minimal" originUrl="" /> </system.web> </configuration> |
The <trust> element contains the level attribute. The value of the level attribute configures Code Access Security (CAS) permissions that restrict Web Part library DLLs from performing potentially dangerous actions such as connecting to a SQL Server database and accessing the local file system. The <trust> element's level attribute is set to WSS_Minimal by default.
These three trust level can be categorized as below:
A setting with WSS_Minimal trust severely restricts Web Part libraries.
A setting of WSS_Medium trust usually allows for testing and debugging Web Part without security-related problems.
A setting with Full trust completely disables all CAS-related security restrictions
Note:
Keep in mind that a <trust> element's level attribute setting in a web.config file only affects Web Part library DLLs running within the \bin directory. It does not affect Web Part library DLLs that have been installed in the GAC. Web Part library DLLs in the GAC are considered to be fully trusted and always run without any CAS restrictions.
Best Practices for Custom Web Part Deployment
- Use WSS_Medium trust level to enforce security on Web Part Library DLLs.
- Only deploy those Web Part Library DLLs to GAC that are coming from fully trusted source.