Asp.net mvc 2 尝试从ASP.NET MVC将文件写入UNC

Asp.net mvc 2 尝试从ASP.NET MVC将文件写入UNC,asp.net-mvc-2,iis-7,identity,impersonation,unc,Asp.net Mvc 2,Iis 7,Identity,Impersonation,Unc,我在IIS 7中的asp.mvc应用程序中运行了一些代码。该代码应该将文件保存到UNC共享。 此函数是通过一些控制器代码调用的,文件路径名为=“\MYSRV\sites\docs\10080003\egg.txt” public void EnsureDocument(string filePathName ,string content,WindowsIdentity identity ) { System.Security.Principal.WindowsImpersonationCo

我在IIS 7中的asp.mvc应用程序中运行了一些代码。该代码应该将文件保存到UNC共享。 此函数是通过一些控制器代码调用的,文件路径名为=“\MYSRV\sites\docs\10080003\egg.txt”

public void EnsureDocument(string filePathName ,string content,WindowsIdentity identity )
{
  System.Security.Principal.WindowsImpersonationContext impersonationContext = null;
  try
  {
    impersonationContext = ((System.Security.Principal.WindowsIdentity)identity).Impersonate();
    File.WriteAllText(filePathName, content);
  }
  finally
  {
    impersonationContext.Undo();
  }
}
来自asp.net mvc控制器的调用如下所示

  // pass running identity
documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent());
//documentSvc.EnsureCaseDocument(filePathname,content,System.Security.Principal.WindowsIdentity)User.Identity);
documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent() ); 
来自NUnit测试的调用如下所示

  // pass running identity
documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent());
//documentSvc.EnsureCaseDocument(filePathname,content,System.Security.Principal.WindowsIdentity)User.Identity);
documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent() ); 
症状是NUnit代码会删除文件,但来自asp.net mvc的调用不会删除文件

**测试1:通过、删除文件** Nunit代码通过标识{AuthType=Keberos,ImpersonationLevel=none,Name=“DOMAIN\Fred Blogs”}发送,这会将文件放到unc上

**测试2:失败,不删除文件** 在web.config中使用impersonate=“true”,并进行调用

documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent());
documentSvc.EnsureCaseDocument(filePathname,content,System.Security.Principal.WindowsIdentity)User.Identity);
asp.net mvc代码通过{AuthType=Keberos,ImpersonationLevel=Delegation,Name=“DOMAIN\Fred Blogs”}发送,文件不会被删除

**测试3:失败,不删除文件** 在web.config中不使用impersonate=“true”并调用和进行调用

documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent());
documentSvc.EnsureCaseDocument(filePathname,content,System.Security.Principal.WindowsIdentity)User.Identity);
asp.net mvc代码通过{AuthType=Negotiate,ImpersonationLevel=Delegation,Name=“DOMAIN\Fred Blogs”}发送,文件不会被删除


NUnit运行的标识是您,而MVC运行的标识很可能是IUSR_…我认为这只是一个安全问题。

是的,这是一个安全问题;在我的开发框中,代码以IIS APPOOL\ASP.NET 4.0的形式运行,在暂存框中,代码以网络服务的形式运行;在web.config中,模拟的目的为true,而代码则不受影响onationContext=((System.Security.Principal.WindowsIdentity)identity).Impersonate();代码将使代码以使用IE浏览器的用户的身份运行。干杯