繁体   English   中英

安全和自动化的方式来授予ASP.NET C#Web应用程序中的上载文件夹权限?

[英]Secure and Automated way of giving permissions for upload folder in ASP.NET C# web application?

我们的ASP.NET C#Web应用程序将jpg,png,docx,txt等各种文件上传到名为ClientBin的文件夹中。 在Visual Studio 2010 .NET IDE随带的Visual Studio 2010 .NET测试服务器上,一切正常。

但是,如果将应用程序部署到IIS7服务器,则必须授予应用程序的Web用户上载文件的权限。 我们基本上使用IIS7登录到我们的服务器,然后手动修改名为ClientBin的文件夹的Security属性,该文件夹最终应包含jpg,png,docx,txt等内容。

---允许网络用户成功上传工作的手动方法---------------------------

右键单击资源管理器中的projectfolder \\ ClientBin文件夹,选择“属性”,然后选择“安全性”选项卡。 单击“添加”以添加适当的用户或组。 突出显示ASP.NET帐户,然后选中所需访问权限的框。 ---使上传成功工作的手动方法---------------------------

-程序化方法,在尝试上传时仍会给网络用户带来异常错误------------------

String DirectoryPath = System.IO.Path.Combine(Server.MapPath("~/ClientBin/"));
DirectorySecurity specificDirectorySecurity = Directory.GetAccessControl(DirectoryPath);
specificDirectorySecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.Modify, AccessControlType.Allow));
specificDirectorySecurity.AddAccessRule(new FileSystemAccessRule("Administrators", FileSystemRights.Modify, AccessControlType.Allow));
specificDirectorySecurity.AddAccessRule(new FileSystemAccessRule("SYSTEM", FileSystemRights.Modify, AccessControlType.Allow));
Directory.SetAccessControl(DirectoryPath, specificDirectorySecurity);

-程序化方法,在尝试上传时仍会给网络用户带来异常错误------------------

另一个在线帖子建议我通过在web.config中输入以下内容来解决此问题:

----可以通过编程方法解决问题的XML配置--------

身份impersonate =“ true” userName =“ ComputerName \\ Administrator”密码=“ don”

----可以通过编程方法解决问题的XML配置--------

但是,如果使身份假冒为真,我担心安全性问题。

什么是最安全,最自动化(可能意味着编程解决方案)的方式?

谢谢,

新员工

通常,向应用程序授予目录权限,并且该应用程序管理用户对上载文件夹的访问。

所有:

即使我无法弄清楚C#如何修改上传文件夹的权限。

似乎Microsoft Windows PowerShell可以以编程方式修改上载文件夹的权限。

这是通过编程方式修改上传文件夹权限的代码片段:

$computerHostName = [System.Net.Dns]::GetHostName()

#These constants are used to set permissions
$inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"

$propagation = [system.security.accesscontrol.PropagationFlags]::None

$colRights = [System.Security.AccessControl.FileSystemRights]"Modify"

$objType =[System.Security.AccessControl.AccessControlType]::Allow

#(MSDN Docs) The IIS_IUSRS Group has access to all the necessary file and system     resources
# so that an account, when added to this group, can seamlessly act as an application     pool identity.
#  IIS_IUSRS group by default includes the web users that log on to the Perls    Applications. 
#If a web user needs to upload resources to the folder within the Perls Web     Application that
# contains uploaded resource files then we need to ensure that the members of the
# IIS_IUSRS Group have permissions to add resource files to that particular Perls Web      Application upload folder.

#This determines which user is the guest user for IIS.  Windows Vista and 08 use the      IIS_USRS group, Previous version use
#IUSR_[MachineName]



  if ([environment]::osversion.Version.Major -eq 6) {
  $webUser="IIS_IUSRS"


  } else {

     $webUser="IUSR_" + $computerHostName

 }


$clientBinDirectoryPath = "D:\DeployedApplications\" + $umbrellaComponentName + "\" +     $siteWebComponentName + "\" + "ClientBin"

$perlsPivotErrorDirectoryPath = "D:\DeployedApplications\" + $umbrellaComponentName +      "\" + $siteWebComponentName + "\" + "PerlsPivotErrorDirectory"

$aclForClientBinDirectoryPath = Get-Acl $clientBinDirectoryPath


$accessRuleForClientBinDirectoryPath = New-Object     System.Security.AccessControl.FileSystemAccessRule($webUser, $colRights, $inherit,     $propagation, $objType)

$aclForClientBinDirectoryPath.AddAccessRule($accessRuleForClientBinDirectoryPath)

Set-Acl -aclobject $aclForClientBinDirectoryPath $clientBinDirectoryPath

$aclForPerlsPivotErrorDirectoryPath = Get-Acl $perlsPivotErrorDirectoryPath

$accessRuleForPerlsPivotErrorDirectoryPath  = New-Object     System.Security.AccessControl.FileSystemAccessRule($webUser, $colRights, $inherit,     $propagation, $objType)

$aclForPerlsPivotErrorDirectoryPath.AddAccessRule($accessRuleForPerlsPivotErrorDirectoryPath)

Set-Acl -aclobject $aclForPerlsPivotErrorDirectoryPath $perlsPivotErrorDirectoryPath

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM