title: ASP.NET MVC 5 Sending an Email with Federated Authentication Azure AD
body: <p>I've burnt hours trying to figure this out, I hope someone can help.  Users authenticate to our ASP.NET site with Azure AD (Microsoft organizational account).  Ideally I'd like to be able to connect with Exchange Web Service but I'm having trouble figuring out how to pass the credentials.  From searching I see there is no way to get the password from User.Identity.  </p>
  
  <p>I'm having the same issues with Pop or IMAP.</p>
  
  <p>This code is returning "The Autodiscover Service Couldn't be Located"
  If I explicitly tell it the the server name I get a 401 Unauthorized Error</p>
  
  <pre><code>ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
  service.UseDefaultCredentials = true;
  service.AutodiscoverUrl(User.Identity.Name, RedirectionUrlValidationCallback);
  
          private static bool RedirectionUrlValidationCallback(string redirectionUrl)
          {
              // The default for the validation callback is to reject the URL.
              bool result = false;
  
              Uri redirectionUri = new Uri(redirectionUrl);
  
              // Validate the contents of the redirection URL. In this simple validation
              // callback, the redirection URL is considered valid if it is using HTTPS
              // to encrypt the authentication credentials. 
              if (redirectionUri.Scheme == "https")
              {
                  result = true;
              }
              return result;
          }
  </code></pre>