简体   繁体   English

Active Directory用户条目和组条目

[英]Active Directory user entry, and group entry

I'm currently having an issues to where I cannot get my Organizational Unit to be recognized as an argument with creating a new Active Directory user, and assigning them to a OU. 我目前遇到一个问题,即在创建新的Active Directory用户并将其分配给OU时,无法将我的组织单位视为一个参数。 It gives me the error, "GetPrincipalContext" takes 1 argument, and I'm lost as what went wrong. 它给了我错误,“ GetPrincipalContext”接受了1个参数,而我因为错误原因而迷失了方向。 If further information if needed, please let me know. 如果需要更多信息,请告诉我。

    #region Variables
    private string sdomain = "test";
    private string sdefaultou = "OU=Dackup Users, OU=Dackup, DC=Test, Dc=com";
    private string sdefaultrootOU = "DC=test, DC=com";
    private string sServiceUser = @"ServiceUser";
    private string sServicePassword = "ServicePassword";
    private string sGroup = "Dackup";
    private string sUserName = "LocalTest";
    private string sOU = "Organizational Unit locations";
    #endregion

    #region Validate
    public PrincipalContext GetPrincipalContext()//(string sdomain, string sdefaultou, string sservicepassword
    {
        PrincipalContext oPrincipal = new PrincipalContext(ContextType.Domain, sdomain, sdefaultou, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
        return oPrincipal;
    }

    public UserPrincipal GetUser(string sUserName)
    {
        PrincipalContext oPrinciple = GetPrincipalContext();
        UserPrincipal oUserprinciple = UserPrincipal.FindByIdentity(oPrinciple, sUserName);
        return oUserprinciple;
    }

    public bool IsUserExisting(string sUserName)
    {
        if (GetUser(sUserName) == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /*   public bool ValidateCredential (string sUserName, string sPassword)
    {
        PrincipalContext oprincipalc = "fix"();
        return oprincipalc.ValidateCredentials(sUserName, sPassword);
    } */

    public UserPrincipal CreateNewUser(string sOU, string sUserName, string sPassword, string sGivenName, string sSurname)
    {
        if (!IsUserExisting(sUserName))
        {
            PrincipalContext oPrincipalContext = GetPrincipalContext(sOU); //This is where the error occurs

            UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext, sUserName, sPassword, true /*Enabled or not*/);

            //User Log on Name
            oUserPrincipal.UserPrincipalName = sUserName;
            oUserPrincipal.GivenName = sGivenName;
            oUserPrincipal.Surname = sSurname;
            oUserPrincipal.Save();

            return oUserPrincipal;
        }
        else
        {
            return GetUser(sUserName);
        }
    }

    public GroupPrincipal GetGroup(string sGroup)
    {
        PrincipalContext oPrincipal = GetPrincipalContext();
        GroupPrincipal ogroup = GroupPrincipal.FindByIdentity(oPrincipal, sGroup);
        return ogroup;
    }

    public bool IsUserGroupMember(string sGroup, string sUserName)
    {
        UserPrincipal oUser = GetUser(sUserName);
        GroupPrincipal ogroup = GetGroup(sGroup);

        if (oUser != null && ogroup != null)
        {
            return ogroup.Members.Contains(oUser);
        }
        else
        {
            return false;
        }
    }

    public bool AddUserToGroup(string sUserName, string sGroup)
    {
        try
        {
            UserPrincipal oUserPrincipal = GetUser(sUserName);
            GroupPrincipal oGroupPrincipal = GetGroup(sGroup);

            if (oUserPrincipal != null && oGroupPrincipal != null)
            {
                if (!IsUserGroupMember(sUserName, sGroup))
                {
                    oGroupPrincipal.Members.Add(oUserPrincipal);
                    oGroupPrincipal.Save();
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CreateNewUser();
    }
}
#endregion

Use the below code 使用下面的代码

PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local",           "OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 0; i < 3; i++)
        {
            try
            {
                UserPrincipal up = new UserPrincipal(ouContex);
                up.SamAccountName = "TestUser" + i;
                up.SetPassword("password");
                up.Enabled = true;
                up.ExpirePasswordNow();
                up.Save();
            }
            catch (Exception ex)
            {

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM