登录
首页 >  Golang >  Go问答

Azure AD B2C用户无法访问Graph API

来源:stackoverflow

时间:2024-03-26 20:42:35 137浏览 收藏

Azure AD B2C用户无法访问Graph API,尽管在Azure AD Graph Explorer中可以成功访问。使用Go程序编辑配置文件策略时,出现“令牌已过期”错误。此外,在添加“user.read”范围后,又出现“此应用程序对此Web资源没有足够的权限”错误。

问题内容

我的测试用户 id 为 [email protected](没有全局管理员权限),并且我正在尝试访问 azure ad 的 graph api。

尝试 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 来获取令牌。我使用 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(失败)

我尝试在 azure ad b2c > 应用程序 > 中添加 user.read <应用程序名称> >已发布范围并使用完整范围url,现在我收到错误为error:aadb2c90205:此应用程序对此web资源没有足够的权限来执行操作。

我不确定这里出了什么问题。知道如何克服这个问题吗?


解决方案


aad b2c 是 aad 的专门实例。您可以将其视为带有一些 b2c 扩展的 aad 租户。注意:这是与您组织的主 aad 租户不同的租户,您已在其中创建了 b2c 目录/功能!

您可以通过 aad graph api 访问 b2c 记录,只需 2 个步骤:

  1. 通过向 aad 端点(例如 https://login.microsoftonline.com/yourtenant.onmicrosoft.com)提供 clientid 和 clientsecret 来获取 aad graph 令牌。
  2. 使用所需方法(get/post/patch/delete)连接到 aad graph rest 端点(例如 https://graph.windows.net/yourtenant.onmicrosoft.com/users?api-version=1.6),并在请求的身份验证标头中向其传递在步骤 1 中获得的令牌。

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

您应该查看 b2cgraphclient.cs 中的 sendgraphpostrequest 方法及其朋友。该代码使用 adal 获取 aad graph 令牌,但您也可以直接通过 rest 请求获取它。 c# 的简化版本(您必须自己将其翻译为 go,如果 go 中不可用,则可能需要替换 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));
            }

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>