[英]C# Active Directory specific radiobutton checked/unchecked
can anybody help me with the problem below? 有人可以帮我解决以下问题吗?
I want to see if a specific radiobutton in active directory is checked or unchecked. 我想查看活动目录中的特定单选按钮是否处于选中状态。 Bellow you have a link to a printscreen of the radio button (marked with red)
在下面,您有一个指向单选按钮的打印屏幕的链接(标记为红色)
http://media.ipsosinteractive.com/projects/S1027838/img/pic.jpg http://media.ipsosinteractive.com/projects/S1027838/img/pic.jpg
Thank you! 谢谢!
LE: LE:
I was using System.DirectoryServices; 我正在使用System.DirectoryServices;
if i can make the connection to AD with System.DirectoryServices.AccountManagement it will be much easy. 如果我可以使用System.DirectoryServices.AccountManagement建立到AD的连接,它将非常容易。
i don't know how to connect... 我不知道该如何连接...
let's say i have domain: office.company.intra 假设我有一个域:office.company.intra
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "...", "DC=..,DC=..,DC=..", systemAccount, systemAccountPassword); PrincipalContext domainContext =新的PrincipalContext(ContextType.Domain,“ ...”,“ DC = ..,DC = ..,DC = ..”,systemAccount,systemAccountPassword);
LEE - for future references AD exemples: 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)
{
}
First you'd have to search specific user based on some criteria (UserName for example) and then check specific property: 首先,您必须根据一些条件(例如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;
}
}
solved in LEE in first post. 在LEE的第一篇文章中解决。
Alex 亚历克斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.