使用EWS创建与c#的约会

使用EWS创建与c#的约会,c#,visual-studio,outlook,calendar,exchangewebservices,C#,Visual Studio,Outlook,Calendar,Exchangewebservices,我想创建一个程序,使其能够在其他人的outlook日历中创建约会。例如:如果有人向他的老板要求5天的免费时间,老板需要能够批准并立即将其显示在此人的outlook日历中。我尝试使用EWS编写代码,但总是出现以下错误: Microsoft.Exchange.WebServices.dll中发生类型为“Microsoft.Exchange.WebServices.Data.AutoDiscoveryLocalException”的未处理异常 其他信息:自动发现阻止了到的潜在不安全重定向 这是我的密码

我想创建一个程序,使其能够在其他人的outlook日历中创建约会。例如:如果有人向他的老板要求5天的免费时间,老板需要能够批准并立即将其显示在此人的outlook日历中。我尝试使用EWS编写代码,但总是出现以下错误: Microsoft.Exchange.WebServices.dll中发生类型为“Microsoft.Exchange.WebServices.Data.AutoDiscoveryLocalException”的未处理异常 其他信息:自动发现阻止了到的潜在不安全重定向

这是我的密码:

    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("username@domain.com", "password");
        service.AutodiscoverUrl("username@domain.com");
        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));
    }
}
}

我真的希望有人能帮我解决这个问题。

您需要使用AutoDiscoveryURL重载,它允许您指定回调验证

     service.AutodiscoverUrl("username@domain.com",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;

    }

Office 365总是进行重定向,所以需要这样的操作,您可以通过验证服务器名称等,使更多的验证代码更安全,以防止人在中间攻击等。 干杯


Glen

复制/粘贴您遇到的错误,以便在得到答复后让其他人找到您的解决方案。图像没有索引。您是否尝试过直接设置服务的URL而不是使用自动发现?我尝试过手动添加URL,但可能是错误的。如果我想登录,我应该使用as域吗?不管怎样,我得到了相同的错误代码。请把完整的错误信息作为文本发送,您已经在中间停止了。另外,请将发生错误的代码作为文本输入。然后可以删除该图像。