通过API的OAuth2资源所有者密码授予 [英] OAuth2 Resource Owner Password Grant via API

查看:191
本文介绍了通过API的OAuth2资源所有者密码授予的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在构建一个需要OAuth2的API,但找不到要使用的库,该库只能通过RESTful API处理本机移动应用程序中的单点登录。我发现的大多数人只有一个网络弹出窗口,该弹出窗口已被该项目否决。当前正在运行的B2C无法使用ROPG。有没有一种方法可以使用C#.NET和Azure轻松地与另一个库进行设置?

I am currently building an API which requires OAuth2, but cannot find a library to use that will handle the single sign on in a native mobile app via RESTful API only. Most I've found only have a web popup, which has been vetoed for this project. B2C, which is currently functioning, is not capable of using ROPG. Is there a way to easily set this up with another library using C#.NET and Azure?

更新:

根据下面的Fei Xue答案尝试使用B2C,我们到了从Microsoft Graph获取访问令牌的地步。
在POST正文中,我们执行了以下操作:

Attempting to use B2C per Fei Xue answer below, we got to the point of getting an access token from Microsoft Graph. In the body of the POST, we did the following:


resource = https%3A%2F%2FGraph.windows。 net& client_id = [B2C设置-
应用程序-
AppId]& grant_type = password& username = rob%40 [tenant] .onmicrosoft.com& password = [password]& client_secret = [B2C设置-应用程序-应用程序密钥-client_secret]

resource=https%3A%2F%2FGraph.windows.net&client_id=[B2C Settings - Applications - AppId]&grant_type=password&username=rob%40[tenant].onmicrosoft.com&password=[password]&client_secret=[B2C Settings - Applications - App Key - client_secret]

我们命名空间的错误是由于我们尝试使用的用户名所致。这是一个使用电子邮件作为用户名的B2C租户,这就是名称空间错误的原因。克服该错误的唯一方法是创建一个B2C用户,电子邮件地址以租户结尾,例如:

Our error with the namespace was due to the usernames we were trying. This is a B2C tenant using email as the username and that was the reason for the namespace error. The only way we got past that error was to create a B2C user with the email address ending in the tenant, like so:


rob @ [tenant] .onmicrosoft.com

rob@[tenant].onmicrosoft.com

我们现在正在获取访问令牌,但是该令牌无法通过我们的Azure应用服务API进行身份验证应用程序,这是最初的目标。

We are getting an access token now, but that token does not authenticate with our azure app service api app, which was the original goal.

我们要完成的工作是发送对B2C登录有效的用户名和密码,并获取IdToken或访问令牌对api应用有效。 api应用程序通过使用客户端ID和B2C设置应用程序中的秘密设置为AAD配置的App Service Authentication设置连接到B2C。

What we are trying to accomplish is to send the username and password that is valid for a B2C signin and get an IdToken or Access Token that is valid for the api app. The api app connects to B2C via App Service Authentication settings configured for AAD with the Client ID and secret setup from the B2C Settings Application.

更新:

尝试在我们的Azure Web api中通过graph.windows.net令牌进行身份验证,我们在 https://Graph.windows.net 在我们的App Service –身份验证– Active Directory身份验证配置中允许令牌访问者。

Attempting to pass through the graph.windows.net token for authentication in our Azure web api, we added in the https://Graph.windows.net allowed token audience in our App Service – Authentication – Active Directory Authentication configuration.

但是,将Bearer标头中的图形访问令牌传递给API仍会导致

However, passing the graph access token in the Bearer header to the API still results in


此请求的授权已被拒绝。

"Authorization has been denied for this request".

发现如果我们将发卡人网址设为空白,例如

Found out that if we make the Issuer Url blank like in the example below, it now accepts the Graph token!

但是,这会在尝试命中

$时引起问题b $ b

https:// [our_web_app] .azurewebsites.net /。 auth / login / aad

https://[our_web_app].azurewebsites.net/.auth/login/aad

现在转到常见的Microsoft登录名。以前,由于发卡行网址设置为:

It goes to the common Microsoft login now. Previously it directed to our B2C sign up in policy because the Issuer Url was set to:


https://login.microsoftonline.com/ [ tenantname] .onmicrosoft.com / v2.0 / .well-known / openid-configuration?p = [B2C_SignUpIn_Policy]

实际上,如果我们还将应用程序中的策略(在删除Issuer Url之前已运行)上拉到登录策略,则可以登录,但是返回的访问令牌现在始终会返回为未授权 Web API调用。

In fact, if we also pull up the policy from within our app (which was working before removing the Issuer Url) to the sign in policy, we can sign in, but that returned Access Token now always comes back as Unauthorized in the web API calls.

发卡行网址是否应留空?

,由于将Issuer Url设为空白,所以当我们使用标头授权载体中的图形访问令牌发送请求时,服务器需要更长的时间来响应API调用。从花费大约1-2秒(使用从MSAL或上面的Web登录获取的有效B2C访问令牌)到花费大约10-15秒来响应它是经过身份验证的请求。那样的速度对我们来说是制胜法宝。 以这种方式验证图形调用通常会花费这么长时间吗?

Also, since making the Issuer Url blank, the server takes much longer to respond to API calls when we send a request using a Graph access token in the Header Authorization Bearer. It went from taking about 1-2 seconds (using a valid B2C access token obtained from MSAL or the web login above) to taking about 10-15 seconds to respond that it is an authenticated request. That kind of speed is a show stopper for us. Does validating a graph call in this manner normally take this long?

推荐答案

该功能现在可用在预览中效果很好:

The feature is now available in preview and works pretty well:

https://docs.microsoft.com/zh-cn/azure/active-directory-b2c/configure-ropc

重要说明:文档中提到的POST URL错误。

https://login.microsoftonline.com/{{Aad_Tenant}}/b2c_1_ropc_auth/oauth2/v2.0/

必须为:

https://login.microsoftonline.com/{{Aad_Tenant}}/oauth2/v2.0/token?p=b2c_1_ropc_auth

调用应用程序必须启用了本机客户端,否则您将收到此错误:

The calling application must have native client enabled, otherwise you will get this error:


AADB2C90224:尚未为该应用程序启用资源所有者流。

AADB2C90224: Resource owner flow has not been enabled for the application.

这篇关于通过API的OAuth2资源所有者密码授予的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆