简体   繁体   English

如何在C#中使用LDAP以编程方式启用AD用户的交换和Lync帐户

[英]How to enable AD user's exchange and Lync accounts programmatically using LDAP in C#

I am creating a new AD user on the domain server using the following code snippet: 我正在使用以下代码段在域服务器上创建一个新的AD用户:

DirectoryEntry newUser = directoryEntry.Children.Add("CN=" + model.Account.FullName, "user");
if (model.Account.SamAccountName != null) newUser.Properties["sAMAccountName"].Value = model.Account.SamAccountName;
newUser.CommitChanges();

setUserPassword("CN=" + model.Account.FullName + "," + model.Account.Path, model.Account.Password);

newUser.RefreshCache();

if (model.Account.FirstName != null) newUser.Properties["givenName"].Add(model.Account.FirstName);
if (model.Account.LastName != null) newUser.Properties["sn"].Add(model.Account.LastName);
if (model.Account.MiddleName != null) newUser.Properties["initials"].Add(model.Account.MiddleName);

if (model.Account.UPNLogon != null && model.Account.DomainName != null) newUser.Properties["userPrincipalName"].Add(model.Account.UPNLogon + "@" + model.Account.DomainName);
if (model.Organization.DisplayName != null) newUser.Properties["displayName"].Add(model.Organization.DisplayName);
if (model.Organization.Email != null) newUser.Properties["mail"].Add(model.Organization.Email);

newUser.Properties["LockOutTime"].Value = 0; //unlock account
newUser.Properties["userAccountControl"].Value = 0x200; // enable account
newUser.CommitChanges();

string homeMDB = profile.Exchange13_Profile.ExchangeDB;

IMailboxStore mailbox;
try
{
     IMailboxStore mailbox = (IMailboxStore)NewUser;
     mailbox.CreateMailbox(sHomeMDB);
     NewUser.CommitChanges();
}
catch (InvalidCastException e)
{
     MessageBox.Show(e.Message.ToString());
}

The above code successfully creates a new user and enables it on the AD server. 上面的代码成功创建了一个新用户,并在AD服务器上启用了该用户。 But I am unable to create/enable the Exchange mailbox, as the IMailboxStore namespace needs cdoexm.dll . 但我无法创建/启用Exchange邮箱,因为IMailboxStore命名空间需要cdoexm.dll I've tried to locate cdoexm.dll on the Domain Controller, MailBox Server, and Client Access Server, but in vein. 我试过在域控制器,邮箱服务器和客户端访问服务器上找到cdoexm.dll ,但实际上。

I know the alternate way of doing this, is by using Powershell cmdlets, but I don't want to do that. 我知道执行此操作的另一种方法是使用Powershell cmdlet,但我不想这样做。

Now precisely stating my questions: 现在精确地陈述我的问题:

  • How to add the COM cdoexm.dll ? 如何添加COM cdoexm.dll Or 要么
  • Is there any other way around to use IMailBoxStore ? 还有其他使用IMailBoxStore吗? Or 要么
  • Is there any way to enable the AD user's mailbox and Lync account other than PowerShell? 除了PowerShell,是否有其他方法可以启用AD用户的邮箱和Lync帐户?

The first two questions are resolved as CDOEXM is now obsolete from Exchange 2010 and onward. 由于CDOEXM现在已从Exchange 2010开始淘汰,因此前两个问题已得到解决。

To enable a mailbox for an existing AD user, use the enable-mailuser powershell command. 要为现有的AD用户启用邮箱,请使用enable-mailuser powershell命令。

To enable the Lync account, use the enable-csuser powershell command. 要启用Lync帐户,请使用enable-csuser powershell命令。

CDOEXM is removed starting from Exchange 2007. And this is not an assembly, it's COM. 从Exchange 2007开始删除了CDOEXM。这不是程序集,而是COM。

http://blogs.msdn.com/b/deva/archive/2010/01/13/update-technologies-not-available-with-exchange-2010-their-migration-reference-s.aspx http://blogs.msdn.com/b/deva/archive/2010/01/13/update-technologies-not-available-with-exchange-2010-their-migration-reference-s.aspx

Did some searching, I can only find solution using PowerShell, 经过一些搜索,我只能使用PowerShell找到解决方案,
or use c# to call PowerShell. 或使用c#调用PowerShell。

To use C# to call PowerShell: 要使用C#调用PowerShell:

Add "usings": 添加“用法”:

using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;

Calling the cmdlet: 调用cmdlet:

PSCredential newCred = (PSCredential)null;
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchangeserver01.my.domain/powershell?serializationLevel=Full"), 
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();

PSCommand command = new PSCommand();
command.AddCommand("Enable-Mailbox");
command.AddParameter("Identity", user.Guid.ToString());
command.AddParameter("Alias", user.UserName);
command.AddParameter("DomainController", ConnectingServer);
powershell.Commands = command;

try
{
    runspace.Open();
    powershell.Runspace = runspace;
    Collection<psobject> results = powershell.Invoke();
}
catch (Exception ex)
{
    string er = ex.InnerException.ToString();
}
finally
{
    runspace.Dispose();
    runspace = null;

    powershell.Dispose();
    powershell = null;
}

The above is is copied from the following links (not tested): 上面的内容是从以下链接复制的(未经测试):

http://codingchris.com/2012/02/15/creating-exchange-2010-mailboxes-in-c/ http://codingchris.com/2014/02/11/creating-exchange-2013-mailbox-in-c/ http://codingchris.com/2012/02/15/creating-exchange-2010-mailboxes-in-c/ http://codingchris.com/2014/02/11/creating-exchange-2013-mailbox-in- C/

But as you want solution other than PowerShell, may be the above is not useful... 但是,如果您想要除PowerShell以外的解决方案,则上述方法可能没有用...

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

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