0

我正在尝试编写一个应用程序,该应用程序允许用户从我的应用程序中从他们的 Azure 订阅中查询网站列表。我不想将他们的凭据存储在我的应用程序中(他们也不希望我这样做),而是要求他们从 Azure AD 实例中注册一个应用程序,然后让他们存储在我的应用程序中生成的 ClientID 和 TenantID应用程序。我会给他们一组说明,说明如何从他们的 Azure 门户中执行此操作,该门户使用尽可能严格的权限,并且他们可以随时关闭。他们需要像我一样感到舒服。

我正在尝试遵循本文的情节: Using Azure resource graph with .net SDK

它让我非常接近,但是当我运行应用程序时,我从 Azure Resource Graph 收到“禁止”响应,因此呼叫正在通过但被拒绝。我还尝试使用我自己的 Azure 门户添加各种 API 权限进行测试。文章说我需要使用以下方法创建服务主体:

az ad sp create-for-rbac -n "MyApp" --role 贡献者 --sdk-auth

我还没有做过。

所以我的问题是:

  • 这是我的问题吗?

  • 我是否需要让我的用户创建一个服务主体,如果需要,上面示例中的“MyApp”变量中有什么值?

  • 有没有更好的方法来减少对用户的期望?

我感谢任何人可以提供的任何指导或文章推荐。

非常感谢....

4

1 回答 1

1

如果要使用服务主体调用 Azure 资源图 Rest API,则必须将Azure RABC 角色分配给 sp。所以你必须运行以下命令

# it will create a service pricipal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp"  --scope "/subscriptions/<subscription id>" --sdk-auth


# if you have a service principal, please run the following script
az ad sp list --display-name "{name}" --query [].objectId --output tsv
az role assignment create --assignee <sp object id> --role "Contributor " --scope "/subscriptions/<subscription id>"

同时,有关如何运行示例的详细步骤如下 1. 运行以下脚本。

az login
az ad sp create-for-rbac -n "MyApp"  --scope "/subscriptions/<subscription id>" --sdk-auth

在此处输入图像描述

  1. 配置代码。请注意,如果要在不同的订阅中调用资源,则需要通过在 step1 中更改范围值来创建不同的服务主体
public  async static Task Test() {


            CustomLoginCredentials creds = new CustomLoginCredentials();

            var resourceGraphClient = new ResourceGraphClient(creds);

            var queryReq = new QueryRequest {

                Subscriptions = new List<string> { "<the subscriptionId you copy>" },
                Query = "where type == 'microsoft.web/sites'"

            };
            var result = await resourceGraphClient.ResourcesAsync(queryReq);
            Console.WriteLine(result.Count);
        }

class CustomLoginCredentials : ServiceClientCredentials {
        private static string tenantId = "<the tenantId you copy >";
        private static string clientId = "<the clientId you copy>";
        private static string cert = "<the clientSecre tyou copy>";
        private string AuthenticationToken { get; set; }
        public override void InitializeServiceClient<T>(ServiceClient<T> client)
        {
            var authenticationContext =
                new AuthenticationContext("https://login.microsoftonline.com/"+tenantId);
            var credential = new ClientCredential(clientId: clientId, clientSecret: cert);

            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/",
                clientCredential: credential).Result;

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }

            AuthenticationToken = result.AccessToken;
        }
        public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (AuthenticationToken == null)
            {
                throw new InvalidOperationException("Token Provider Cannot Be Null");
            }



            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);



            await base.ProcessHttpRequestAsync(request, cancellationToken);

        }

更多详情,请参阅

https://odetocode.com/blogs/scott/archive/2018/02/01/setting-up-service-principals-to-use-the-azure-management-apis.aspx

https://docs.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create


更新

根据您的需要,我认为您可以创建一个 Web 应用程序来调用 Azure Resource Graph API。如果您这样做,您可以提供 Web 应用程序的 url,然后您的客户可以登录您的应用程序并自己调用 API。1.注册Azure AD web应用 在此处输入图像描述

2.配置权限 在此处输入图像描述

  1. 创建客户端密码 在此处输入图像描述

  2. 配置代码。请参考样本

于 2020-02-10T01:30:28.597 回答