【问题标题】:EWS: "The remote server returned error (401) Unauthorized"EWS:“远程服务器返回错误 (401) 未经授权”
【发布时间】:2016-01-17 12:00:57
【问题描述】:

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

请求失败。远程服务器返回错误:(401) Unauthorized.

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

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);

然后我定义我想要接收的项目(通过 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);

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

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

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

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

编辑

我确实从以下位置收到访问令牌:

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

我可以通过调试代码看到。

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

编辑

我只是设法调试到exchange.ResponseHeaders,在那里我看到了这个:

访问令牌是使用一种身份验证方法获取的,该方法是 太弱,无法访问此应用程序。提交身份验证 强度为 1,要求为 2

decoded the 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]

从这里到哪里去?

【问题讨论】:

  • 我建议硬设置“exchange.Credentials”,直到您完成身份验证为止......因为它似乎是您的问题......
  • exchange.Credentials = new WebCredentials(authEmailAddress, authEmailPassword, "DOMAINIFYOUHAVEONE");
  • @Seabizkit 这行得通。这就是我开始时所做的,效果很好,但我想使用 OAuth
  • @Detilium 参考此link Np 并使用aud 检查剩余项目
  • @Webruster 我不确定你的意思? audhttps://outlook.office365.com/哪个是正确的?

标签: c# asp.net-mvc office365 exchangewebservices


【解决方案1】:

我在过去使用 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);

关键部分是定义一个新的 ClientAssertionCertificate 作为你的 ClientAssertion。

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

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

【讨论】:

  • 您能否编辑您的答案并详细解释为什么需要此证书?是不是因为该服务的访问权限如此广泛,所以应用需要信任运行该服务的 PC?
  • 除了建立一个比仅拥有应用程序机密的信任更安全的信任之外,我不知道其他原因。我认为“窃取/知道”à秘密(字符串)比à证书更容易。这是 EWS 让您拨打电话的唯一方式。
  • 你得到我的 +50 代表,因为这是唯一的答案,而且在某种程度上是正确的答案,尽管我希望你能告诉我更多关于证书的信息。我设法将错误从401: Unauthorized 更改为403: Forbidden,我相信这与AAD 的许可有关。无论如何感谢您的回答,并享受您的 50 代表 :)
  • 嗨@Trysior / Detilium 我正在尝试对客户端进行基于证书的相同验证,但是在调用 await authenticationContext.AcquireTokenAsync("outlook.office365.com", MyClientAssertionCertificate); .知道如何克服它吗?
猜你喜欢
  • 2012-04-29
  • 2012-02-23
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 2012-09-19
相关资源
最近更新 更多