none
Random Unauthorized error RRS feed

  • Question

  • We have an MVC app that connects to the Exchange server. We used to connect to an on premises server using this code to create the service:

     if (string.IsNullOrEmpty(Current.UserPassword))
                {
                    throw new UnauthorizedAccessException("Exchange access requires Authentication by Password");
                }
                return new ExchangeService
                    {
                        Credentials = new NetworkCredential(Current.User.LoginName, Current.UserPassword),
                        Url = new Uri(ConfigurationManager.AppSettings["ExchangeServiceUrl"]),
                    };

    This worked fine, but now our IT department is migrating the Exchange server to the cloud, and some users are on the cloud server while others are on premises. So I changed the code to this:

      if (string.IsNullOrEmpty(Current.UserPassword))
                {
                    throw new UnauthorizedAccessException("Exchange access requires Authentication by Password");
                }
                var user = ConfigurationManager.AppSettings["ExchangeUser"];
                var password = ConfigurationManager.AppSettings["ExchangePassword"];
                var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
                {
                    Credentials = new NetworkCredential(user, password),
                };
                exchangeService.AutodiscoverUrl(Current.EmaiLuser + "@calamos.com", RedirectionCallback);
                exchangeService.Credentials = new NetworkCredential(Current.EmaiLuser + "@calamos.com", Current.UserPassword);
                return exchangeService;
    I am using a service account to do the autodiscovery ( for some reason it doesn't work with a regular account) and then I am changing the credentials of the service to the user that logs in, so he can access the inbox. The problem is that , randomly, the server returns "The request failed. The remote server returned an error: (401) Unauthorized.". 
    I asked the IT department to check the Exchange logs, but there is nothing there about this error, so I don't know how to fix it...




    • Edited by Oana Marina Thursday, August 11, 2016 2:13 PM
    Thursday, August 11, 2016 1:59 PM