hi
my problem is:
I develop this code in my test lab for remote exchange cmdlet by C# and Asp.net
this code worked fine in Visual Studio 2013 (it is my test lab in "mohsen.com" domain and my exchange server is 2013 with FQDN: "Exchange.mohsen.com" this code in my consol application work fine!
using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Remoting; using System.Management.Automation.Runspaces; using System.Security; using System.Management.Automation.Host; namespace ConsoleApplication1 { class Program { static SecureString GetSecurePassword(string password) { var securePassword = new SecureString(); foreach (var c in password) { securePassword.AppendChar(c); } return securePassword; } static void Main(string[] args) { string _username = "administrator"; string _pass = "abc@123"; var name = "mynamde"; var family = "myfamily"; var pass = "abc@123"; var username = "username"; var upn = username + "@mohsen.com"; var database_name = "Mailbox1"; var ou = "empploye"; var ssPassword = GetSecurePassword(pass); var securePassword = GetSecurePassword(_pass); PSCredential newCred = new PSCredential(_username, securePassword); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchange.mohsen.com/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("New-Mailbox"); command.AddParameter("UserPrincipalName", username + "@mohsen.com"); command.AddParameter("Alias", username); command.AddParameter("Database", database_name); command.AddParameter("Name", username); command.AddParameter("OrganizationalUnit", ou); command.AddParameter("Password", ssPassword); command.AddParameter("FirstName", name); command.AddParameter("LastName", family); command.AddParameter("DisplayName", name + "" + family); powershell.Commands = command; try { runspace.Open(); powershell.Runspace = runspace; powershell.Invoke(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadKey(); } finally { runspace.Dispose(); runspace = null; powershell.Dispose(); powershell = null; } } } }
and worked fine!
but when create my web app by above code in asp.net page with following code recive access Denied error!!!
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.DirectoryServices.AccountManagement; using System.DirectoryServices; using System.Management.Automation; using System.Management.Automation.Remoting; using System.Management.Automation.Runspaces; using System.Security; public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } static SecureString GetSecurePassword(string password) { var securePassword = new SecureString(); foreach (var c in password) { securePassword.AppendChar(c); } return securePassword; } protected void Button1_Click(object sender, EventArgs e) { string _username = "mohsen\administrator"; string _pass = "abc@123"; var name = TextBox_name.Text; var family = TextBox_family.Text; var pass = TextBox_pass.Text; var username = TextBox_username.Text; var upn = username+"@mohsen.com"; var database_name = "Mailbox1"; var ou = "empploye"; var ssPassword = GetSecurePassword(TextBox_pass.Text); var securePassword = GetSecurePassword(_pass); PSCredential newCred = new PSCredential(_username, securePassword); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchange.mohsen.com/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("New-Mailbox"); command.AddParameter("UserPrincipalName", username + "@mohsen.com"); command.AddParameter("Alias", username); command.AddParameter("Database", database_name); command.AddParameter("Name", username); command.AddParameter("OrganizationalUnit", ou); command.AddParameter("Password", ssPassword); command.AddParameter("FirstName", name); command.AddParameter("LastName", family); command.AddParameter("DisplayName",name+""+family); powershell.Commands = command; try { runspace.Open(); powershell.Runspace = runspace; powershell.Invoke(); } catch (Exception ex) { Literal1.Text = ex.ToString(); } finally { runspace.Dispose(); runspace = null; powershell.Dispose(); powershell = null; } } }erstwhile , this code worked fine by exchange 2010 !
where my code is wrong?any thing chaned in exchange 2013?
should config any thing in client iis?
in my exchange server in iis /internal powershell / Authentication / windows Authentication is enable
should use a different AuthenticationMechanism.Kerberos?
why my consol app work fine where as my web app wont?
may I config some thing in web.config? my web.config is:
<?xml version="1.0" encoding="UTF-8"?><!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --><configuration><system.web><compilation strict="false" explicit="true" targetFramework="4.5" debug="true"><assemblies><add assembly="System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /><add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /><add assembly="System.DirectoryServices.Protocols, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /><add assembly="System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /></assemblies></compilation><httpRuntime targetFramework="4.5" /></system.web><system.webServer><defaultDocument><files><clear /><add value="Register.aspx" /></files></defaultDocument></system.webServer></configuration>
please help my
thanx