【问题标题】:How to give Folder Permission for IIS User in C#?如何在 C# 中为 IIS 用户授予文件夹权限?
【发布时间】:2013-12-06 23:23:11
【问题描述】:

我需要为 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..

但这也不起作用,但它正在抛出 "部分或全部身份引用无法翻译"

注意:我不想手动设置权限

请有人帮我解决这个问题..?

【问题讨论】:

  • 运行应用程序的用户帐户是否有权设置此类权限?

标签: c# asp.net iis folder-permissions


【解决方案1】:
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");
        }
    }

【讨论】:

    【解决方案2】:

    基于Application Pool Identities 文章:

    IIS 在 Service Pack 2 (SP2) 中引入了一项新的安全功能 Windows Server 2008 和 Windows Vista。它被称为应用程序池 身份。应用程序池标识允许您运行应用程序 一个唯一帐户下的矿池,无需创建和管理 域或本地帐户。应用程序池帐户的名称 对应应用程序池的名称。

    Here 很好地解释了发生的事情:

    在 Windows 7 中,IIS 应用程序池隔离尚未达到 不同的层次。 IIS7 中引入的新变化(Windows Server 2008) 是作为 AppPoolIdentiy 运行应用程序池的新选项。 但是,IIS7 中应用程序池标识的默认设置仍然存在 同样——网络服务。在 IIS7.5 中,AppPoolIdentiy 变为 默认。因此,脚本以前期望其权限 应用程序池标识设置为“NT Service\NetworkService” 现在必须为“IIS AppPool\”设置权限(ACL)——为每个新应用程序池创建的用户帐户。

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

    【讨论】:

    • 感谢您的回复..但我什至使用了 IIS AppPool\DefaultAppPool ...但仍然无法正常工作..
    猜你喜欢
    • 2011-08-31
    • 2012-07-23
    • 1970-01-01
    • 2019-06-18
    • 2012-04-23
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多