EWS:"远程服务器返回错误(401)未经授权"

Det*_*ium 5 c# asp.net-mvc exchangewebservices office365

我正在尝试在当前上下文中找到所有项目的单个项目,但我似乎经常收到此错误消息:

请求失败.远程服务器返回错误:(401)未经授权.

首先,我设置一切以访问交换服务:

var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

AuthenticationResult authenticationResult = null;
AuthenticationContext authenticationContext = new AuthenticationContext(
            SettingsHelper.Authority, new model.ADALTokenCache(signInUserId));

authenticationResult = authenticationContext.AcquireToken(
            SettingsHelper.ServerName, 
            new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret));

ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2013);
exchange.Url = new Uri(SettingsHelper.ServerName + "ews/exchange.asmx");
exchange.TraceEnabled = true;
exchange.TraceFlags = TraceFlags.All;
exchange.Credentials = new OAuthCredentials(authenticationResult.AccessToken);
Run Code Online (Sandbox Code Playgroud)

然后我定义了我想要接收的项目(通过ID):

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);
Run Code Online (Sandbox Code Playgroud)

最后但并非最不重要的是,我尝试在我的项目中搜索此项目:

FindItemsResults<Microsoft.Exchange.WebServices.Data.Item> results = exchange.FindItems(WellKnownFolderName.Inbox, searchid, view);
Run Code Online (Sandbox Code Playgroud)

这就是我的错误发生的地方.我尝试了其他各种方法,但无论我做什么,我都会受到未经授权.

有人可能会以正确的方式指导我,以解决这个问题吗?

编辑

我确实从以下地址收到了访问令牌:

authenticationResult = authenticationContext.AcquireToken(
            SettingsHelper.ServerName, 
            new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret));
Run Code Online (Sandbox Code Playgroud)

正如我通过调试代码所看到的那样.

在此输入图像描述

虽然没有刷新令牌,我不知道这是否有话要说?

编辑

我只是设法调试到exchange.ResponseHeaders我看到的地方:

使用身份验证方法获取访问令牌,该身份验证方法太弱而无法访问此应用程序.提出的auth强度为1,要求为2

解码了JWT,因为这是我的结果:

{
  typ: "JWT",
  alg: "RS256",
  x5t: "MnC_VZcATfM5pOYiJHMba9goEKY",
  kid: "MnC_VZcATfM5pOYiJHMba9goEKY"
}.
{
  aud: "https://outlook.office365.com/",
  iss: "https://sts.windows.net/d35f5b06-f051-458d-92cc-2b8096b4b78b/",
  iat: 1445416753,
  nbf: 1445416753,
  exp: 1445420653,
  ver: "1.0",
  tid: "d35f5b06-f051-458d-92cc-2b8096b4b78b",
  oid: "c5da9088-987d-463f-a730-2706f23f3cc6",
  sub: "c5da9088-987d-463f-a730-2706f23f3cc6",
  idp: "https://sts.windows.net/d35f5b06-f051-458d-92cc-2b8096b4b78b/",
  appid: "70af108f-5c8c-4ee4-a40f-ab0b6f5922e0",
  appidacr: "1"
}.
[signature]
Run Code Online (Sandbox Code Playgroud)

然后去哪儿?

小智 5

过去使用EWS时,我已经遇到了此错误“访问令牌是使用认证方法获得的,该方法太弱而无法允许对此应用程序进行访问。目前的身份验证强度为1,要求为2”

您需要做的是使用证书强制执行身份验证。

AuthenticationContext authContext = new AuthenticationContext(authority);

exchangeService.Credentials = new OAuthCredentials(authContext.AcquireToken("https://outlook.office365.com", new ClientAssertionCertificate(ConfigurationManager.AppSettings["ida:ClientId"], certificate)).AccessToken);
Run Code Online (Sandbox Code Playgroud)

关键部分是在定义ClientAssertion时定义一个新的ClientAssertionCertificate。

您还必须修改Azure Active Directory应用程序的清单。

查看此参考资料(有关“为您的应用程序配置X.509公共证书”的部分):https : //msdn.microsoft.com/zh-cn/office/office365/howto/building-service-apps-in- office-365