Search code examples
active-directorysingle-sign-onexchangewebservices

Connect to Exchange with AD User (Single Sign On)


At the moment I have a Login-Form with Username (logonParamters.Username) and Password (logonParameters.Password) fields. The code below is working fine but it presupposes that I have a Password to connect to Exchange. But we want to use Single Sign On and if we do so we don't have a Password right? But EWS wants an Username, Password and Domain. So how do I connect to the Exchange using SSO?

            var domain = logonParameters.UserName.Split('\\').First();
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain))
                {
                // validate the credentials
                var username = logonParameters.UserName.Split('\\').Last();
                bool isValid = pc.ValidateCredentials(username, logonParameters.Password);
                if (!isValid)
                    {
                    //throw exception
                    }
                using (UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username))
                    {                      
                    //Connect to ExchangeWebService with those AD Credentials.
                    }
                }

Solution

  • In the EWS Managed API (or WSDL proxy) you can use the current security context (eg logged on user creds) like

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016);
    service.UseDefaultCredentials = true;
    

    which means getting it to run in a impersonation context and relying on kerberos to do the auth if possible.