【问题标题】:EWS error message: "403: Forbidden - Not enough scopes"EWS 错误消息:“403:禁止 - 范围不足”
【发布时间】: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


    【解决方案1】:

    只有 Office 365 REST API 支持精细访问,例如“从所有邮箱读取和写入电子邮件”。对于 EWS,您需要“使用具有对所有邮箱的完全访问权限的 Exchange Web 服务”的权限。如果您在查找此权限时遇到问题,请告诉我们。

    【讨论】:

    • 当我收到一条新消息时,事情进展顺利:An ExchangeImpersonation SOAP header must contain a user principal name, user SID, or primary SMTP address
    • 对于模拟错误,您需要正确设置模拟:_exchange.HttpHeaders.Remove("X-AnchorMailbox"); _exchange.HttpHeaders.Add("X-AnchorMailbox", targetEmail); _exchange.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, targetEmail);
    【解决方案2】:

    OAuth 流程不采用 X509Certificate2 身份验证。您应该在 AAD 中注册多租户应用程序(可以使用 Exchange Online)。通过 OAuth 进行身份验证时,访问邮箱需要以下 3 个委托权限:

    • 阅读邮件
    • 阅读日历
    • 以登录用户身份通过​​ Exchange Web 服务访问邮箱

    要授予您应用程序的访问权限,应将用户重定向到https://login.microsoftonline.com/common/oauth2/authorize(带有相应的参数)。授予权限后,您会收到带有授权码的响应,该授权码应该被交换以访问/刷新令牌:

    
        ClientCredential credential = new ClientCredential(clientId, appKey);
        AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/common", false);
        var url = new Uri(Request.Url.GetLeftPart(UriPartial.Path));
        AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                    code, url, credential, "https://outlook.office365.com/");
     

    其中 clientIdappKey - 注册应用程序的参数,code - 是从 OAuth 响应中收到的授权码。

    【讨论】:

    • 所以你是在告诉我使用验证码而不是证书?
    • @Detilium,是的,有了授权码,一切都应该工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2017-02-27
    • 2021-03-21
    • 2019-02-28
    • 1970-01-01
    相关资源
    最近更新 更多