我的测试用户ID为test@gollahalliauth.onmicrosoft.com
(无全局管理员权限),并且我正在尝试访问Graph API for Azure AD。
尝试1次(成功)
我使用了Azure AD Graph Explorer,使用test@gollahalliauth.onmicrosoft.com
登录并使用API https://graph.windows.net/gollahalliauth.onmicrosoft.com/users/test@gollahalliauth.onmicrosoft.com
来获取内容。我能够做到这一点而没有任何问题。
尝试2次(失败)
我写了一个具有配置文件编辑策略的Go程序
import (
"crypto/rand"
"encoding/base64"
"fmt"
"golang.org/x/oauth2"
"os"
)
const AuthDomainName string = "https://gollahalliauth.b2clogin.com/gollahalliauth.onmicrosoft.com/oauth2/v2.0"
func main() {
conf := &oauth2.Config{
ClientID: os.Getenv("clientID"),
ClientSecret: os.Getenv("clientSecret"),
RedirectURL: "http://localhost:8080/callback",
Scopes: append([]string{"openid", "profile"}),
Endpoint: oauth2.Endpoint{
AuthURL: AuthDomainName + "/authorize?p=b2c_1_gollahalli_edit",
TokenURL: AuthDomainName + "/token?p=b2c_1_gollahalli_edit",
},
}
// Generate random state
b := make([]byte, 32)
rand.Read(b)
state := base64.StdEncoding.EncodeToString(b)
parms := oauth2.SetAuthURLParam("response_type", "id_token")
url := conf.AuthCodeURL(state, parms)
fmt.Println("AUth URL:",url)
}
这将创建一个身份验证URL以获取令牌。我使用id_token
使用Authorization: Barer id_token
访问图形API,但收到错误消息
{
"odata.error": {
"code": "Authentication_ExpiredToken",
"message": {
"lang": "en",
"value": "Your access token has expired. Please renew it before submitting the request."
}
}
}
尝试3次(失败)
我尝试在User.Read
中添加Azure AD B2C > Applications >
<application name> > Published scopes
并使用了完整范围的URL,现在我得到了一个错误,名称为Error: AADB2C90205: This application does not have sufficient permissions against this web resource to perform the operation.
我不确定这是什么问题。关于如何克服这个想法?
答案 0 :(得分:0)
AAD B2C是AAD的专门实例。您可以将其视为具有某些B2C扩展名的AAD租户。注意:这是与组织的主要AAD租户不同的租户,您已经在其中创建了B2C目录/功能!
您可以通过2个步骤通过AAD Graph API访问B2C记录:
最好的例子可能是MS提供的用户迁移工具。 here涵盖了AAD B2C配置,可以从documentation page或直接从Github project下载示例代码。
您应该查看B2CGraphClient.cs中的SendGraphPostRequest方法及其朋友。该代码使用ADAL获取AAD Graph令牌,但是您也可以直接通过REST请求获取它。 C#的简化版本(您必须将其自己翻译为GO,如果GO中不提供ADAL,则可以替换ADAL):
// NOTE: This client uses ADAL v2, not ADAL v4
AuthenticationResult result = aadAuthContext.AcquireToken(Globals.aadGraphResourceId, aadCredential);
HttpClient http = new HttpClient();
string url = Globals.aadGraphEndpoint + tenant + api + "?" + Globals.aadGraphVersion;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await http.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formatted = JsonConvert.DeserializeObject(error);
throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
}