2013-11-22 55 views
1

我需要为IIS用户提供文件夹权限。
其实我写了这样的代码..如何在C#中为IIS用户提供文件夹权限?

public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights,AccessControlType ControlType) 
{ 
    DirectoryInfo dInfo = new DirectoryInfo(FileName); 
    DirectorySecurity dSecurity = dInfo.GetAccessControl(); 
    dSecurity.AddAccessRule(
     new System.Security.AccessControl.FileSystemAccessRule(objUser, Rights, ControlType)); 
    dInfo.SetAccessControl(dSecurity); 
} 

我调用这个上面这样的方法......

void givepermission() 
{ 
    DirectoryInfo a = new DirectoryInfo(Server.MapPath("~/resources")); 
    AddDirectorySecurity(Server.MapPath("~/"), "IUSR", FileSystemRights.FullControl,AccessControlType.Allow); 
} 

但是局部的工作。当服务器不工作时。

相反IUSR我试过以下帐户名,但也没有工作..


IIS_IUSRS
IIS_WPG
网络服务
大家
等。

相反IIS_IUSRS。我试过这样也...

System.Environment.MachineName + "\\IIS_IUSRS" 

IIS_IUSRS_System.Environment.MachineName 

System.Environment.UserDomainName + "\\IIS_IUSRS" 

etc.. 

但是这也没有工作,但它抛出 “一些或全部身份引用不能被翻译成”

注:我不想手动设置权限

请有人可以帮我这个..?

+1

用户帐户应用程序在运行时是否有权限设置这些权限? –

+0

这是IIS 7.5吗? –

回答

4

基础上Application Pool Identities文章:

IIS介绍 的Windows Server 2008的Service Pack 2(SP2)和Windows Vista的新安全功能。它被称为应用程序池 身份。应用程序池标识允许您在独特帐户下运行应用程序 池,而无需创建和管理 域或本地帐户。应用程序池帐户名称 对应于应用程序池的名称。

Here的内容做一个很好的解释:

在Windows 7中,IIS应用程序池隔离被带到没有一个 不同的级别。在IIS7中引入的新更改(Windows Server 2008)是一种新的选项,可以将应用程序池作为AppPoolIdentiy运行。 但是,IIS7中应用程序池标识的默认值仍然是 - NetworkService。在IIS7.5中,AppPoolIdentiy默认为 。因此,之前将其 应用程序池标识的权限设置为“NT Service \ NetworkService” 的脚本现在必须为“IIS AppPool \”(为每个新应用程序池创建的用户帐户)设置权限(ACL)。

因此,要设置DefaultAppPool的权限,脚本将 需要为“IIS AppPool \ DefaultAppPool”设置ACL。

+1

谢谢你的答复..但我甚至使用IIS AppPool \ DefaultAppPool也...但仍然无法正常工作.. – msgopalkrish

0
public static void FolderPermission(String accountName, String folderPath) 
    { 
     try 
     { 

      FileSystemRights Rights; 

      //What rights are we setting? Here accountName is == "IIS_IUSRS" 

      Rights = FileSystemRights.FullControl; 
      bool modified; 
      var none = new InheritanceFlags(); 
      none = InheritanceFlags.None; 

      //set on dir itself 
      var accessRule = new FileSystemAccessRule(accountName, Rights, none, PropagationFlags.NoPropagateInherit, AccessControlType.Allow); 
      var dInfo = new DirectoryInfo(folderPath); 
      var dSecurity = dInfo.GetAccessControl(); 
      dSecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out modified); 

      //Always allow objects to inherit on a directory 
      var iFlags = new InheritanceFlags(); 
      iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; 

      //Add Access rule for the inheritance 
      var accessRule2 = new FileSystemAccessRule(accountName, Rights, iFlags, PropagationFlags.InheritOnly, AccessControlType.Allow); 
      dSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule2, out modified); 

      dInfo.SetAccessControl(dSecurity); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error"); 
     } 
    } 
相关问题