我正在尝试编写一个应用程序,该应用程序允许用户从我的应用程序中查询其Azure订阅中的网站列表。我不想将他们的凭据存储在我的应用程序中(他们也不希望我这样做),而是让他们让他们从其Azure AD实例中注册应用程序,然后让他们存储在我的应用程序中生成的ClientID和TenantID应用程式。我将向他们提供一组说明,说明如何从其Azure门户使用尽可能严格的权限进行操作,并且他们可以随时关闭。他们需要像我一样舒服地做到这一点。
我正在尝试遵循本文的内容:Using Azure resource graph with .net SDK
它使我非常接近,但是当我运行该应用程序时,我从Azure资源图得到了“禁止访问”响应,因此该呼叫通过了但被拒绝了。我还尝试使用自己的Azure门户添加各种API权限进行测试。文章说,我需要使用以下方法创建服务主体:
az ad sp create-for-rbac -n“ MyApp”-角色贡献者--sdk-auth
我还没有完成。
所以我的问题是:
这是我的问题吗?
我是否需要让用户创建服务主体,如果是,那么在上面的示例中,“ MyApp”变量中包含什么值?
是否有更好的方法来实现这一目标,因此期望更少的用户?
我感谢任何人都可以提供的任何指导或文章推荐。
非常感谢。...
如果要使用服务主体调用Azure资源图Rest API,则必须将Azure RABC Role分配给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
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://docs.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create
根据您的需求,我认为您可以创建一个Web应用程序来调用Azure资源图API。如果这样做,则可以提供Web应用程序的URL,然后客户可以登录您的应用程序并自行调用API。1.注册Azure AD Web应用程序
配置代码。请参考sample