0
votes

I'm a new user using c# and exchange web service and I got a problem. What I'm trying to do is to add an appointment to the calendar of outlook.

    ExchangeService service = null;
    public WebService1()
    {
        service = new ExchangeService(ExchangeVersion.Exchange2013);
        service.UseDefaultCredentials = false;
        service.Credentials = new WebCredentials("user1@mydomain`enter code here`.com", "password");


        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;




        service.Url = new Uri("https://mydomain/EWS/Exchange.asmx");
        service.TraceListener = new TraceListener();
    // Optional flags to indicate the requests and responses to trace.
        service.TraceFlags = TraceFlags.All;
        service.TraceEnabled = true;
        service.TraceListener.Trace("begin", service.HttpHeaders.ToString());}

       [WebMethod]
        public string addMeeting(string userNameList, string subject, string content, string beginDate, string endDate, string location="", string optionalUserList="", string remindTime="")
    {
        try
        {
            Appointment meeting = Appointment.Bind(service, new ItemId("meeting"));

            if (userNameList == "")
            {
                return "FAIL: NO USER IS ADDED! PLEASE CHECK AGAIN!";
            }
            meeting.Subject = subject.Trim() == "" ? "未知主题" : subject;
            meeting.Body = content.Trim() == "" ? "未知内容" : content;
            meeting.Start = DateTime.Parse(beginDate);
            meeting.End = DateTime.Parse(endDate);
            meeting.Location = location == "" ? "未知地点" : location;
            string[] userListArr = userNameList.Split(new Char[] { ';', ',' });
            foreach (string s in userListArr)
            {
                if (s.Trim() != "")
                {
                    meeting.RequiredAttendees.Add(s);
                }
            }

            string[] opUserListArr = optionalUserList.Split(new Char[] { ';', ',' });
            foreach (string s in opUserListArr)
            {
                if (s.Trim() != "")
                {
                    meeting.OptionalAttendees.Add(s);
                }
            }
            remindTime = remindTime.Trim() == "" ? "60" : remindTime;
            meeting.ReminderMinutesBeforeStart = int.Parse(remindTime);
            meeting.Save(SendInvitationsMode.SendToAllAndSaveCopy);
            return "SUCCESS";
        }
        catch (Exception ex)
        {

            return ex.Message + "\n" + ex.StackTrace + "\n\n" + ex.Source +" \n" + ex.ToString();

        }
    }

And this works well when I debug it in visio studio, and then I put it into IIS, NOTHING GOES RIGHT! And I found that in the log file on the exchange server, error code 401 is logged. And then I found that the ews' authentication is set to be "base, NTLM, Windows authentication, Windows SharePoint safty, OAuth".

I'm asking how can I change my code to get these authentications. Thanks!


Forgive me of my poor English.

1
Are you trying to use the logged on users credentials? I would start by looking at the information and sample in blogs.msdn.com/b/emeamsgdev/archive/2012/11/05/…Glen Scales

1 Answers

0
votes

Finally I get this code worked! At first, my code is like this:

service.Credentials = new NetCredential("Domain\\myUserName", "password");

And it works well in visio studio as exchange has done something special to the request from visio studio that it ignore the authentication part. So when I put it in iis, this could not work as the credential is not right!

So I changed my code to this:

service.Credentials = new NetCredential("myUserName", "password", "Domain");

And guess what? It worked!

It took me two days to finally fix this, What A Bug!

==============================================================

After I read the api document on msdn, I found that the reason why the first one does not work is because the two arguments method is used on Basic authentication, and the method which has three arguments is used to identify NTLM authentication.