ASMX web service issue Communication with a Windows service in a separate thread

There is a Windows service on Windows Server 2003 (Amazon Virtual Machine). Some applications can communicate with him (using channels, but there is a wrapper for this). It is tested and working. In addition, there is a web service written in C # (ASP.NET) that interacts with the aforementioned Windows service. When the Web Method is called, it creates an instance of the class and calls the function - the function "connects" to the Windows service and sends the task to it. But if a stream is created inside the Web method, and a function that "connects" to the Windows service is called inside the stream, the connection fails. A connection to a Windows service uses feeds. The web service runs on IIS7. It is worth mentioning that everything works on my local machine, either from a debugger (a local server running VS 2010) or from IE,when I call a web method on a web service that runs on local IIS7. Everything works on local - but not on Amazon Instance. I am not a web programmer, so I think there is a security issue. Any hint? Thank.

EDIT: Uwe comment reminded me - the web method first tries to upload some files using http and saves them to the path C: \ intetpub \ wwwroot \ files \. It works if files are downloaded from the web method, but the download fails if it is executed from another thread created in the web method. Exception: access denied. Therefore, I changed the security settings in the specified folder and explicitly allowed the user created by IIS7 (IIS_IUSRS) to read / write the folder, and now the files can be downloaded. The source of these problems seems to be the same.

EDIT: The solution moves in response to Will's suggestion.

+3
source share
1 answer

Ok guys, a solution has been found and as Will wishes, I will post it here as an answer to my own question. So the solution is:

, , Web-, . , - :

[WebMethod]
public void Fnc()
{
    ...
    ...
    System.Security.Principal.WindowsIdentity wi =    System.Security.Principal.WindowsIdentity.GetCurrent();
    System.Threading.Thread postJobThread = new Thread(PostJobThread);
    postJobThread.Start(wi);
    ...
}

...

private void PostJobThread(object ob)
{
    System.Security.Principal.WindowsIdentity wi = (System.Security.Principal.WindowsIdentity)ob;
    System.Security.Principal.WindowsImpersonationContext ctx = wi.Impersonate();

    ...
    // Do some job which couldn't be done in this thread
    ...
    // OK, job is finished
    if(ctx != null)
    ctx.Undo();
}

. , , , - .

+2

Source: https://habr.com/ru/post/1782767/


All Articles