开发者

ASP.NET Access Denied in separate Thread

// test.ashx
public class test : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            File.WriteAllText(@"D:\inetpub\site.net\www\gs\data\test.txt", "Test");
            Thread t = new Thread(write);
            t.Start();
            context.Response.Write("OK");
        }
        catch (Exception ex) 
        {
            context.Response.Write(ex.Message);
        }
    }

    private void write()
    {
        try
        {
           File.WriteAllText(@"D:\inetpub\site.net\www\gs\data\test2.txt", "Test2");
        }
        catch (Exception ex)
        {
            LogManager.Instance.Write(ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

I have a simple ashx file that writes two text files to the disk of server. One is written directly and another is written in a separate thread.

The problem is, File.WriteAllText method which is executed in s开发者_如何学Ceparate thread causes an Access Denied exception. In other words "test.txt" is written but "test2.txt" causes Access Denied exception.

Is there a way to give a thread Read/Write rights of main Session thread?


The thread that executes your ASP.NET request is one of the I/O threads or one of the workerThreads in the ASP.NET worker process (aspnet_wp.exe). These threads are all Multi-Threaded Apartment (MTA) threads. If you programmatically impersonate an account in your .aspx or .asmx code, or if you impersonate by using in Web.config or Machine.config, then the impersonation token is held on this MTA thread. If you then make a call into a single-threaded or an apartment-threaded COM component, that component is accessed by an entirely different thread, which is the single thread in its Single-Threaded Apartment (STA). Because this STA thread does not have an impersonation token of its own, and the impersonation token of the MTA thread is not passed to the STA thread, the STA thread then executes under the process identity.

In order to solve this problem, take a look here : http://support.microsoft.com/kb/325791#top

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜