63

最初は、グループが「IT」の場合、ユーザー名が Active Directory の IT グループにあるため、正しく機能するため、以下のコードが機能すると考えました。私が学んだことは、IT グループに自分のユーザー名があるかどうかに関係なく、常に true を返し、自分が所属している他のグループに変更すると、常に false を返すということです。どんな助けでも大歓迎です。

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // tab control security for admin tab
        bool admin = checkGroup("IT");

        if ((admin == true) && (tabControl1.SelectedTab == tpHistory))
        {
            tabControl1.SelectedTab = tpHistory;
        }
        else if ((admin == false) && (tabControl1.SelectedTab == tpHistory))
        {
            tabControl1.SelectedTab = tpRequests;
            MessageBox.Show("Unable to load tab. You have insufficient privileges.",
                "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }

    // check active directory to see if user is in Marketing department group
    private static bool checkGroup(string group)
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(group);
    }
4

4 に答える 4

126

.NET 3.5 以降を使用しているため、System.DirectoryServices.AccountManagement(S.DS.AM) 名前空間を確認する必要があります。ここでそれについてすべて読んでください:

基本的に、ドメイン コンテキストを定義し、AD でユーザーやグループを簡単に見つけることができます。

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

if(user != null)
{
   // check if user is member of that group
   if (user.IsMemberOf(group))
   {
     // do something.....
   } 
}

新しい S.DS.AM を使用すると、AD でユーザーやグループを簡単に操作できます。

于 2012-08-19T19:50:29.777 に答える
0

この方法ではできません。Active Directory を照会する必要があります。AD のラッパーを使用できます。http://www.codeproject.com/Articles/10301/Wrapper-API-for-using-Microsoft-Active-Directory-Sを確認してください。

于 2012-08-19T19:44:16.797 に答える
0

グループ内の現在のユーザーかどうかを確認する

public bool AuthenticateGroup(string groupfind)
        {
            var p = new Process();
            StringBuilder stringbd = new StringBuilder();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = @"/c gpresult /V";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = false;
            p.StartInfo.UseShellExecute = false;
            p.OutputDataReceived += (a, b) => stringbd.AppendLine(b.Data);
            p.ErrorDataReceived += (a, b) => stringbd.AppendLine(b.Data);
            p.Start();
            p.BeginErrorReadLine();
            p.BeginOutputReadLine();
            p.WaitForExit();
            var textfind = stringbd.ToString();
            int findpoint = textfind.IndexOf("The user is a part of");
            string findgroup = "";
            if (findpoint > 0)
            {
                findgroup = textfind.Substring(findpoint, textfind.Length - findpoint);
            }

            return findgroup.Split('\n').ToList().Any(r=>r.Trim().ToLower()==groupfind.Trim().ToLower());
        }
于 2021-01-19T02:44:53.157 に答える