如何从应用程序(客户端)ID 和目录(租户)ID 获取对象 ID?

问题描述 投票:0回答:2

使用azure go sdk,是否可以使用

Application (client) ID
Directory (tenant) ID
、有效的
Client secret
获取Azure Active Directory应用程序的
Object ID
?怎么样?

这里是 Azure 门户的屏幕截图,以帮助阐明这三个字段。

我尝试使用 graphrbac 中的以下函数,但

*result.Value
与上面屏幕截图中的
Object ID
不匹配。

func (client ApplicationsClient) GetServicePrincipalsIDByAppID(ctx context.Context, applicationID string) (result ServicePrincipalObjectResult, err error)

azure go azure-active-directory
2个回答
1
投票

您问题中的功能是获取服务主体的 ID(位于门户中的

Enterprise applications
),而不是屏幕截图中的 azure 广告应用程序(位于
App registrations
)。

似乎没有这样的功能可以通过您要使用的属性获取应用程序的

objectid
。 (我不熟悉go,如果有不对的地方,请指正。)最相似的是
func (ApplicationsClient) Get
需要
objectid
,或者
func (ApplicationsClient) List
列出应用程序。


0
投票

我想你现在可能不需要这个,但以防万一有人试图做同样的事情。下面是我目前的做法,但感觉超级 hacky(这就是为什么我试图查找是否有人可以通过 MSGraph 或更优雅的方式实现它)。

它依赖于以下机制:

  1. 使用 azure-sdk-for-go 的
    azidentity
    模块创建客户端。所有客户端类型都实现了 azcore 的
    TokenCredential
    接口。有了这个你就可以获得客户端的 JWT 令牌。
  2. 使用客户端获取令牌,然后提取对象ID(声明
    oid
    )。你可以使用
    golang-jwt
    来做到这一点。

以下是我将其组合在一起的方式(警告:不完整且不安全):

    client, _:= azidentity.NewClientSecretCredential(tenantId, clientId, clientSecret,
        &azidentity.ClientSecretCredentialOptions{
            ClientOptions: azcore.ClientOptions{
                Cloud: cloud.AzurePublic,
            },
        })

    tokenString, _:= client.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{"https://management.azure.com/.default"}})

    type custom struct {
        ObjectId string `json:"oid"`
        jwt.StandardClaims
    }

    p := jwt.NewParser(jwt.WithoutClaimsValidation())
    c := &custom{}
    p.ParseUnverified(tokenString.Token, c)
    fmt.Println(c.ObjectId)


© www.soinside.com 2019 - 2024. All rights reserved.