【问题标题】:Trying to write file to UNC from ASP.NET MVC尝试从 ASP.NET MVC 将文件写入 UNC
【发布时间】:2011-04-13 07:09:35
【问题描述】:

我在 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);

来自 NUnit 测试的调用如下所示...

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());

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" } 发送并且文件没有被丢弃。

?

【问题讨论】:

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


    【解决方案1】:

    NUnit的运行身份是你,而MVC的运行身份很可能是IUSR_...我认为这只是一个安全问题。

    【讨论】:

    • 是的,这是一个安全问题;在我的开发盒上,代码作为 IIS APPOOL\ASP.NET 4.0 运行,在暂存盒上,代码作为 NETWORK SERVICE 运行; web.config中impersonate=true的用途和代码impersonationContext = ((System.Security.Principal.WindowsIdentity)identity).Impersonate();代码是让代码以使用IE浏览器的用户身份运行。欢呼
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2020-08-22
    • 2016-02-05
    • 1970-01-01
    相关资源
    最近更新 更多