C# 在远程Exchange 2010服务器上用C创建Exchange邮箱#
标题几乎解释了这一切。我有一个C#应用程序没有在Exchange服务器上运行。我需要能够创建邮箱。我尝试使用教程,但它要求PowerShell IIS虚拟目录:C# 在远程Exchange 2010服务器上用C创建Exchange邮箱#,c#,powershell,exchange-server,exchange-server-2010,C#,Powershell,Exchange Server,Exchange Server 2010,标题几乎解释了这一切。我有一个C#应用程序没有在Exchange服务器上运行。我需要能够创建邮箱。我尝试使用教程,但它要求PowerShell IIS虚拟目录: 不需要SSL 允许基本身份验证 这些都是我们不能做的事情。这使我想到两种可能的解决办法。有没有办法修改上面列出的教程,使其不需要这两个限制,或者有没有办法不使用powershell就可以做到这一点 以下是代码,以防您不想访问链接: using System; using System.Security; using System.Ma
using System;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PowerShellTest
{
class Program
{
static void Main(string[] args)
{
// Prepare the credentials that will be used when connecting
// to the server. More info on the user to use on the notes
// below this code snippet.
string runasUsername = @"username";
string runasPassword = "password";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword)
ssRunasPassword.AppendChar(x);
PSCredential credentials =
new PSCredential(runasUsername, ssRunasPassword);
// Prepare the connection
var connInfo = new WSManConnectionInfo(
new Uri("http://ServersIpAddress/PowerShell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credentials);
connInfo.AuthenticationMechanism =
AuthenticationMechanism.Basic;
// Create the runspace where the command will be executed
var runspace = RunspaceFactory.CreateRunspace(connInfo);
// generate the command parameters
var testNumber = 18;
var firstName = "Test";
var lastName = "User" + testNumber;
var username = "tuser" + testNumber;
var domainName = "pedro.test.local";
var password = "ActiveDirectoryPassword1234";
var ssPassword = new SecureString();
foreach (char c in password)
ssPassword.AppendChar(c);
// create the PowerShell command
var command = new Command("New-Mailbox");
command.Parameters.Add("Name", firstName + " " + lastName);
command.Parameters.Add("Alias", username);
command.Parameters.Add(
"UserPrincipalName", username + "@" + domainName);
command.Parameters.Add("SamAccountName", username);
command.Parameters.Add("FirstName", firstName);
command.Parameters.Add("LastName", lastName);
command.Parameters.Add("Password", ssPassword);
command.Parameters.Add("ResetPasswordOnNextLogon", false);
command.Parameters.Add(
"OrganizationalUnit", "NeumontStudents");
// Add the command to the runspace's pipeline
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(command);
// Execute the command
var results = pipeline.Invoke();
runspace.Dispose();
if (results.Count > 0)
Console.WriteLine("SUCCESS");
else
Console.WriteLine("FAIL");
}
}
}
您可以使用许多不同的
身份验证机制设置所谓的运行空间
访问MSDN站点,获取代码示例,使用:
- 基本身份验证(在您的示例中使用)
- 证书认证
- Kerberos身份验证
- 协商认证
无论如何,现在还不需要放弃PowerShell。那篇博文中的代码有点不完整。Pipeline类是创建命令的一种过于复杂的方式,它的编写方式涉及到创建一对运行空间(一个本地运行空间,一个远程运行空间),而不仅仅是远程运行空间
此外,IIS中的“基本”和“http”并不意味着“PowerShell中没有安全性和加密”。默认情况下,通过WinRM层发送的所有内容都是加密的
来自Exchange团队的信息涵盖了在C#中实现这一点的正确方法,相当好
因此:
- 您不必担心IIS的“基本”,因为还有另一层安全性
- 如果使用Exchange团队的C#,您可以将代码切成两半并使其更快
也要100%透明:
除非通过PowerShell,否则无法远程管理Exchange 2010
希望这有帮助