我有以下代码,只需单击一下按钮即可将用户添加到广告组中。同样,单击按钮会将用户从AD组中删除。 用户信息被拉到LDAP服务器,我已经在IIS上托管了它。 但是,单击按钮后,什么也没有发生。
我已经复制了代码。需要建议。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
namespace UserDetail
{
public partial class UserDet : System.Web.UI.Page
{
public DirectorySearcher dirSearch = null;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.Web.HttpContext.Current.User.Identity.Name.ToString();
lblError.Visible = false;
}
public bool AddUserToGroup(string userId, string groupName)
{
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "abc.com"))
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
group.Members.Add(pc, IdentityType.UserPrincipalName, userId);
group.Save();
}
return true;
}
catch (System.DirectoryServices.DirectoryServicesCOMException E)
{
return false;
}
}
public bool RemoveUserFromGroup(string userId, string groupName)
{
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "abc.com"))
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
group.Members.Remove(pc, IdentityType.UserPrincipalName, userId);
group.Save();
}
return true;
}
catch (System.DirectoryServices.DirectoryServicesCOMException E)
{
return false;
}
}
public string GetUserName()
{
return Label1.Text.Split('\\')[1].ToString();
}
protected void btnSubscribe_Click(object sender, EventArgs e)
{
string userName = GetUserName();
string groupName = "CoE Advanced Applications";
lblError.Visible = true;
if (AddUserToGroup(userName, groupName))
{
lblError.Style.Add("color","green");
lblError.Text ="Added "+ userName + " to group: " + groupName + " successfully.";
}
else
{
lblError.Style.Add("color", "red");
lblError.Text = "Adding " + userName + " to group: " + groupName + " failed.";
}
}
protected void btnUnSubscribe_Click(object sender, EventArgs e)
{
string userName = GetUserName();
string groupName = "CoE Advanced Applications";
lblError.Visible = true;
if (RemoveUserFromGroup(userName,groupName))
{
lblError.Style.Add("color", "green");
lblError.Text = "Removed "+ userName + " from group: " + groupName + " successfully.";
}
else
{
lblError.Style.Add("color", "red");
lblError.Text = "Removing " + userName + " from group: " + groupName + " failed.";
}
}
}
}
先谢谢。