【发布时间】:2015-10-24 00:04:45
【问题描述】:
上周我在这里提出了一个关于 EWS 的问题,我收到错误消息:
401:未经授权 - 访问令牌无效
我设法通过使用 X.509 证书而不是客户端凭据(来自 AAD 的客户端 ID 和客户端密码)来解决此错误。现在,使用证书,我收到一条新的错误消息:
403:禁止 - 范围不足
我认为这与 AAD 中的权限有关?
我的权限如下(只有一个权限):
应用程序权限:从所有邮箱读取和写入电子邮件
我如何接收访问令牌:
//Create the certificate file, using the path (certFile), password (certPassword) and the MachineKeySet
X509Certificate2 cert = new X509Certificate2(certFile, certPassword, X509KeyStorageFlags.MachineKeySet);
//Create the ClientAssertionCertificate using the clientID and the actual certificate
ClientAssertionCertificate cac = new ClientAssertionCertificate(clientID, cert);
//Retreive the access token using the serverName and client assertion
authenticationResult = authenticationContext.AcquireToken(serverName, cac);
//authenticationResult = authenticationContext.AcquireToken(serverName, cc);
ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2013);
exchange.Url = new Uri(serverName + "ews/exchange.asmx");
exchange.TraceEnabled = true;
exchange.TraceFlags = TraceFlags.All;
exchange.Credentials = new OAuthCredentials(authenticationResult.AccessToken);
当像这样调用FindItems 方法时:
ItemView view = new ItemView(5);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
var tempId = id.Replace('-', '/').Replace('_', '+');
SearchFilter.IsEqualTo searchid = new SearchFilter.IsEqualTo(ItemSchema.Id, tempId);
// This results in a FindItem call to EWS.
FindItemsResults<Microsoft.Exchange.WebServices.Data.Item> results = exchange.FindItems(WellKnownFolderName.Inbox, searchid, view);
出现错误。
有人能解释一下什么可能导致这种错误吗?
【问题讨论】:
标签: c# asp.net-mvc office365 exchangewebservices azure-active-directory