簡體   English   中英

使用.Net獲取指定用戶的域組和指定文件的權限

[英]Get domain groups for a specified user and permissions for a specified file using .Net

我想返回特定域用戶的組成員身份列表。 就像是...

string[] UserGroups(string domain, string domainUserName)
{
    // query domain info

    // return a list of accounts the user is a member of
}

另外,我希望能夠查看哪些域帳戶/組有權訪問指定的文件/文件夾。

string[] AllowedAccounts(string domain, string filePath)
{
    // query file/folder permission

    // return a list of accounts with access to file/folder
}

使用c#執行這兩項任務的最佳方法是什么?

這是一個例子。 除非您要過濾,否則您不需要文件訪問功能的域。

string[] UserGroups(string domain, string domainUserName)
{

    WindowsIdentity ident = new WindowsIdentity(domainUserName + "@" + domain);
    List<string> groups = new List<string>();
    foreach (IdentityReference g in ident.Groups)
    {            
        groups.Add(g.Value);
    }
    return groups.ToArray();
}

string[] AllowedAccounts(string filePath)
{
    List<string> accounts = new List<string>();
    FileInfo fInfo = new FileInfo(filePath);
    var fsec = fInfo.GetAccessControl();
    AuthorizationRuleCollection acl = fsec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
    foreach (FileSystemAccessRule ace in acl)
    {
        accounts.Add(ace.IdentityReference.Value);
    }
    return accounts.ToArray();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM