【问题标题】:Check if a given user is in the security group of a given path检查给定用户是否在给定路径的安全组中
【发布时间】:2014-08-28 14:25:04
【问题描述】:

我有一份简单的工作,但我不知道如何完成,随着搜索的深入,我迷失得更深。

我需要编写一个方法,通过给定的文件夹路径返回给定用户的FileSystemAccessRule(我得到了他的 samAccountName,objectGUID)。

我已经完成了向路径添加或删除 FileSystemAccessRule 的操作,如下所示:

var fSecurity = Directory.GetAccessControl(physicalPath);

fSecurity.AddAccessRule(new FileSystemAccessRule(samAccountName, FileSystemRights.FullControl, AccessControlType.Allow));
fSecurity.RemoveAccessRule(new FileSystemAccessRule(samAccountName, FileSystemRights.FullControl, AccessControlType.Allow));

Directory.SetAccessControl(physicalPath, fSecurity);

检查给定用户是否对路径具有某种特定的访问权限是一项类似的工作?还是应该走别的路? DirectoryEntryLDAPActive Directory 之类的?

我想要的是一个可能看起来像这样的方法:

FileSystemAccessRule[] GetAccessRulesOfTheUserOverPath(string samAccountName, string folderPath)
{
    /// how?
}

【问题讨论】:

    标签: security active-directory directory ldap filesystems


    【解决方案1】:

    感谢 SO 上的一些答案,我想出了一个答案。虽然这不是我问题的确切答案,但它满足了我的需求。在this question 的回答中,我找到了解决方案。此解决方案告诉我给定的 FileSystemRights 是否绑定到给定文件夹的 acl(AuthorizationRuleCollection) 上的当前 Windows 用户。

    我提到的问题中的几乎所有答案都给出了结果,我认为最准确的答案是@Olivier Jacot-Descombes 的答案,因为它计算允许规则、拒绝规则和继承规则的优先级.

    所以我做的是这样的:

    WindowsIdentity _currentUser;
    WindowsPrincipal _currentPrincipal;
    
    using ( new Impersonator(userName, passwordOfTheUser) )
    {
        _currentUser = WindowsIdentity.GetCurrent();
        _currentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    }
    
    if ( !Directory.Exists(path) ) throw new Exception("Directory does not exist");
    
    var di = new DirectoryInfo(path);
    
    var directoryACLs = di.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
    
    ///rw_accessRules list consists of the rules for ReadWrite permissons.
    bool auth_RW = rw_accessRules.All(aR => HasFileOrDirectoryAccess(_currentUser, _currentPrincipal, aR, directoryACLs)); 
    

    这里是“HasFileOrDirectoryAccess”方法:

    bool HasFileOrDirectoryAccess ( WindowsIdentity _currentUser, WindowsPrincipal _currentPrincipal, FileSystemRights right, AuthorizationRuleCollection acl )
    {
        bool allow = false;
        bool inheritedAllow = false;
        bool inheritedDeny = false;
    
        foreach ( FileSystemAccessRule currentRule in acl )
        {
            // If the current rule applies to the current user.
            if ( _currentUser.User.Equals(currentRule.IdentityReference) || _currentPrincipal.IsInRole((SecurityIdentifier)currentRule.IdentityReference) )
            {
                if ( currentRule.AccessControlType.Equals(AccessControlType.Deny) )
                {
                    if ( ( currentRule.FileSystemRights & right ) == right )
                    {
                        if ( currentRule.IsInherited )
                        {
                            inheritedDeny = true;
                        }
                        else
                        { // Non inherited "deny" takes overall precedence.
                            return false;
                        }
                    }
                }
                else if ( currentRule.AccessControlType.Equals(AccessControlType.Allow) )
                {
                    if ( ( currentRule.FileSystemRights & right ) == right )
                    {
                        if ( currentRule.IsInherited )
                        {
                            inheritedAllow = true;
                        }
                        else
                        {
                            allow = true;
                        }
                    }
                }
            } 
        }
    
    
        if ( allow )
        { // Non inherited "allow" takes precedence over inherited rules.
            return true;
        }
        return inheritedAllow && !inheritedDeny;
    }
    

    我首先模拟给定用户,获取他的主体和身份,然后检查他是否具有给定规则集的权限。

    这适用于我的情况,但您会注意到我们需要我们要检查权限的用户的密码。如果有任何方法可以在没有密码的情况下执行此操作,那就太好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多