Credentialed Service Receives 401 Unauthorized from EWS - c#

I have written a windows service in C# to automatically poll an exchange mailbox using EWS.
On my development machine, which is connected to the exchange server network via VPN, it works perfectly.
On the target server the EWS is returning:
Request failed. The remote server returned an error: (401) Unauthorized.(The remote server returned an error: (401) Unauthorized.)
On the same server, I can connect to the EWS URL via a browser using the same credentials as are being provided to the service.
The credentials are provided in the app.config file, and I have triple checked that they are the same on the target server as my development machine.
What could be causing this?

Well as it turned out the issue was that this
_ews.Credentials = new NetworkCredential(Settings.Username, Settings.Password); // Username in the form "domainname\username"
should have been this
_ews.Credentials = new NetworkCredential(Settings.Username, Settings.Password, Settings.Domain); // Domainname and username separate
For some reason the first line worked externally over a VPN but not internally on the domain network itself.

Related

Microsoft's url for the Exchange powershell schema is not valid anymore

I am trying to create a remote connection to an exchange Powershell hosted on IIS 8.5 - Windows Server 2012 R2.
Here's my code :
var connInfo = new WSManConnectionInfo(
new Uri("https://xxx/PowerShell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credentials);
It look like the Microsoft's Schemas url is not valid anymore. If I navigate to http://schemas.microsoft.com/powershell/Microsoft.Exchange, I have this :
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
And when I try to open my connection, I have this error :
Connecting to remote server failed with the following error message : The WinRM client received an HTTP bad request status (400), but the remote service did not include any other information about the cause of the failure.
Is it possible the url changed ? I trid to find a topic about it, but I found no trace of a change...
The schema uri doesn't have to be 'valid' in the sense you don't have to be able to navigate to it. I know I'm not explaining it properly but it's more a reference than a valid location.
The error you're receiving is actually referring to the uri you have for your Exchange Server. If you aren't connecting to an Exchange Server the connection will error.

How to recycle aws server app pool remotely

I am trying to recycle the aws app pool remotely using below syntax
using (DirectoryEntry appPoolEntry = new DirectoryEntry(
"IIS://" + appPoolModel.ServerName + "/W3SVC/AppPools/"+appPoolModel.AppPoolName))
{
appPoolEntry.Invoke("Recycle", null);
appPoolEntry.Close();
}
But i am getting this below error
System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable.
For aws server i am using the server name as follows
"ec2-[Server Public IP].compute-1.amazonaws.com"
There couple of reason for this.
One obivious reason that shown in error that RPC server is actually not running.
If RPC server is running you have to check the firewall port open for RPC.
http://support.microsoft.com/kb/178517
Context of user in which your code is executed. Support you run in context of user1 and it does not have permission for DirectoryEntry object then it might fail.
if This is not possible then try to do it via Remote Powershell.
https://social.technet.microsoft.com/Forums/windowsserver/en-US/b24d5b03-343c-47b5-937e-aa47664cdfb2/how-can-i-restart-an-iis-webapppool-on-a-remote-server-in-powershell?forum=winserverpowershell

Can't login to Asterisk from .NET

I am trying to run the samples from Asterisk.NET . When I try to run the following piece of code from Asterisk.NET.Test:
manager = new ManagerConnection(ASTERISK_HOST, ASTERISK_PORT, ASTERISK_LOGINNAME, ASTERISK_LOGINPWD);
manager.Login();
I always get the follwing message:
AuthenticationFailedException: Authentication failed ... at ManagerConnection.cs:line 1263
Which username and password should I use?
I've tried "admin"/"admin"which I use for logging in at FreePBX web administration and with "admin"/"amp111", none of which work. I have not modified anything after installing AsteriskNow in VirtualBox. In the FreePBX web administration page every server status is green (ok).
Regards,
Tamas Ionut
Start Telnet on machine from which you are trying to connect and run commands:
telnet 000.000.000.000 5038
ACTION:LOGIN
USERNAME:username
SECRET:password
EVENTS:ON
Where 000.000.000.000, username, password are server address, username and password.
If you receive message "authentication failed" most likely the connection from your machine's address is not allowed for the user 'username'. The addresses from which the user is allowed to establish connection are configured on the server in config files.

C# code access exchange server without domain name, is that possible?

I'm working on a web application to access exchange server 2010 to get emails from the server. The question is, we don't have domain name for this server. What I know is it's IP address. Is that possible to use Exchange Web Service to access the server? This is my code:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.Credentials=new NetworkCredential("StevenZack","123456","208.243.49.20");
//service.AutodiscoverUrl("stevenzack#mail2.local");
service.Url = new Uri("https://208.243.49.20/EWS/Exchange.asmx");
FindItemsResults findResults = service.FindItems(
WellKnownFolderName.Inbox,
new ItemView(10));
foreach (Item item in findResults.Items)
{
div_email.InnerHtml = item.Subject + "<br />";
}
It comes out with an error of "The remote certificate is invalid according to the validation procedure"
How to deal with this situation? Any idea? Thank you for your time!
The SSL warning can be avoiding by setting the ServicePointManager.ServerCertificateValidationCallback delegate (see http://www.infinitec.de/post/2008/11/26/ExchangeWebServices-WebDAV-and-untrusted-server-certificates.aspx).
But this probably won't help you. If you specify 208.243.49.20 you are connecting to the machine using a machine-local account instead of your domain account.
Can you, by any chance, use integrated authentication? You could just impersonate the user and use his credential directly. This way, the user doesn't have to enter his credentials again. Note that you would need to configure Kerberos if your web application resides on a different server as your Exchange server.

c# programmatically reading emails from the Exchange server

When you search on web you will find very easy answers for "How to read emails programmatically"... Al the websites are explaining most of the same like this page.
http://omegacoder.com/?p=454
// depends from Exchange server version
service.Credentials = new NetworkCredential("MDR", "password", "zzz");
service.AutodiscoverUrl("mdr#zzz.be");
object o = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
foreach (Item item in findResults.Items)
{
Console.WriteLine(item.Subject);
}
it fails when it executes the autodiscoverURL line. The error says "The Autodiscover service couldn't be located."
So I googled further and found this site from Microsoft https://www.testexchangeconnectivity.com/#&&/wEXAQUBcwUBME93h2+JjI0+MV2gTqcRL0g43z9m Here you can test your mail server....
When I pass the parameters I get the error below....
But I still don't understand what the problem is? Do I need to add a record to DNS ? Can someone help?
Attempting to test potential Autodiscover URL https://autodiscover.zzz.be/AutoDiscover/AutoDiscover.xml
Testing of this potential Autodiscover URL failed.
Test Steps
Attempting to resolve the host name autodiscover.ncb.be in DNS.
The host name resolved successfully.
Additional Details
IP addresses returned: 213.246.192.205
Testing TCP port 443 on host autodiscover.ncb.be to ensure it's listening and open.
The specified port is either blocked, not listening, or not producing the expected response.
Tell me more about this issue and how to resolve it
Additional Details
A network error occurred while communicating with the remote host.
Exception details:
Message: No connection could be made because the target machine actively refused it 213.246.192.205:443
Type: System.Net.Sockets.SocketException
Stack trace:
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at Microsoft.Exchange.Tools.ExRca.Tests.TcpPortTest.PerformTestReally()
Attempting to contact the Autodiscover service using the HTTP redirect method.
The attempt to contact Autodiscover using the HTTP Redirect method failed.
Test Steps
Attempting to resolve the host name autodiscover.zzz.be in DNS.
The host name resolved successfully.
Additional Details
IP addresses returned: 213.246.192.205
Testing TCP port 80 on host autodiscover.zzz.be to ensure it's listening and open.
The port was opened successfully.
ExRCA is checking the host autodiscover.zzz.be for an HTTP redirect to the Autodiscover service.
ExRCA failed to get an HTTP redirect response for Autodiscover.
Additional Details
A Web exception occurred because an HTTP 404 - NotFound response was received from IIS7.
Attempting to contact the Autodiscover service using the DNS SRV redirect method.
ExRCA failed to contact the Autodiscover service using the DNS SRV redirect method.
Test Steps
Attempting to locate SRV record _autodiscover._tcp.ncb.be in DNS.
The Autodiscover SRV record wasn't found in DNS.
Tell me more about this issue and how to resolve it
You don't necessarily need to use the autodiscovery if you already know the address of your exchange server. Try the following instead (for more info, look here:
service.Url = new Uri("https://hostname/EWS/Exchange.asmx");
Replace "hostname" with the hostname for your exchange server.
I hope you should have the solution by this time now. This is just to help anyone bumped on this post.
I found the solution on one of the technet article, I twick to suite me, and is working fine for me.
Just replace the line in your code with following:
service.AutodiscoverUrl("user#yourdomain.com",
delegate
{
return true;
});
I had some other issues but not related to this bit though.
Happy Coding,
Sanjay.
I had the same issue with AutoDiscover. It's not necessary, you can specify your URL like
Uri myUri = new Uri("https://Hostname/ews/exchange.asmx");
userData.AutodiscoverUrl = myUri;
service.Url = myUri;
As the hostname you can put the Server IP address like 192.168.100.10
Alternatively, to find what your Exchange server hostname is (in in fact the whole url to use) if you are using Outlook, go to your computer start bar, where the Date and time is showing, you will find the Outlook icon, hold Ctrl + right click on the outlook icon and click “Test Email Auto Configuration”
Check the "Use AutoDiscover" checkbox. Enter an email address hosted on that Exchange Server along with its password and you will recieve a bunch of url's. Use the 1 that says "Availability Service URL"
Consider that the credentials being passed need to have permission to the given exchange mailbox / server. In my case using a different set of credentials that are properly permissioned works but not for a service account which I'm trying to get to work.
Once I discover what exactly the account needs to be permissioned for I will update it here.
Update: My issue was the service account was from a domain different than the domain on which the exchange 2007 instance is running, even though there is a trust relationship between the two. I found this is a documented known issue in Exchange 2007 in how it looks up accounts in its forest. In the end had to create an identical service account (name/pass) on the domain on which the exchange server is sitting and specify username as {exchange_domain}{service_account_name}. The windows service that calls EWS runs as {original_domain}{service_account_name}.
For reference, the exception was:
Microsoft.Exchange.WebServices.Data.ServiceResponseException:
Failed to get valid Active Directory information for the calling account. Confirm that it is a valid Active Directory account.

Categories