从ExchangeWebService基本身份验证迁移到OAuth2.0

时间:2019-09-24 19:19:11

标签: c# asp.net oauth-2.0 exchangewebservices

我的目标是迁移我们的Exchange连接以使用OAuth2.0,因此我们将在2020年删除基本身份验证。

我使用基本身份验证的当前代码为:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials(MailBox, Password, "domamer");
try
{
    service.AutodiscoverUrl(MailBox, RedirectionUrlValidationCallback);
}
catch
{
    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
}

在此处查看Microsoft提供的文档:(link) 我对以下代码进行了编码,并期望它将取代上面的代码。

var pcaOptions = new PublicClientApplicationOptions
{
    ClientId = AppSettings.GetOauthClientID(),
    TenantId = AppSettings.GetOauthTenantID()
};

var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();

// The permission scope required for EWS access
var ewsScopes = new string[] { "https://outlook.office.com/EWS.AccessAsUser.All" };

// Make the interactive token request
var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();

// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, MailBox);

我认为新代码中的“ ewsClient”等同于原始代码中的“ service”。

当我尝试逐步执行项目时,它仅在这一行结束:

var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();

我已经仔细检查了我的ClientID,TenantID是否正确。

以前有人遇到过这个问题吗?可能的解决方案或需要检查的东西?

我尝试使用“尝试/捕获”功能,希望得到一条错误消息,但我从未遇到在所有Console.WriteLine上设置的断点。它只是锁定并在ExecuteAsync()行停止响应

try
{
    // Make the interactive token request
    var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();

    // Configure the ExchangeService with the access token
    var ewsClient = new ExchangeService();
    ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
    ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, EmailBox);

    Console.WriteLine("Made it Here");
}
catch (MsalException ex)
{
    Console.WriteLine($"Error acquiring access token: {ex.ToString()}");
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.ToString()}");
}

1 个答案:

答案 0 :(得分:1)

在与Microsoft通电话后,他们努力帮助我使其正常运行。花了一段时间,但是我终于能够使用以下代码建立与Microsoft服务的OAuth连接:

List<DataCenterCluster> entityListToDaoList(List<Dccs> source);

@Mappings({
        @Mapping(source = "updatedDate", target = "updatedDate", dateFormat = "yyyy-MM-dd'T'HH:mm:ss"),
    })
    DataCenterCluster entityToDao(Dccs source);

然后,我可以连接到该服务并使用类似于我之前所做的用户帐户进行身份验证

@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    """
    Signals the Profile about User creation.
    """
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()