C# 如何在http处理程序ashx中将文件上载到网络路径?

C# 如何在http处理程序ashx中将文件上载到网络路径?,c#,asp.net,silverlight-4.0,httphandler,C#,Asp.net,Silverlight 4.0,Httphandler,我尝试使用silverlight应用程序的以下代码将文件上载到网络路径: public void ProcessRequest (HttpContext context) { //..... using (FileStream fs = File.Create(@"\\Server\Folder\" + filename)) { byte[] buffer = new byte[4096]; int bytesRead; while ((by

我尝试使用silverlight应用程序的以下代码将文件上载到网络路径:

public void ProcessRequest (HttpContext context) 
{
  //.....
  using (FileStream fs = File.Create(@"\\Server\Folder\" + filename))            
 {
   byte[] buffer = new byte[4096];
   int bytesRead;
   while ((bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
   {
      fs.Write(buffer, 0, bytesRead);
   }
 }
}
当我使用VS内置web服务器在调试模式下运行它时,它工作正常。在SL端,我使用url调用此处理程序,如下所示:

UriBuilder ub = new UriBuilder("http://localhost:38700/FileUpload.ashx");
然后我将此应用发布到本地IIS,并将url更改为
http://localhost/Mysite/FileUpload.ashx

然后再次运行应用程序。它将不再工作,但没有错误

我猜这是因为调用File.Create的凭据不同。所以我想在处理程序中使用特定的凭证将文件放入目标


如何为File.Create使用凭据?

我相信您需要模拟用户。下面的代码应该可以做到这一点。本质上,您可以收集域、用户和密码,并实例化ImpersonationMgr类。然后调用BeginImpersonation方法。然后调用相应的WriteFile方法。如果启用了模拟,WriteFile方法将断言。对于其他文件方法(如删除和移动),您可以遵循类似的模式

public class ImpersonationMgr : IDisposable

  [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType,
                                      int dwLogonProvider, out SafeTokenHandle phToken);

  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  public static extern bool CloseHandle(IntPtr handle);

  private const int LOGON32_PROVIDER_DEFAULT = 0;
  private const int LOGON32_LOGON_NEW_CREDENTIALS = 9;

  private readonly string userName = string.Empty;
  private readonly string domainName = string.Empty;
  private readonly string password = string.Empty;

  private SafeTokenHandle safeTokenHandle = null;
  private WindowsImpersonationContext impersonatedUser = null;

  public ImpersonationMgr(string userName, string domainName, string password)
  {
     this.userName = userName;
     this.domainName = domainName;
     this.password = password;
  }

  public void BeginImpersonation()
  {
     bool returnValue = LogonUser(userName, domainName, password, LOGON32_LOGON_NEW_CREDENTIALS,
                                  LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);
     if (returnValue == false)
     {
        int ret = Marshal.GetLastWin32Error();
        throw new System.ComponentModel.Win32Exception(ret);
     }

     impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle());
  }

  private void AssertImpersonationIsEnabled()
  {
     if(safeTokenHandle == null || impersonatedUser == null)
     {
        throw new UnauthorizedAccessException("You must call the BeginImpersonation method before attempting file write access.");
     }
  }

  public void WriteFile(string pathToFile, string fileContents)
  {
     AssertImpersonationIsEnabled();
     using (FileStream fileStream = File.Open(pathToFile, FileMode.CreateNew))
     {
        using (StreamWriter fileWriter = new StreamWriter(fileStream))
        {
           fileWriter.Write(fileContents);
        }
     }
  }

  public void WriteFile(string pathToFile, byte[] fileContents)
  {
     AssertImpersonationIsEnabled();
     using (FileStream fileStream = new FileStream(pathToFile, FileMode.Create))
     {
        fileStream .Write(fileContents, 0, fileContents.Length);
        fileStream .Flush();          
     }
  }

  public void Dispose()
  {
     Dispose(true);
     GC.SuppressFinalize(this);
  }
}
更新

   public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
   {
      private SafeTokenHandle()
         : base(true)
      {
      }


      [DllImport("kernel32.dll")]
      [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
      [SuppressUnmanagedCodeSecurity]
      [return: MarshalAs(UnmanagedType.Bool)]
      private static extern bool CloseHandle(IntPtr handle);

      protected override bool ReleaseHandle()
      {
         return CloseHandle(handle);
      }
   }

哇!看起来不容易。我试过了。将其放在web项目中,发现SafeTokenHandle不可用。这是针对.NET1/2/3/4的?我将VS2010与.NETFramework4一起使用,并将迁移到4.5.0版本。谢谢。更多问题:句柄的定义在哪里?可以调用Dispose。重试后出现错误:错误2可访问性不一致:参数类型“out SafeTokenHandle”的可访问性不如方法“ImpersonationMgr.LogonUser(string,string,string,int,int,out SafeTokenHandle)”,请将其更改为公共类SafeTokeHandle谢谢。请再试一次。它使用VS内置的Web服务器。但当它部署到IIS时,仍然无法工作。不知道为什么。