0

有人可以帮我解决下面的问题吗?

我想查看活动目录中的特定单选按钮是选中还是未选中。下面你有一个单选按钮打印屏幕的链接(标有红色)

http://media.ipsosinteractive.com/projects/S1027838/img/pic.jpg

谢谢!

乐:

我正在使用 System.DirectoryServices;

如果我可以使用 System.DirectoryServices.AccountManagement 连接到 AD,那将非常容易。

不知道怎么连接。。。

假设我有域:office.company.intra

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "...", "DC=..,DC=..,DC=..", systemAccount, systemAccountPassword);

LEE - 供将来参考 AD 示例:

string acc="";

        if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null)
            Label20.Text = rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["givenName"].Value != null)
            Label21.Text = rs.GetDirectoryEntry().Properties["givenName"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["sn"].Value != null)
            Label22.Text = rs.GetDirectoryEntry().Properties["sn"].Value.ToString();

        //if (rs.GetDirectoryEntry().Properties["userAccountControl"].Value != null)
        //    TextBox4.Text = "value : " + rs.GetDirectoryEntry().Properties["userAccountControl"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["userAccountControl"].Value != null)
            acc = rs.GetDirectoryEntry().Properties["userAccountControl"].Value.ToString();

        //if (String.Compare(acc, "512")==-1)
        if (acc=="512")
        {
            Label24.ForeColor = System.Drawing.Color.Green;
            Label24.Text = "Enabled Account";
        }
        if (acc == "514")
        {
            Label24.ForeColor = System.Drawing.Color.Red;
            Label24.Text = "Disabled Account";

        }
        if (acc == "544")
        {
            Label24.ForeColor = System.Drawing.Color.Gold;
            Label24.Text = "Enabled, Password Not Required";
        }
        if (acc == "546")
        {
            Label24.ForeColor = System.Drawing.Color.Red;
            Label24.Text = "Disabled, Password Not Required";
        }
        if (acc == "66050")
        {
            Label24.ForeColor = System.Drawing.Color.Red;
            Label24.Text = "Disabled, Password Doesn't Expire";
        }
        if (acc == "66048")
        {
            Label24.ForeColor = System.Drawing.Color.Gold;
            Label24.Text = "Enabled, Password Doesn't Expire";
        }

        if (acc == "66080")
        {
            Label24.ForeColor = System.Drawing.Color.Gold;
            Label24.Text = "Enabled, Password Doesn't Expire & Not Required";
        }

        if (acc == "66082")
        {
            Label24.ForeColor = System.Drawing.Color.Red;
            Label24.Text = "Disabled, Password Doesn't Expire & Not Required";
        }

        Int64 pls = 1;

        if (rs.GetDirectoryEntry().Properties["pwdLastSet"] != null && rs.GetDirectoryEntry().Properties["pwdLastSet"].Value != null)
        {
            pls = ConvertADSLargeIntegerToInt64(rs.GetDirectoryEntry().Properties["pwdLastSet"].Value);
        }
        //else
        //{
        //    throw new Exception("Nu am putut determina!");
        //}
        Label30.ForeColor = System.Drawing.Color.Green;
        Label30.Text = "NU <font color=\"black\">(Nota: functie netestata.)</font>";
        if (pls == 0)
        {
            Label30.Text = "DA <font color=\"black\">(Nota: functie netestata.)</font>";
        }

//////////////

private static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
    {
        var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
        var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
        return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, blabla);
        string valEx = @"\<\w\>";

        //if (!(Regex.IsMatch(this.TextBox1.Text.Trim(), valEx)))



        try
        {
            if (TextBox1.Text.Trim().Length != 0)
            {
                GetUserInformation(username, passowrd, domain);

                UserPrincipal user = UserPrincipal.FindByIdentity(insPrincipalContext, TextBox1.Text);
                //textBox1.Text = user.ToString();

                Label23.ForeColor = System.Drawing.Color.Red;



                string pla = user.LastPasswordSet.ToString();
                Label28.Text = pla + " (Format: MM/DD/YYY)";

                //DateTime expiration = user.AccountExpirationDate.Value.ToLocalTime();
                if (user.AccountExpirationDate != null)
                {
                    string expiration = user.AccountExpirationDate.ToString();
                    Label32.Text = expiration.ToString();
                }
                else
                {
                    Label32.Text = "NU";
                }

                if (user.IsAccountLockedOut())
                {
                    Label23.ForeColor = System.Drawing.Color.Red;
                    Label23.Text = "Locked";
                }
                else
                {
                    Label23.ForeColor = System.Drawing.Color.Green;
                    Label23.Text = "NOT Locked";
                }
            }
            else
            {
                Label23.Text = "Please enter all required inputs.";

            }
        }
        catch (Exception j)
        {
        }  
4

2 回答 2

0

首先,您必须根据某些条件(例如用户名)搜索特定用户,然后检查特定属性:

 string UserName = "SomeLogin";     

 PrincipalContext domainContext = 
    new PrincipalContext(ContextType.Domain, "...", "... );            

 using ( var searcher = 
            new PrincipalSearcher( new UserPrincipal( domainContext ) 
                     { SamAccountName = UserName } ) )
 {
     var principal = UserPrincipal i in searcher.FindAll().ToList().FirstOrDefault();
     if ( principal != null )
     {
        // this is what you look for
        bool enabled = principal.Enabled; 
     }

 }
于 2013-10-25T09:26:47.263 回答
0

在第一篇文章中解决了 LEE。

亚历克斯

于 2013-11-19T11:59:15.617 回答