[英]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,但我不想這樣做。
現在精確地陳述我的問題:
cdoexm.dll
? 要么 IMailBoxStore
嗎? 要么 由於CDOEXM現在已從Exchange 2010開始淘汰,因此前兩個問題已得到解決。
要為現有的AD用戶啟用郵箱,請使用enable-mailuser
powershell命令。
要啟用Lync帳戶,請使用enable-csuser
powershell命令。
從Exchange 2007開始刪除了CDOEXM。這不是程序集,而是COM。
經過一些搜索,我只能使用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.