簡體   English   中英

檢查本地用戶所屬的組

[英]Check for groups a local user is a member of

我有代碼來獲取本地組示例管理員的成員

private void GetUserGrps()
{
    using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))
    {
        foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
        {
            using (DirectoryEntry memberEntry = new DirectoryEntry(member))
            {
                new GUIUtility().LogMessageToFile(memberEntry.Path);
            }
        }
    }

有沒有辦法讓本地用戶所屬的組使用目錄服務?

不使用Active Directory或域,因為我只需要本地計算機而不是域。

試試這個

using System.DirectoryServices.AccountManagement;

static void Main(string[] args)
{
    ArrayList myGroups = GetUserGroups("My Local User");
}
public static ArrayList GetUserGroups(string sUserName)
{
    ArrayList myItems = new ArrayList();
    UserPrincipal oUserPrincipal = GetUser(sUserName);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

public static UserPrincipal GetUser(string sUserName)
{
    PrincipalContext oPrincipalContext = GetPrincipalContext();

    UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
    return oUserPrincipal;
}
public static PrincipalContext GetPrincipalContext()
{
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Machine);
    return oPrincipalContext;
}

暫無
暫無

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

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