为什么使用ClientCredential进行AcquireToken会失败,提示invalid_client(ACS50012)?

9
为什么我的Azure AD应用程序不允许oauth client_credentials授权?
我想使用Azure Graph API,但首先需要一个oauth令牌。为了获取令牌,我正在尝试使用Microsoft.IdentityModel.Clients.ActiveDirectory(也称为ADAL版本1.0.3(来自NuGet))。
我正在使用AuthenticationContext.AcquireToken的重载,该重载接受ClientCredential对象。(我无法使用提示用户登录的重载,因为我正在编写服务,而不是应用程序。)
我按照各种教程/示例(例如ADAL-服务器对服务器身份验证)中所述配置了我的Azure AD Web应用程序。
我的代码如下:
AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/thommmondago.onmicrosoft.com");
ClientCredential cc = new ClientCredential("41151135-61b8-40f4-aff7-8627e9eaf853", clientSecretKey);
AuthenticationResult result = ac.AcquireToken("https://graph.windows.net", cc);

AcquireToken行抛出了一个异常:

sts_token_request_failed: Token request to security token service failed.  Check InnerException for more details

内部异常是WebException,收到的响应看起来像是oauth错误:
{ "error":"invalid_client",
 "error_description":"ACS50012: Authentication failed."
 "error_codes":[50012],
 "timestamp":"2014-03-17 12:26:19Z",
 "trace_id":"a4ee6702-e07b-40f7-8248-589e49e96a8d",
 "correlation_id":"b304af2e-2748-4067-99d0-2d7e55b121cd" }

绕过ADAL并使用oauth端点的curl也会导致相同的错误。如果我使用我在这里发现的Azure应用程序的详细信息,则我的代码可以正常工作。
AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/graphDir1.onmicrosoft.com");
ClientCredential cc = new ClientCredential("b3b1fc59-84b8-4400-a715-ea8a7e40f4fe", "FStnXT1QON84B5o38aEmFdlNhEnYtzJ91Gg/JH/Jxiw=");
AuthenticationResult result = ac.AcquireToken("https://graph.windows.net", cc);

所以这不是我的代码出错了。我认为这要么是与我的 Azure AD 有关的错误,要么是我使用的 ClientCredential 参数有误。


如果您的代码可以使用示例凭据正常工作,那么这似乎是您配置方面的问题。您是否在Azure管理门户中设置了您的域,即APP ID URI?这必须与您的“资源”参数匹配。另外,请重新检查您的客户端密钥。 - davo
我想要一个令牌的资源是Graph API,所以我将APP ID URI设置为图形终结点(从“查看终结点”按钮中获取),并在资源参数中使用它。同时生成了一个新的秘钥。结果相同。 - tjleigh
你能尝试使用https://graph.windows.net作为APP ID URI吗?这就是我在同样的场景下使用的。 (抱歉,必须在URL中放一个空格以防止自动格式化) - davo
感谢您的帮助,最终问题是 Azure 的问题。FYI,我无法将应用程序 ID URI 设置为 https://graph.windows.net,Azure 阻止了它。 - tjleigh
4个回答

6

事实证明这是一个Windows Azure的错误,我的代码或配置没有问题。

在Microsoft修复了Azure中的问题之后,我不得不创建一个新应用程序,然后它开始工作了。

来自Microsoft的论坛答案:

Hi,

We are seeing some errors with applications created in a several day time range, ending yesterday. We are continuing to fix up these applications but I don't have a good eta when this will be done. I'm apologize for the impact here.

Can you try creating a new application and retying the operation with the new client id?

thanks


1

文档链接已损坏 - Michael Freidgeim

1

我遇到了相同的问题,但只有在Azure(在Azure网站内部)直接运行代码时才会出现。

我解决了这个问题,升级了'Microsoft.IdentityModel.Clients.ActiveDirectory'包到'2.6.1-alpha'。



0

翻译专注于编程,以下是内容:

微软Azure版本的翻译器再次更改了一些东西 - Oauth令牌请求使用新的URL,只需要您的秘密密钥,而不是其他所有信息。这个页面讨论它(但是使用PHP代码):http://www.bradymoritz.com/php-code-for-bingmicrosoftazure-translator/

关键要点如下:

  1. https://api.cognitive.microsoft.com/sts/v1.0/issueToken 发送一个空请求
  2. 使用标题“Ocp-Apim-Subscription-Key:”将您的秘密密钥传递给它
  3. 或者,只需使用查询字符串参数:“Subscription-Key =”

然后将返回的正文作为实际令牌获取-它是整个正文,不是JSON格式。

这比以前使用的方法要简单得多,但是事情再次改变肯定很痛苦。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接