繁体   English   中英

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

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

我正在使用以下代码段在域服务器上创建一个新的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());
}

上面的代码成功创建了一个新用户,并在AD服务器上启用了该用户。 但我无法创建/启用Exchange邮箱,因为IMailboxStore命名空间需要cdoexm.dll 我试过在域控制器,邮箱服务器和客户端访问服务器上找到cdoexm.dll ,但实际上。

我知道执行此操作的另一种方法是使用Powershell cmdlet,但我不想这样做。

现在精确地陈述我的问题:

  • 如何添加COM cdoexm.dll 要么
  • 还有其他使用IMailBoxStore吗? 要么
  • 除了PowerShell,是否有其他方法可以启用AD用户的邮箱和Lync帐户?

由于CDOEXM现在已从Exchange 2010开始淘汰,因此前两个问题已得到解决。

要为现有的AD用户启用邮箱,请使用enable-mailuser powershell命令。

要启用Lync帐户,请使用enable-csuser powershell命令。

从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

经过一些搜索,我只能使用PowerShell找到解决方案,
或使用c#调用PowerShell。

要使用C#调用PowerShell:

添加“用法”:

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

调用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;
}

上面的内容是从以下链接复制的(未经测试):

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

但是,如果您想要除PowerShell以外的解决方案,则上述方法可能没有用...

暂无
暂无

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

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