C#Active Directory特定的radiobutton已选中/未选中

时间:2013-10-25 07:35:33

标签: c# active-directory

任何人都可以帮我解决下面的问题吗?

我想查看是否选中或取消选中活动目录中的特定单选按钮。 Bellow你有一个指向单选按钮的打印屏幕的链接(标有红色)

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

谢谢!

LE

我使用的是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)
        {
        }  

2 个答案:

答案 0 :(得分:0)

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

 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; 
     }

 }

答案 1 :(得分:0)

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

亚历