2016-02-24 82 views
0

我想创建一个程序,使其能够在别人的Outlook日历中创建约会。例如:如果有人向老板提出5天免费,老板需要批准并立即将其显示在个人的Outlook日历中。我试着用EWS编码,但是我总是得到这个错误:enter image description here Microsoft.Exchange.WebServices.dll中发生未处理的“Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException”类型的异常 附加信息:自动发现阻止了潜在的不安全重定向到使用EWS创建与c#的约会

这里是我的代码:

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using Microsoft.Exchange.WebServices.Data; 

namespace exchangetest 
{ 
public partial class Test1 : Form 
{ 

    public Test1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     ExchangeService service = new ExchangeService(); 
     service.UseDefaultCredentials = true; 
     service.Credentials = new WebCredentials("[email protected]", "password"); 
     service.AutodiscoverUrl("[email protected]"); 
     Appointment appointment = new Appointment(service); 

     // Set the properties on the appointment object to create the appointment. 
     appointment.Subject = "Tennis lesson"; 
     appointment.Body = "Focus on backhand this week."; 
     appointment.Start = DateTime.Now.AddDays(2); 
     appointment.End = appointment.Start.AddHours(1); 
     appointment.Location = "Tennis club"; 
     appointment.ReminderDueBy = DateTime.Now; 

     // Save the appointment to your calendar. 
     appointment.Save(SendInvitationsMode.SendToNone); 

     // Verify that the appointment was created by using the appointment's item ID. 
     Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject)); 
    } 
} 
} 

我真的希望有人能帮助我与此有关。

+0

复制/粘贴你越来越让别人找到你的解决方案,一旦它会回答错误。图像没有编入索引。 – phaberest

+0

您是否尝试过直接设置服务的URL,而不是使用AutoDiscover? –

+0

我曾尝试手动添加URL,但也许我做错了。如果我想在[link](https://login.live.com/)上登录,我应该使用https://login.live.com/作为域名吗?无论如何,我有相同的错误代码。 –

回答

0

您需要使用AutoDiscoverURL重载允许你指定一个回调确认例如

 service.AutodiscoverUrl("[email protected]",adAutoDiscoCallBack); 

     internal static bool adAutoDiscoCallBack(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; 

    } 

Office365总是做一个重定向所以像这种需要,你可以把更多的验证代码,使其更安全防止中间人攻击等通过验证服务器名称等

干杯 格伦

+0

谢谢,它工作 –