Security exception unhandled when using DirectoryServices

I've recelty started writing c# code so I'm still kinda new at this. Anyway here is the problem. I installed visual studio 2005 and I'm trying to do an active directory bind statement. Here is my code for the bind:

DirectoryEntry entry = new DirectoryEntry(LDAP://mtmercy.edu);

But when this segment of code executes I get the debug error below:

Here is the error:
Request for the permission of type 'System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.

The odd thing is that I tried this same bind code in visual studio 2003 and it worked fine. I'm totaly at a loss in regards to what the difference is here. Any ideas would be appriciated. Thanks.

Oh, and here is the entire debug error just in case:

System.Security.SecurityException was unhandled
Message="Request for the permission of type 'System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed."
Source="createuser"
StackTrace:
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Object assemblyOrString, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Object assemblyOrString, SecurityAction action, Boolean throwException)
at System.Security.CodeAccessSecurityEngine.CheckSetHelper(CompressedStack cs, PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Assembly asm, SecurityAction action)
at createuser.Create_User.button_Create_User_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at createuser.Program.Main()
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()




Answer this question

Security exception unhandled when using DirectoryServices

  • Mark Arnott

    This error occurred when trying to run code that accessed the System.DirectoryServices namespace.

    To work around this, just open up your .NET 2.0 security:

    1. Start the .NET Framework 2.0 Configuration tool (from Administrative Tools).
    2. Expand Runtime Security Policy.
    3. Expand User - Code Groups and right-click on All_Code and select Properties.
    4. On the Membership Condition tab make sure All Code is selected in the dropdownlist.
    5. On the Permission Set tab make sure FullTrust is selected in the dropdownlist.
    6. Click OK
    7. Under Runtime Security Policy, expand Machine - Code Groups and right-click on All_Code and select Properties.
    8. Perform steps 4 - 6.

  • Neo83

    Hi,

    I have the same problem when I use "DirectoryServices" in Infopath vb formcode.

    But my problem didn't solve with your solution, HELP me.

    thanks.

    sasa


  • matt love

    I love you!

    I have been suffering for this problem for 2 days and was pulling my hair out. I can't believe something as simple as a network share was causing my problems. This problem managed to bypass all exception handling and I had no idea what the problem was.

    Thanks for this.

    Ben


  • janfe313

    Hi Gendalf,

    I have ensured all the steps 1 to 8 as mentioned but still getting the same fail error.

    Not sure how to proceed further.

    Any more help !!

    Thanks,

    zullu.


  • saspsnvg111381

    Ok, I think I figured it out. It's always something simple. The code seems to execute correctly when I'm not trying to run the executable from a shared folder on another computer. My "My Documents" folder is mapped to another network location, and of course VS.NET saves everything to "My Documents" by default.


  • Kenneth LeFebvre

    I added this line to my web.config file for the application and it works now

    needs to be in the system.web section

    <trust level="Full"/>


  • atimsa

    Awesome!

    Thank you so much!



  • Security exception unhandled when using DirectoryServices