добавить / удалить пользователя в группе Active Directory - PullRequest
0 голосов
/ 27 февраля 2019

У меня есть код ниже, который добавляет пользователя в группу AD по нажатию кнопки.Точно так же он удаляет пользователя из группы 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.";
            }
        }


    }
}

спасибо заранее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...