与Exchange Server 2013的ews(交换Web服务)身份验证

时间:2014-11-18 03:30:31

标签: c# authentication exchange-server exchangewebservices

我是使用c#和交换Web服务的新用户,我遇到了问题。    我要做的是在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();

        }
    }

当我在visio studio中调试它时,这很有效,然后我将它放入IIS,没有权利! 我发现在Exchange服务器的日志文件中,会记录错误代码401。然后我发现ews的身份验证设置为“ base,NTLM,Windows身份验证,Windows SharePoint安全,OAuth ”。

我问我怎样才能更改代码以获得这些身份验证。 谢谢!


请原谅我英语不好。

1 个答案:

答案 0 :(得分:0)

最后我得到了这段代码! 起初,我的代码是这样的:

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

它在visio studio中运行良好,因为交换机对visio studio的请求做了一些特别的事情,它忽略了认证部分。所以,当我把它放在iis中时,由于凭证不对,这不起作用!

所以我将代码更改为:

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

猜猜是什么?它奏效了!

我花了两天时间才最终解决这个问题,这是一个什么样的错误!

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

在我阅读msdn上的api文档后,我发现第一个不起作用的原因是因为两个arguments方法用于基本身份验证,而具有三个参数的方法用于标识NTLM身份验证。