I have developed a custom search and successfully deployed in our SharePoint 2007 (We have almost 16 sites and one document library, so we decided to filter the search according to the location, I will post the step by step development to the custom search later :)). My manager asked me to pick the location (ie. Organizational Unit (OU)) from Active Directory (AD) according to the user dynamically and I have written a method to pick the OU, you can see the post here. So far I didn’t get any problem but when I tried to deploy the web part to the SharePoint, I have got several errors and I managed to solve them but the following error gave me very hard time.
Security Exception: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application’s trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request for the permission of type ‘System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ failed.
Source Error:

Stack Trace:

I tried several solutions to fix this issue but no luck, because my C# code is trying to access the AD information which is in the different server, so there is no trusted permission from SharePoint, but I could access the details form the local machine. Finally I just read the error message carefully and modified the Trust Level in the SharePoint web.config to “Full” then it was working.

Please note: Changing this Trust Level might harm your SharePoint Security!
To do the changes:
Step1: Open the web.config file (Using Notepad OR Visual Studio)
Step2: Locate the <trust level=”WSS_Minimal” originUrl=”” /> tag in the web. config file. The default value is “WSS_Minimal”. I tried “WSS_Medium” but it didn’t solve the problem, sometimes it will solve yours.

Step3: Change the <trust level=”Full” originUrl=”” />

That’s all, it is working now. Happy coding n SharePointing! There are some other solutions for this related issue, you can try.
1. Changes on the .NET Framework 2.0 Configuration tool, Tim Huffam’s blog will show how to do the work around.
References:
1. http://support.microsoft.com/kb/555466
2. http://us.generation-nt.com/answer/using-system-directoryservices-sharepoint-webpart-help-35022312.html
3. http://geekswithblogs.net/timh/archive/2006/03/08/71714.aspx