我的测试用户ID为[email protected](无全局管理员权限),并且我试图访问Graph API for Azure AD。

尝试1次(成功)

我使用Azure AD Graph Explorer,使用[email protected]登录并使用API​​ https://graph.windows.net/gollahalliauth.onmicrosoft.com/users/[email protected]获取内容。我能够做到这一点而没有任何问题。

尝试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以获取 token 。我使用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并使用了完整范围的网址,现在我收到了Error: AADB2C90205: This application does not have sufficient permissions against this web resource to perform the operation.的错误提示

我不确定这是什么问题。关于如何克服这个想法?

最佳答案

AAD B2C是AAD的专门实例。您可以将其视为具有某些B2C扩展名的AAD租户。注意:这是与组织的主要AAD租户不同的租户,您已经在其中创建了B2C目录/功能!

您可以通过2个步骤通过AAD Graph API访问B2C记录:

  • 通过将ClientID和ClientSecret提供给AAD终结点(例如https://login.microsoftonline.com/yourtenant.onmicrosoft.com)来获取AAD图 token 。
  • 使用所需的方法(GET/POST/PATCH/DELETE)连接到AAD Graph REST端点(例如https://graph.windows.net/yourtenant.onmicrosoft.com/users?api-version=1.6),并将在步骤1中获得的 token 传递给请求的Authentication header 。

  • 最好的例子可能是MS提供的用户迁移工具。 AAD B2C配置包含在here中,可以从documentation page或直接从Github project下载示例代码。

    您应该在B2CGraphClient.cs中查看SendGraphPostRequest方法及其 friend 。该代码使用ADAL获取AAD Graph token ,但是您也可以直接通过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));
                }
    

    关于azure - 使用Azure AD B2C时无法访问Graph API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56899363/

    10-16 05:12