尝试从ASP.NET MVC向UNC写入文件

时间:2010-09-16 15:36:15

标签: asp.net-mvc-2 iis-7 identity impersonation unc

我在IIS 7中的asp.mvc应用程序中运行了一些代码。该代码应该将文件保存到UNC共享。 从某些控制器代码调用此函数,filePathname =“\ 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);

来自NUnit测试的调用看起来像这样......

documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent() ); 

症状是NUnit代码丢弃文件但是来自asp.net mvc的调用不会丢弃文件。

**测试1:PASSES,DROPS FILE ** Nunit代码通过身份{AuthType = Keberos,ImpersonationLevel = none,Name =“DOMAIN \ Fred Blogs”}发送,并将文件放在unc上。

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

documentSvc.EnsureDocument(filePathname, content, WindowsIdentity.GetCurrent());

asp.net mvc代码通过{AuthType = Keberos,ImpersonationLevel = Delegation,Name =“DOMAIN \ Fred Blogs”}发送,文件不会被删除。

**测试3:失败,不会丢失文件** 在web.config中没有impersonate =“true”并且调用并进行调用

documentSvc.EnsureCaseDocument(filePathname,content,System.Security.Principal.WindowsIdentity)User.Identity);

asp.net mvc代码通过{AuthType = Negotiate,ImpersonationLevel = Delegation,Name =“DOMAIN \ Fred Blogs”}发送,文件不会被删除。

1 个答案:

答案 0 :(得分:0)

NUnit的运行标识就是你,而MVC的运行标识可能是IUSR _...我认为这只是一个安全问题。