Web service security, impersonation

Last post 01-22-2008 4:19 AM by XiaoYong Dai – MSFT. 1 replies.

Sort Posts:

  • Web service security, impersonation

    01-18-2008, 1:51 PM
    • Member
      point Member
    • thanhhuynh
    • Member since 10-02-2007, 9:35 PM
    • Posts 16

    I have an ASP.NET page calling a web service (using javascript).  Both the aspx and asmx web service are hosted in the IIS under the same web site. The web service has a method to determine if a shared folder exists on the network.  To avoid long delay and potential hang up if the remote machine doesn't exist, the web service spawns a thread to check for the folder and timeout after 1 second.  The method always fails when running in IIS so I think it must be security reason. 

    1) Enable impersonation to use my login and get rid of the thread in the web service.

    My Web.config:

    <identity impersonate="true" userName="MyMachine\XYZ" password="xxxxxxx"/> 

    My web service method:

            [WebMethod]
            public bool PathExists(string path)
            {
                 return Directory.Exists(path);
            }
     

    The change corrects the problem. But I do want the the timeout so I put the thread back into the web service. As soon as I do that, the code fails again.  I read an article that says worker threads use the default ASP.NET account unless I programatically change it.  So I tried to add impersonation code into the thread but it stills fails.

              [WebMethod]
            public bool PathExists(string path)
            {

                  bool exists = false;
     

                     Thread t = new Thread(delegate()
                                          {
                                              System.Security.Principal.WindowsIdentity id= System.Security.Principal.WindowsIdentity.GetCurrent();
                                              Log( "WindowsIdentity = " + id.Name);
                                              id.Impersonate();
                                              exists = Directory.Exists(dir);
                                          });

                    t.Start();
                    t.Join(TIMEOUT);
                    t.Abort();

                  return exists;

            } 


     To find out why, I log the WindowsIdentity that the web page uses. Lo and behold they are different

    ASPX page: WindowsIdentity = what I specify in the web config impersonation.

    ASMX web service: WindowsIdentity = MyMachine\ASPNET

    This explains why the code fails to access to the folder on another machine.

     
    Am I missing anything? (I have same same account on the other machine and it is granted permission to the shared folder).  How can I force the web service to a different account? (adding <authentication mode="Windows"> into web.config doesn't help)

  • Re: Web service security, impersonation

    01-22-2008, 4:19 AM
    Answer

    thanhhuynh:

    So I tried to add impersonation code into the thread but it stills fails.

              [WebMethod]
            public bool PathExists(string path)
            {

                  bool exists = false;
     

                     Thread t = new Thread(delegate()
                                          {
                                              System.Security.Principal.WindowsIdentity id= System.Security.Principal.WindowsIdentity.GetCurrent();
                                              Log( "WindowsIdentity = " + id.Name);
                                              id.Impersonate();
                                              exists = Directory.Exists(dir);
                                          });

                    t.Start();
                    t.Join(TIMEOUT);
                    t.Abort();

                  return exists;

            } 

    Hi

    I've made some local test on my box(Win2003, VS2008) and it works well (returns True), the only differentia is that my current Windows Identity is " NT AUTHORITY\NETWORK SERVICE"

    thanhhuynh:

    To find out why, I log the WindowsIdentity that the web page uses. Lo and behold they are different

    ASPX page: WindowsIdentity = what I specify in the web config impersonation.

    ASMX web service: WindowsIdentity = MyMachine\ASPNET

    If you access the web service using asmx page, it will be the account of your authenticated user when integrated windows authentication is enabled. But now it's the aspx page which delivered the credentials when request Web service. so you need to specify a username/password in web.config or it will be a double-hop issue.

    Best Regards
    XiaoYong Dai
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (2 items)