Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am wondering if it is possible to pass credentials of current user to ExchangeService object.

Currently I am providing specific UserName/Password to WebCredentials in order to authenticate the ExchangeService access but instead I want to use credential of current user by using SPContext.Current.Web.CurrentUser.UserToken.

Here my code which create appointments:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials("UserName", "password", "domain.contoso.com");
service.Url = new Uri("https://computername.domain.contoso.com/EWS/Exchange.asmx");

Appointment appointment = new Appointment(service);
foreach (MailAddress MailAddress in MailAddresses)
{
    appointment.RequiredAttendees.Add(MailAddress.Address);
}

appointment.Subject = Title;
appointment.Body = Summary;
appointment.Start = StartDate;
appointment.End = EndDate;
appointment.IsResponseRequested = true;
appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
share|improve this question
    
did you find any solution? –  Schiavini Sep 25 '12 at 11:44
    
Yes, I decided to go with impersonation and it's working great for last six months now. –  Firoz Ansari Sep 25 '12 at 14:15
add comment

3 Answers

up vote 6 down vote accepted

You can impersonate the user account.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new NetworkCredential("serviceAccount", "serviceAccountPassword", "domain");
service.Url = new Uri("https://computername.domain.contoso.com/EWS/Exchange.asmx"); 
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress,"userEmail");

Refer to msdn for more information. Hope this helps.

share|improve this answer
1  
Thanks for your reply. To use impersonation, I will require to enable impersonation property for serviceAccount user. Will it not create any security risk in whole organization? Any developer with serviceAccount/Password can access others mail-box without their consent. –  Firoz Ansari Feb 14 '12 at 16:23
add comment

This should work:

service.Credentials=CredentialCache.DefaultCredentials;
share|improve this answer
1  
Thanks Amit. I am getting this error with DefaultCredentials. Erorr: "When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids." –  Firoz Ansari Feb 14 '12 at 17:46
add comment

If your authentication is set to NTLM then this is not an option due to the well-known double hop issue. In this case, you must create a new token in the middle layer as you are doing now. This does open a security hole as anyone with access to the code can see the user ID and Password needed to access Exchange. You could always encrypt and store these values somewhere (like the registry) and pull them from there instead of hard-coding them. This is a better option but still not ideal.

If you are using Kerberos then passing the token should work, provided that both your site and the Exchange server are also configured properly.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.