none
EWS The request failed. The remote server returned an error: (401) Unauthorized RRS feed

  • Question

  • Hello,

    I am trying to create appointment from Exchange Web Service, If I am trying to login to exchange server from URL then its working and allowing me to login to my exchange server but if I am trying to connect from my C# application then I am getting this error "The request failed. The remote server returned an error: (401) Unauthorized."

    My current code is as below:

    ExchangeService service = new ExchangeService();
    service.Credentials = new WebCredentials("XXX\username", "pwd", "exchange.domain.com");
    
    service.Url = new Uri("https://exchange.domain.com/EWS/Exchange.asmx"); 
    Appointment appointment = new Appointment(service);
    appointment.Subject = "Status Meeting";
    appointment.Body = "The purpose of this meeting is to discuss status.";
    appointment.Start = new DateTime(2017, 1, 30, 9, 0, 0);
    appointment.End = appointment.Start.AddHours(2);
    appointment.Location = "Conf Room";
    appointment.RequiredAttendees.Add("xyz@domain.com");
    appointment.Save(SendInvitationsMode.SendToNone);

    If I am trying to login from url and its also working fine.
    exchange.domain.com
    username - XXX\username
    pwd - "pwd"

    Also I have tried to login web service from URL and its work for me.

    Please anyone suggest what's the wrong with C# code and how I can make working it.

    Thanks.

    Monday, January 30, 2017 8:49 AM

Answers

All replies

  • You credentials aren't specified correctly you should use either downlevel format which is domain\username which would be

    service.Credentials = new WebCredentials("XXX\username", "pwd");

    (Note you don't specify the domain as you have already included it in the downlevel format)

    or use

    service.Credentials = new WebCredentials("username", "pwd","XXX");

    (Where XXX is the NetBIOS domain name not the FQDN like you have used) this is recommended method because of potential issue with UPN suffixes https://support.microsoft.com/en-us/help/2933452/-401-unauthorized-error-when-you-use-an-ews-application-to-impersonate-a-user-in-office365-dedicated-itar

    or use the Active Directory User Principal name which includes the domain name eg

    service.Credentials = new WebCredentials("username@upndomain.com", "pwd");
    Cheers
    Glen

     

    • Marked as answer by NH Kariya Tuesday, January 31, 2017 5:49 PM
    Tuesday, January 31, 2017 3:43 AM
  • Hello Glen,

    Thank you so much. Its working for me.

    service.Credentials = new WebCredentials("username", "pwd","XXX")

     Cheers.

    NH Kariya

    Tuesday, January 31, 2017 5:51 PM