困在Azure OAuth2令牌请求中的两个错误之间

Phi*_*son 9 asp.net-identity azure-active-directory asp.net-identity-2

我正在为OWIN和Azure Active Director实现OAuth2提供程序.FWIW,此时OpenId Connect选项不符合此工作的要求.

我获得了一个auth代码,并使用auth_code,state返回到我的回复url,并将令牌请求发送到"scheme://login.windows.net/ {myguid}/oauth2/token".

 // Build up the body for the token request
 var body = new List<KeyValuePair<string, string>>();
 body.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
 body.Add(new KeyValuePair<string, string>("code", code));
 body.Add(new KeyValuePair<string, string>("redirect_uri", redirectUri));
 body.Add(new KeyValuePair<string, string>("client_id", Options.ClientId));
 body.Add(new KeyValuePair<string, string>("client_secret", Options.ClientSecret));

 // Request the token
 HttpResponseMessage tokenResponse =
     await httpClient.PostAsync(TokenEndpoint, new FormUrlEncodedContent(body));
 string text = await tokenResponse.Content.ReadAsStringAsync();
 tokenResponse.EnsureSuccessStatusCode();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

{"error":"invalid_resource","error_description":"AADSTS50001: Resource identifier is not provided.
Trace ID: 227f2af8-0837-4f22-ac0f-a09b3f9a6d50
Correlation ID: 3d783f11-44d0-4efa-8831-3dd581d653ed
Timestamp: 2014-08-08 21:59:49Z","error_codes":[50001],"timestamp":"2014-08-08 21:59:49Z","trace_id":"227f2af8-0837-4f22-ac0f-a09b3f9a6d50","correlation_id":"3d783f11-44d0-4efa-8831-3dd581d653ed"}
Run Code Online (Sandbox Code Playgroud)

好的,我添加了资源选项:

 // Build up the body for the token request
 var body = new List<KeyValuePair<string, string>>();
 body.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
 body.Add(new KeyValuePair<string, string>("code", code));
 body.Add(new KeyValuePair<string, string>("redirect_uri", redirectUri));
 body.Add(new KeyValuePair<string, string>("client_id", Options.ClientId));
 body.Add(new KeyValuePair<string, string>("client_secret", Options.ClientSecret));
 body.Add(new KeyValuePair<string, string>("resource", "https://myappid"));

{"error":"invalid_request","error_description":"AADSTS90027: The client 'xxxxx' and resource 'https://myappid' identify the same application.
Trace ID: 6c77f123-d75f-43a9-8117-b3f372891ee4
Correlation ID: d9081f8b-b690-4478-bf15-55325a9736ec
Timestamp: 2014-08-08 21:48:34Z","error_codes":[90027],"timestamp":"2014-08-08 21:48:34Z","trace_id":"6c77f123-d75f-43a9-8117-b3f372891ee4","correlation_id":"d9081f8b-b690-4478-bf15-55325a9736ec"}
Run Code Online (Sandbox Code Playgroud)

所以我必须拥有与我的客户ID相关联的正确应用ID.hrrmph!我显然做错了什么但似乎无法看到它.有什么建议?

wut*_*aer 20

我遇到了同样的问题,我只想实现用户登录.

在尝试了1000件事(其中包括这篇文章)后,我发现我可以使用Microsoft.Azure.ActiveDirectory-id作为资源参数.通过这种方式,我不必创建第二个应用程序.

http://blogs.msdn.com/b/besidethepoint/archive/2012/10/23/getting-started-with-azure-active-directory.aspx

nameValuePairs.add(new BasicNameValuePair("resource", "00000002-0000-0000-c000-000000000000"));
Run Code Online (Sandbox Code Playgroud)

得到了令牌

更新:

azure支持建议我使用https://graph.windows.net/:

nameValuePairs.add(new BasicNameValuePair("resource", "https://graph.windows.net/"));
Run Code Online (Sandbox Code Playgroud)


gye*_*gye 8

在授权请求中使用"openid"作用域应触发OpenID Connect流,该流将返回id_token并且不需要资源.


Dus*_*ill 6

OAuth涉及4方:1)资源所有者aka用户2)资源应用程序:通常是用于保护用户对资源所有者的访问的Web API 3)客户端应用程序:Web应用程序或移动应用程序或甚至是想要访问的其他Web API代表用户的资源4)权限:对用户和/或客户端应用程序进行身份验证的安全令牌服务,并向客户端发出委托访问令牌以访问资源.

您的代码使用与客户端应用程序相同的标识符以及资源 - 实际上它正在尝试请求访问令牌来访问自身.可以说应该允许这种情况 - 但Azure AD今天不是这样.

请执行以下操作:在Azure AD中注册资源应用程序.在其清单中添加一个新的appPermission(按照这篇文章).然后,转到客户端应用程序配置页面并滚动到底部 - 在"对其他应用程序的权限"部分中,将资源权限添加到"委派权限"的客户端应用程序列表中.

现在,在您的OAuth请求中使用资源应用程序的AppIDURI或ClientID,这些东西应该可行.

希望这可以帮助.