2016-02-24 106 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

謝謝,它工作 –