Impersonation, DirectoryServices

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Re: Impersonation, DirectoryServices

    Do you get an UnauthorizedAccessException from the InnerException from the
    TargetInvocationException that gets thrown or something else?

    SetPassword can be very tricky to get working unfortunately as it fails for
    many different reasons. Hopefully we can solve this.

    Joe K.

    "idstam" <idstam@discussions.microsoft.com> wrote in message
    news:48393E8E-5559-4F80-AD94-27C9CFF60780@microsoft.com...
    > Hi
    >
    > I'm trying to set the password for a user in AD, but I get Access Denied.
    >
    > I don't use the logged on users credentials when I add the user to AD,
    > instead I supply username and password when I get the initial
    > DirectoryEntry.
    >
    > I am NOT a user in the domain.
    >
    > When I run the code from a console-app, or an asp.net-app without
    > impersonation it works. Even if I impersonate myself in asp.net it fails.
    >
    > Does anyone have a clue?
    >
    > /johan/
    >
    >
    > Here's the code:
    >
    > private DirectoryEntry getRoot()
    > {
    > DirectoryEntry root = null;
    > try
    > {
    > AuthenticationTypes authType =
    > AuthenticationTypes.Secure|AuthenticationTypes.Sea ling;
    > //AuthenticationTypes.ServerBind;
    > root = new DirectoryEntry(_url + "/" + _root,_adAdmin,_password,
    > authType);
    > return root;
    > }
    > catch
    > {
    > }
    > finally
    > {
    >
    > }
    > return root;
    > }
    >
    >
    > public bool addUser(string upn)
    > {
    >
    >
    > bool ret = true;
    >
    > DirectoryEntry användare = null;
    >
    > string clientName = "Acme";
    > string clientRoot = "OU=" + clientName + ",OU=Clients";
    > string cn = upn;
    > string användarRoot = "CN=" + cn;
    > string initialPassword = "AQaq12#¤";
    >
    > DirectoryEntry root = getRoot();
    >
    > DirectoryEntry client = null;
    > DirectoryEntry domainUsers = null;
    > DirectoryEntry applicationUsers = null;
    >
    > try
    > {
    >
    > client = root.Children.Find(clientRoot);
    >
    > användare = client.Children.Add("CN=" + cn, "user");
    > användare.Properties["sAMAccountName"].Value = createSamAccountName(upn);
    > användare.Properties["userPrincipalName"].Value = upn;
    > användare.CommitChanges();
    >
    > användare.Properties["userAccountControl"].Value = 0x200;
    > användare.Invoke("SetPassword", new object[]{initialPassword});//*****
    > THIS IS THE WHERE IT FAILS *****//
    > användare.CommitChanges();
    >
    >
    >
    > }
    > catch(Exception ex)
    > {
    > Debug.WriteLine(ex);
    >
    >
    > throw;
    > }
    > finally
    > {
    > if(användare != null) användare.Dispose();
    > if(client != null) client.Dispose();
    > if(domainUsers != null) domainUsers.Dispose();
    > if(applicationUsers != null) applicationUsers.Dispose();
    >
    > root.Dispose();
    > }
    > return ret;
    > }
    >
    > private string createSamAccountName(string upn)
    > {
    > string ret = upn.Replace("@", "").Replace(".","").Replace("-",
    > "").Replace("_","");
    > if(ret.Length > 19)
    > {
    > return ret.Substring(0,19);
    > }
    > else
    > {
    > return ret;
    > }
    >
    > }
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  2. Similar Questions and Discussions

    1. DirectoryServices Namespace
      Hi I am trying to import the System.DirectoryServices namespace in an aspx page with the following syntax <%@ Import...
    2. Sytem.DirectoryServices
      Add an assembly reference to it first. Joe K. "prakash s via .NET 247" <anonymous@dotnet247.com> wrote in message...
    3. System.DirectoryServices
      I have a few pages which authenticate a user to our site. Checking a login and password with syntax as below: Dim entry As New...
    4. DirectoryServices error!
      Hi all, I encounter some problem in DirectoryServices, could someone help me? private void InitLoginUser() { DirectoryEntry objEntry = new...
    5. ASP.NET + DirectoryServices
      Hi all, Could I use DirectoryServices in ASP.NET? Or could you give sample code? Thanks! Best Regards Enosh Chang
  3. #2

    Default Re: Impersonation, DirectoryServices

    If you are supplying user name and pwd, then ADSI shouldn't be using
    anything from the impersonated security context UNLESS you aren't supplying
    a server name in your binding string. I have no idea why that would matter
    in this case, but you might try adding an explicit server to your LDAP path
    in your DirectoryEntry constructor and see if that fixes it.

    Joe K.

    "idstam" <idstam@discussions.microsoft.com> wrote in message
    news:3A924F5F-4A41-48B8-BF6C-61DF846E3A65@microsoft.com...
    > Thanks for the response, if someone's gonna solve it I guess it'll be you
    > :)
    >
    > That's exactly the exception I get.
    >
    > Since it works when I run the code in a console app or in asp.net without
    > impersonation I thought there might be a local security setting on some
    > file
    > containing the underlying COM-object.
    >
    > I want to emphasize that I'm not trying to impersonate the user that has
    > write permissions in AD. I'm supplying the username and password in all 3
    > cases.
    >
    > I forgot to mention that the computer I run the code on is a Win XP that
    > is
    > not part of a domain and AD is on a Win 2003 where it is the primary
    > domain
    > controller.
    >
    >
    > best regards
    >
    > /johan/

    Joe Kaplan \(MVP - ADSI\) Guest

  4. #3

    Default Re: Impersonation, DirectoryServices

    My initial DirectoryEntry is fetched lite this:

    My binding string looks like this :
    "LDAP://server.department.company.se/DC=department,DC=company,DC=se"

    i guess that means that I've supplied the server name.

    I can't figure out why the impersonate setting in my web.config should
    affect this.

    /johan/

    "Joe Kaplan (MVP - ADSI)" wrote:
    > If you are supplying user name and pwd, then ADSI shouldn't be using
    > anything from the impersonated security context UNLESS you aren't supplying
    > a server name in your binding string. I have no idea why that would matter
    > in this case, but you might try adding an explicit server to your LDAP path
    > in your DirectoryEntry constructor and see if that fixes it.
    >
    > Joe K.
    >
    idstam Guest

  5. #4

    Default Re: Impersonation, DirectoryServices

    Yeah, I'm not sure either unless the variable you have set for the password
    is a null string or null reference.

    Sorry I'm not too helpful,

    Joe K.

    "idstam" <idstam@discussions.microsoft.com> wrote in message
    news:A9C4FFF7-DC2B-4170-AD53-11A94E748A65@microsoft.com...
    > My initial DirectoryEntry is fetched lite this:
    >
    > My binding string looks like this :
    > "LDAP://server.department.company.se/DC=department,DC=company,DC=se"
    >
    > i guess that means that I've supplied the server name.
    >
    > I can't figure out why the impersonate setting in my web.config should
    > affect this.
    >
    > /johan/
    >
    > "Joe Kaplan (MVP - ADSI)" wrote:
    >
    >> If you are supplying user name and pwd, then ADSI shouldn't be using
    >> anything from the impersonated security context UNLESS you aren't
    >> supplying
    >> a server name in your binding string. I have no idea why that would
    >> matter
    >> in this case, but you might try adding an explicit server to your LDAP
    >> path
    >> in your DirectoryEntry constructor and see if that fixes it.
    >>
    >> Joe K.
    >>
    >

    Joe Kaplan \(MVP - ADSI\) Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139