none
EWS Managed API - The request failed. The remote server returned an error: (401) Unauthorized for one logon id RRS feed

  • Question

  • Hi all,

    I recently try to use EWS Managed API to access exchange email account from two LAN ID

    My scenario is that the two LAN IDs (let's say ID_A, ID_B) can access the email inbox, I proved

    this by using outlook accessing the email account (It prompts me to enter LAN ID and Password)

    Then I use the following codes to see if I can use EWS Managed API with windows authentication to access the email inbox

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    service.TraceEnabled = false;
    Console.Out.WriteLine("Try using windows authentication!");
    service.Credentials = new WebCredentials(CredentialCache.DefaultNetworkCredentials);
    service.Url = new Uri("https://xxxhost/ews/exchange.asmx");
    Console.Out.WriteLine("Connected to Exchange!");
    FindItemsResults<Item> findResults = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox("HK.XX@xxxhost.com")), new ItemView(100));

    With ID_A logon, the C# program can access the email inbox with windows authentication and print out the email subjects

    But Wth ID_B logon, the C# program returns the following error:

    Exception : The request failed. The remote server returned an error: (401) Unauthorized.

    I really don't get it from one account can work but another is failed to work.

    Both accounts should have access read right to the email account.

    Would some experts here help me on this issue?

    Thanks for your kind attention,

    RayLee

    Tuesday, December 3, 2013 6:04 PM

Answers

  • What type of application are you trying to create ? is this ASPX ?

    In a Console App you should just use

    service.UseDefaultCredentials = true;

    which will use the currently logged on credentials.

    You can test both credentials against EWS using the EWSEditor http://ewseditor.codeplex.com/ to check for issue outside of your code

    Cheers
    Glen

    • Marked as answer by RLee1010 Wednesday, December 18, 2013 4:31 PM
    Wednesday, December 4, 2013 4:44 AM

All replies

  • What type of application are you trying to create ? is this ASPX ?

    In a Console App you should just use

    service.UseDefaultCredentials = true;

    which will use the currently logged on credentials.

    You can test both credentials against EWS using the EWSEditor http://ewseditor.codeplex.com/ to check for issue outside of your code

    Cheers
    Glen

    • Marked as answer by RLee1010 Wednesday, December 18, 2013 4:31 PM
    Wednesday, December 4, 2013 4:44 AM
  • Hi Glen,

    I use your code and it solves the problem.

    Thanks,

    Ray

    Tuesday, December 17, 2013 5:43 PM