c#如何将AD成员资格从现有用户复制到新用户?

时间:2017-12-13 11:24:03

标签: c# winforms visual-studio active-directory membership

我有以下代码在Active Directory中创建用户。我唯一要做的就是从其他用户(部分成员(文件服务器访问等))复制成员资格。我只是无法弄清楚如何做到这一点,在网上找不到任何关于...

private void Form1_Load(object sender, EventArgs e)
{
    textboxOu.Text = 
    "OU=Common,OU=Users,OU=NLEML,OU=NL,OU=_Sites,DC=testdomain,DC=nl";
    textboxAdminUsername.Text = @"kuiken\";
}

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string stringDomainName = 
        System.Net.NetworkInformation.IPGlobalProperties.
        GetIPGlobalProperties().DomainName;
        PrincipalContext PrincipalContext4 = new 
        PrincipalContext(ContextType.Domain, stringDomainName,   
        textboxOu.Text, ContextOptions.SimpleBind, 
        textboxAdminUsername.Text, 
        passwordboxAdminPassword.Text);
        UserPrincipal UserPrincipal1 = new UserPrincipal(PrincipalContext4, 
        textboxLonOnName.Text, 
        passwordboxUserPass.Text, true);

        //User Logon Name
        string homefolder = @"\\kuiken\users$\" + textboxLonOnName.Text;
        string emailadress = txtboxMail.Text + "@test.network";

        UserPrincipal1.UserPrincipalName = textboxSamAccountName.Text;
        UserPrincipal1.Name = textboxName.Text;
        UserPrincipal1.GivenName = textboxGivenName.Text;
        UserPrincipal1.Surname = textboxSurname.Text;
        UserPrincipal1.DisplayName = textboxName.Text;
        UserPrincipal1.Description = textboxDescription.Text;
        UserPrincipal1.EmailAddress = emailadress;
        UserPrincipal1.EmployeeId = txtID.Text;
        UserPrincipal1.VoiceTelephoneNumber = textboxPhonenumber.Text;
        UserPrincipal1.HomeDrive = "H:";
        UserPrincipal1.HomeDirectory = homefolder;




        if (radiobuttonEnable.Checked == true)
        {
            UserPrincipal1.Enabled = true;
        }
        else
        {
            UserPrincipal1.Enabled = false;
        }
        UserPrincipal1.Save();

        MessageBox.Show("AD account created succesfuly");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

我想使用带有用户名的文本框,例如现有用户的输入。

1 个答案:

答案 0 :(得分:0)

如果您要从中复制组=sum(filter(filter($C$3:$5, month($C$2:$2)=column()-1), $A$3:$A$5=$A9)) ,则使用GetGroups()方法查找其所属的所有组。

这样的事情(UserPrincipal是您要复制的帐户):

sourceAccount