Impersonation and UNC network resources

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

  1. #1

    Default Impersonation and UNC network resources

    Hi guys -

    I've been getting knotted up with this for a few days now. Originally I was
    in an ASP.NET context (using VFP8 OleDB to access tables located on a
    network fileshare from inside of ASP.NET) but after much simplification and
    reduction, I've reduced my question to a simple .NET console application.
    The two tests I'm using in the console applications are: a) dirInfo on a
    network folder (as per the example) and b) an VFP8 OleDB connection.

    Excuse me for asking this question here in the aspnet.security when it could
    be a more generic problem, but there have been some great minds answering
    here...

    Anyway. I based my code on this excellent sample:
    [url]http://www.netomatix.com/ImpersonateUser.aspx[/url]. It's using LogonUser() to
    impersonate a known DOMAIN\user with appropriate permissions.

    (I know the user credentials are fine, if for no other reason than because
    if I set up ASP.NET to impersonate the user, I have no problems, providing I
    don't cause ASP.NET to recompile the ASPX files. If I update the aspx files,
    I get an exception on the compiler permissions. I guess the impersonated
    user doesn't have appropriate rights.)

    I've stepped through the code and observed two things:

    On my laptop - a Windows XP Pro machine not part of the domain - the
    LogonUser() succeeds, but the directory code:

    System.IO.DirectoryInfo dirInfo = new
    System.IO.DirectoryInfo(@"\\SERVERNAME\sharename\f older");
    Console.WriteLine( dirInfo.Exists );
    string strFolderToCreate = @"\\SERVERNAME\sharename\folder" + "\\" +
    "boogle";
    System.IO.Directory.CreateDirectory(strFolderToCre ate);

    I get:

    False
    Error: Could not find a part of the path "\\SERVERNAME\sharename".

    The Vfp OleDb code generates a "invalid path or filename" exception which I
    recognise from my regular VFP apps when directories aren't visible.

    Interestingly, when I run the console app on a different machine, Windows
    2000 SP3 which *is* on the DOMAIN, the LogonUser() fails with error 1314 -
    "A required privilege is not held by the client".

    I understand that on Wind2k I would have to allow this user account to "act
    as part of the operating system" - which I did, but it didn't seem to make
    any difference. Do I need to reboot???

    But apart from that - why would LogonUser() say it succeeded, but my code is
    still prevented from accessing the resources it needs?

    Question 2 is, I guess, is if this is insolveable, and instead of using
    custom impersonation code in my aspx.cs files, I *have* to use the
    web.config <impersonate true, user, DOMAIN> etc, then why does the recompile
    fail?

    If I'm being an idiot, please let me know.

    Thanks in advance,
    - Colin


    Colin Nicholls Guest

  2. Similar Questions and Discussions

    1. Impersonation Stops Language Resources Working
      (This is a repost from the internationalization group where I am getting no joy) Hi, My ASP.NET application has internationalised string...
    2. Accessing remote network resources from ASP.NET applications
      Hi All Sorry to bring up a beaten-down-to-death topic. But I seem to be running into a wall whatever I try here. To cut a long story short, I...
    3. accessing network resources from Flash
      I built a static site with a flash front end and no back end. It allows e-learning students to click a link to their course on-line. The LCMS...
    4. Help with accessing network resources
      Simply, I wish to return System.IO.File.Exists ("\\myserver_1\myshare\myfolder\myfile.doc") within an ASP.NET web application that sits on...
    5. Access network resources from ASP.NET
      Hi! after extensive research of this topic, I'm still not sure what is "official" way to do it. Task: 1. User submits some request to...
  3. #2

    Default Re: Impersonation and UNC network resources

    > Question 2 is, I guess, is if this is insolveable, and instead of using
    > custom impersonation code in my aspx.cs files, I *have* to use the
    > web.config <impersonate true, user, DOMAIN> etc, then why does the
    > recompile fail?
    Well, I don't know the answer to this, but I found a solution to the problem
    on a web thread posted by shamrox
    ([url]http://forums.rainbowportal.net/ShowPost.aspx?PostID=5503):[/url]

    "Is this error happening when you try to run your project? I guess what I am
    asking is that it compiles fine but when it goes to the first screen you get
    this error. IE would never open if it didn't compile.

    If it is what I think it has to do with the aspnet_wp. It does some weird
    stuff. You don't have to restart www to fix it, all you need to do is go to
    task manager and [terminate the aspnet_wp.exe process]. Don't worry it
    restarts automatically. That will fix the problem for now. It comes up from
    time to time so if there is a better way I would like to know too."

    Thanks shamrox - this works for me. Now I can use the <impersonate ...>
    switch in the web.config in order for my web site to "see" the network
    resources, and when I upload a new version of the aspxs to my web site - it
    will stick on the recompile but a restart of the aspnet_wp.exe will fix it.

    I still don't know the answer to my Question 1 though.

    - Colin



    Colin Nicholls Guest

  4. #3

    Default Re: Impersonation and UNC network resources

    On Wed, 9 Mar 2005 13:07:28 -0800, "Colin Nicholls" <colin@spacefold.com> wrote:

    ¤ > Question 2 is, I guess, is if this is insolveable, and instead of using
    ¤ > custom impersonation code in my aspx.cs files, I *have* to use the
    ¤ > web.config <impersonate true, user, DOMAIN> etc, then why does the
    ¤ > recompile fail?
    ¤
    ¤ Well, I don't know the answer to this, but I found a solution to the problem
    ¤ on a web thread posted by shamrox
    ¤ ([url]http://forums.rainbowportal.net/ShowPost.aspx?PostID=5503):[/url]
    ¤
    ¤ "Is this error happening when you try to run your project? I guess what I am
    ¤ asking is that it compiles fine but when it goes to the first screen you get
    ¤ this error. IE would never open if it didn't compile.
    ¤
    ¤ If it is what I think it has to do with the aspnet_wp. It does some weird
    ¤ stuff. You don't have to restart www to fix it, all you need to do is go to
    ¤ task manager and [terminate the aspnet_wp.exe process]. Don't worry it
    ¤ restarts automatically. That will fix the problem for now. It comes up from
    ¤ time to time so if there is a better way I would like to know too."
    ¤
    ¤ Thanks shamrox - this works for me. Now I can use the <impersonate ...>
    ¤ switch in the web.config in order for my web site to "see" the network
    ¤ resources, and when I upload a new version of the aspxs to my web site - it
    ¤ will stick on the recompile but a restart of the aspnet_wp.exe will fix it.
    ¤
    ¤ I still don't know the answer to my Question 1 though.

    I'm not sure if I understood your first question. If it has to do with accessing a network resource
    the problem is likely one of delegation, which is next step after impersonation.

    If you can confirm what type of application this is we might be able to help.


    Paul ~~~ [email]pclement@ameritech.net[/email]
    Microsoft MVP (Visual Basic)
    Paul Clement Guest

  5. #4

    Default Re: Impersonation and UNC network resources

    "Paul Clement" <UseAdddressAtEndofMessage@swspectrum.com> wrote in message
    news:f0o0311u94hlmv2sor1i8dq34qccc7l765@4ax.com...
    >
    > I'm not sure if I understood your first question. If it has to do with
    > accessing a network resource
    > the problem is likely one of delegation, which is next step after
    > impersonation.
    >
    > If you can confirm what type of application this is we might be able to
    > help.
    Hi Paul -

    The original application was an ASP.NET page (hence posting here) that uses
    OleDb to access Foxpro tables on a network share on a different physical
    machine. I could only get it to work if I used <identity impersonate="true"
    username="DOMAIN\netuser" password="***"/> in the web.config, which had
    other problems.

    I investigated workarounds, including performing impersonation in code
    bracketing the OleDb calls. I couldn't get it to work, and started reducing
    it down to a "simple" console app example.

    My example ended up looking like the sample here:
    [url]http://www.netomatix.com/ImpersonateUser.aspx[/url].

    So, forget the ASP layer; forget OleDb. It uses DirInfo to look at the
    folders. I can repro the problem with that sample.

    The network resource resides on a Win2K server (\\SERVER\Share\Folder) on
    domain DOMAIN.
    There is a known domain user, DOMAIN\netuser, that we want to use to access
    the resource.

    Test 1: Windows XP Laptop, not on the DOMAIN:

    When I run the console app from there, the LogonUser()/impersonation code
    seems to work (no errors, returns true, etc) but:

    DirectoryInfo dirInfo = new DirectoryInfo(@"\\SERVER\Share\Folder");
    Console.WriteLine( dirInfo.Exists );

    False

    string x = @"\\SERVER\Share\Folder" + "\\" + "test";
    Directory.CreateDirectory( x );

    Error: Could not find a part of the path \\SERVER\Share.

    Test 2: Win2k workstation, part of the DOMAIN:

    The LogonUser() fails with error 1314 - "A required privilege is not held by
    the client".

    So question 1 is really two questions:

    -- Why does the LogonUser fail on Windows 2k?
    -- Why, if the LogonUser succeeds, should the network resource still be
    unavailable?

    ....I get the feeling I have re-typed my first post. Sorry for that.

    Thanks in advance,
    - Colin


    Colin Nicholls Guest

  6. #5

    Default Re: Impersonation and UNC network resources

    Did you read the docs on LogonUser? It is pretty clear where it states you
    need the "Act as part of the operating system" privilege to call it under
    Win2K. Generally, only the SYSTEM account has this. You can add this
    privilege to any account you want to, but be very careful about that as it
    significantly compromises the security of your application.

    Another option for you would be using impersonation with Kerberos delegation
    or possibly setting up your process account as an appropriate domain account
    and not using impersonation. Lots of choices.

    Joe K.

    "Colin Nicholls" <colin@spacefold.com> wrote in message
    news:uj6%23qFaJFHA.484@TK2MSFTNGP15.phx.gbl...
    > "Paul Clement" <UseAdddressAtEndofMessage@swspectrum.com> wrote in message
    > news:f0o0311u94hlmv2sor1i8dq34qccc7l765@4ax.com...
    >>
    >> I'm not sure if I understood your first question. If it has to do with
    >> accessing a network resource
    >> the problem is likely one of delegation, which is next step after
    >> impersonation.
    >>
    >> If you can confirm what type of application this is we might be able to
    >> help.
    >
    > Hi Paul -
    >
    > The original application was an ASP.NET page (hence posting here) that
    > uses OleDb to access Foxpro tables on a network share on a different
    > physical machine. I could only get it to work if I used <identity
    > impersonate="true" username="DOMAIN\netuser" password="***"/> in the
    > web.config, which had other problems.
    >
    > I investigated workarounds, including performing impersonation in code
    > bracketing the OleDb calls. I couldn't get it to work, and started
    > reducing it down to a "simple" console app example.
    >
    > My example ended up looking like the sample here:
    > [url]http://www.netomatix.com/ImpersonateUser.aspx[/url].
    >
    > So, forget the ASP layer; forget OleDb. It uses DirInfo to look at the
    > folders. I can repro the problem with that sample.
    >
    > The network resource resides on a Win2K server (\\SERVER\Share\Folder) on
    > domain DOMAIN.
    > There is a known domain user, DOMAIN\netuser, that we want to use to
    > access the resource.
    >
    > Test 1: Windows XP Laptop, not on the DOMAIN:
    >
    > When I run the console app from there, the LogonUser()/impersonation code
    > seems to work (no errors, returns true, etc) but:
    >
    > DirectoryInfo dirInfo = new DirectoryInfo(@"\\SERVER\Share\Folder");
    > Console.WriteLine( dirInfo.Exists );
    >
    > False
    >
    > string x = @"\\SERVER\Share\Folder" + "\\" + "test";
    > Directory.CreateDirectory( x );
    >
    > Error: Could not find a part of the path \\SERVER\Share.
    >
    > Test 2: Win2k workstation, part of the DOMAIN:
    >
    > The LogonUser() fails with error 1314 - "A required privilege is not held
    > by the client".
    >
    > So question 1 is really two questions:
    >
    > -- Why does the LogonUser fail on Windows 2k?
    > -- Why, if the LogonUser succeeds, should the network resource still be
    > unavailable?
    >
    > ...I get the feeling I have re-typed my first post. Sorry for that.
    >
    > Thanks in advance,
    > - Colin
    >
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  7. #6

    Default Re: Impersonation and UNC network resources

    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:eqq6XoeJFHA.1280@TK2MSFTNGP09.phx.gbl...
    > Did you read the docs on LogonUser? It is pretty clear where it states
    > you need the "Act as part of the operating system" privilege to call it
    > under Win2K. Generally, only the SYSTEM account has this. You can add
    > this privilege to any account you want to, but be very careful about that
    > as it significantly compromises the security of your application.
    >
    Yes, thanks, Joe, I tried that. It didn't seem to make any difference. And
    it doesn't explain the WinXp results.
    > Another option for you would be using impersonation with Kerberos
    > delegation or possibly setting up your process account as an appropriate
    > domain account and not using impersonation. Lots of choices.
    Yup, and some learning on my part. Thanks for the pointers.

    - Colin



    Colin Nicholls Guest

  8. #7

    Default Re: Impersonation and UNC network resources

    Another helpful thing is to check the value of
    System.Security.Principal.WindowsIdentity.GetCurre nt().Name to see what the
    identity of the current thread is when you call the code.

    If you change the process account but are impersonating, then the
    impersonated account will be used to call LogonUser. In that case, the
    impersonated account would need the extra privilege.

    Joe K.

    "Colin Nicholls" <colin@spacefold.com> wrote in message
    news:et9H1cfJFHA.3376@TK2MSFTNGP14.phx.gbl...
    > "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    > in message news:eqq6XoeJFHA.1280@TK2MSFTNGP09.phx.gbl...
    >> Did you read the docs on LogonUser? It is pretty clear where it states
    >> you need the "Act as part of the operating system" privilege to call it
    >> under Win2K. Generally, only the SYSTEM account has this. You can add
    >> this privilege to any account you want to, but be very careful about that
    >> as it significantly compromises the security of your application.
    >>
    >
    > Yes, thanks, Joe, I tried that. It didn't seem to make any difference. And
    > it doesn't explain the WinXp results.
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  9. #8

    Default Re: Impersonation and UNC network resources

    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:OHHr5KlJFHA.2356@TK2MSFTNGP14.phx.gbl...
    > Another helpful thing is to check the value of
    > System.Security.Principal.WindowsIdentity.GetCurre nt().Name to see what
    > the identity of the current thread is when you call the code.
    >
    > If you change the process account but are impersonating, then the
    > impersonated account will be used to call LogonUser. In that case, the
    > impersonated account would need the extra privilege.
    Interesting. I'll check this out. I'm fairly sure, though, that because the
    LogonUser() call appears to be successful under WinXp, that the user account
    I expect is, in fact, being used. But I'll check.

    Thanks
    - Colin


    Colin Nicholls Guest

  10. #9

    Default Re: Impersonation and UNC network resources

    Remember that anyone (just about) can call LogonUser on XP or 2003, so you
    aren't likely to see any surprising privilege issues on those OS's. Thus, I
    would expect to see LogonUser work in general on XP, regardless of your
    impersonation settings.

    Joe K.

    "Colin Nicholls" <colin@spacefold.com> wrote in message
    news:u3MDZImJFHA.1096@tk2msftngp13.phx.gbl...
    > "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    > in message news:OHHr5KlJFHA.2356@TK2MSFTNGP14.phx.gbl...
    >> Another helpful thing is to check the value of
    >> System.Security.Principal.WindowsIdentity.GetCurre nt().Name to see what
    >> the identity of the current thread is when you call the code.
    >>
    >> If you change the process account but are impersonating, then the
    >> impersonated account will be used to call LogonUser. In that case, the
    >> impersonated account would need the extra privilege.
    >
    > Interesting. I'll check this out. I'm fairly sure, though, that because
    > the LogonUser() call appears to be successful under WinXp, that the user
    > account I expect is, in fact, being used. But I'll check.
    >
    > Thanks
    > - Colin
    >
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  11. #10

    Default Re: Impersonation and UNC network resources

    Joe -

    Thanks for your suggestions. Very helpful. I added some debug code to report
    the Principal.WindowsIdentity like you suggested. Let me describe what I am
    seeing and see if it makes sense:

    I'm logged into my laptop as LAPTOP\colin. LAPTOP is WindowsXP, not part of
    the domain.
    I run my test program, which attempts to impersonate DOMAIN\netuser.

    Here is the console.WriteLine() debug output that I see:

    Current/Normal user: LAPTOP\colin
    After impersonating: LAPTOP\netuser
    Directory exists: False
    Create folder: Error: Could not find a part of the path \\SERVER\Share.

    (LAPTOP\netuser exists and has the same password as DOMAIN\netuser.)

    I've read and been told that LogonUser() works on local accounts. It seems
    that it ignores the domain part of the account and finds a local user and
    successfully logs in using that.

    I suspect that if LAPTOP were part of the NT DOMAIN, and there was a trust
    relationship thingy in place, then the network resource would be available.

    I ran the same test on the Win2k workstation. Now I see:

    Current/Normal user: DOMAIN\colin
    Error: LogonUser failed with error code : 1314

    In other words, I am logged into the workstation with my NT Domain
    credentials...

    Just for kicks, my Workstation's Local Security Policy setting for "Act as
    part of the operating system" looks like this:

    WORKSTATION\netuser [x]
    WORKSTATION\aspnet [x]
    DOMAIN\colin [x]

    Perhaps instead of trying to impersonate "DOMAIN\netuser", I should be
    trying to impersonate "WORKSTATION\netuser" and then rely on the trust
    relationship thing.

    - Colin


    Colin Nicholls Guest

  12. #11

    Default Re: Impersonation and UNC network resources

    You definitely need to have your laptop be a domain member if you want to
    authenticate a domain account using LogonUser. That user needs to be able
    to login, after all.

    I'm still not sure why your console application is failing on LogonUser with
    1314 if the account you are running under really has "act as OS" privilege.
    Did you log out and log back on after that?

    In general, if you need to access the network share with a domain user's
    privileges, it would make the most sense to be able to use the domain user
    account on the local machine as well.

    Another option would be to set up your ASP.NET processModel on the Win2K box
    to run as the required domain user (and turn off impersonation). Then,
    access to the network share would use its credentials. There are some kbase
    articles on how to set this up correctly.

    Joe K.

    "Colin Nicholls" <colin@spacefold.com> wrote in message
    news:ex5TcemJFHA.2736@TK2MSFTNGP09.phx.gbl...
    > Joe -
    >
    > Thanks for your suggestions. Very helpful. I added some debug code to
    > report the Principal.WindowsIdentity like you suggested. Let me describe
    > what I am seeing and see if it makes sense:
    >
    > I'm logged into my laptop as LAPTOP\colin. LAPTOP is WindowsXP, not part
    > of the domain.
    > I run my test program, which attempts to impersonate DOMAIN\netuser.
    >
    > Here is the console.WriteLine() debug output that I see:
    >
    > Current/Normal user: LAPTOP\colin
    > After impersonating: LAPTOP\netuser
    > Directory exists: False
    > Create folder: Error: Could not find a part of the path \\SERVER\Share.
    >
    > (LAPTOP\netuser exists and has the same password as DOMAIN\netuser.)
    >
    > I've read and been told that LogonUser() works on local accounts. It seems
    > that it ignores the domain part of the account and finds a local user and
    > successfully logs in using that.
    >
    > I suspect that if LAPTOP were part of the NT DOMAIN, and there was a trust
    > relationship thingy in place, then the network resource would be
    > available.
    >
    > I ran the same test on the Win2k workstation. Now I see:
    >
    > Current/Normal user: DOMAIN\colin
    > Error: LogonUser failed with error code : 1314
    >
    > In other words, I am logged into the workstation with my NT Domain
    > credentials...
    >
    > Just for kicks, my Workstation's Local Security Policy setting for "Act as
    > part of the operating system" looks like this:
    >
    > WORKSTATION\netuser [x]
    > WORKSTATION\aspnet [x]
    > DOMAIN\colin [x]
    >
    > Perhaps instead of trying to impersonate "DOMAIN\netuser", I should be
    > trying to impersonate "WORKSTATION\netuser" and then rely on the trust
    > relationship thing.
    >
    > - Colin
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  13. #12

    Default Re: Impersonation and UNC network resources

    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:%23lAxjPnJFHA.3376@TK2MSFTNGP14.phx.gbl...
    > You definitely need to have your laptop be a domain member if you want to
    > authenticate a domain account using LogonUser. That user needs to be able
    > to login, after all.
    Yeah, that's how I figure it. I was just curious as to explain what I was
    seeing, but I think I have that straight now, thanks!
    > I'm still not sure why your console application is failing on LogonUser
    > with 1314 if the account you are running under really has "act as OS"
    > privilege. Did you log out and log back on after that?
    Um, I think so, but I want to do more tests before I say for sure <grin>.
    > Another option would be to set up your ASP.NET processModel on the Win2K
    > box to run as the required domain user (and turn off impersonation). Then,
    > access to the network share would use its credentials. There are some
    > kbase articles on how to set this up correctly.
    Yes, I understand. I have the ASP.NET app working now (with <identity
    impersonation/>) so the urgency is not as much as it was. I understand the
    processModel would be another way to go that would probably work as well.

    I will persevere with the simple console impersonation to make sure I
    understand what is going on.

    Thanks for all your help, I appreciate it.


    Colin Nicholls 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