In this post, we will solve this server error Security Exception: the application attempted to perform an operation not allowed by the security policy
Signing a Strong Key to an existing DLL
When I tried to add a custom Data Calendar Control in SharePoint WebPart,I got this error DLL referenced file not had Strong Key which means the referenced DLL is not trusted and not signed with a strong key.Thence,
- I followed the mention steps in this article Signing a Strong Key to an existing DLL that you don’t have its source code to sign “DataCalender.dll” with a strong key and it had been signed properly.
- I copied the DataCalender.dll to C:\inetpub\wwwroot\wss\VirtualDirectories[port]_app_bin folder of SharePoint Web Application.
- I opened Visual Studio > Open SharePoint Solution > Solution Explorer > Add reference for DataCalender.dll that located in the Bin folder of SharePoint Web Application.
- I deployed the SharePoint Solution that has been deployed successfully.
But when I tried to add the Visual WebPart to a SharePoint Page I got this error
Security Exception: the application attempted to perform an operation not allowed by the security policy
The application attempted to perform an operation not allowed by the security policy
This issue usually occurs because the current web application / SharePoint solution does not have the full trust level to run this Web Part.
Solution
To solve this issue,you should follow the mentioned steps below one by one :
- Install DataCalender.dll (your DLL file) with a strong key to GAC. so It will then get full trust.
- Go to the web.config of the SharePoint site and add it to the trusted list.
- Make sure that the DLL file is also located at the below path
- C:\inetpub\wwwroot\wss\VirtualDirectories[port]_app_bin folder.
- Temporary Change the Trust tag in web.config <trust level=”WSS_Minimal” originUrl=”” /> to <trust level=”Full” originUrl=”” />.
- Deploy your Visual WebPart, Great the error is gone.
- Go back to rollback the trust tag in web.config file from <trust level=”Full” originUrl=”” /> to <trust level=”WSS_Minimal” originUrl=”” />.