试试下面的例子:
C#代码
首先添加事件点击您的按钮:
// Button click event
private void btnDisableAcc_Click(object sender, EventArgs e)
{
// When the user clicks the button
String _ADUserName = textBox1.Text; // <-- The textbox you enter your username?
// Call the method below 'DiableADUserUsingUserPrincipal'
DiableADUserUsingUserPrincipal(_ADUserName); // <-- Pass in the user name via the local variable
}
然后在同一类定义你的方法由于保护级别是私人的 否则如果它是德在另一类/装配裁判判罚再进行保护层次的公共
// Private Method
private static void DiableADUserUsingUserPrincipal(string username)
{
try
{
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
(principalContext, username);
userPrincipal.Enabled = false;
userPrincipal.Save();
MessageBox.Show("AD Account disabled for {0}", username);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
要启用该帐户:
// Private Method with return type "Boolean" to determine if the method succeed or not.
private static bool EnableADUserUsingUserPrincipal(string username)
{
try
{
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
(principalContext, username);
userPrincipal.Enabled = true;
userPrincipal.Save();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
private void button2_Click(object sender, EventArgs e)
{
String _ADUserName = textBox1.Text; // <-- The textbox you enter your username?
// Check if the account is enabled
if (EnableADUserUsingUserPrincipal(_ADUserName))
{
MessageBox.Show("AD Account Enabled for {0}", _ADUserName);
this.StatusTextBox.Text = "Account Enabled";
}
}
请在您的问题更加清晰和添加代码时,是描述性的。 – ThatAwesomeCoder
是的,我的错误。我已经添加了几个描述。 我想禁用活动目录用户使用C#但无法计算如何链接文本框条目与此代码。 –
你指的是“文本框条目”? C#表单应用程序TextBox? – ThatAwesomeCoder