gpt4 book ai didi

c# - 如何在 C# 中以编程方式将服务主体分配给工作区

转载 作者:行者123 更新时间:2023-12-03 03:45:19 25 4
gpt4 key购买 nike

我已在 AAD 中创建了服务主体,并且能够在 https://app.powerbi.com/home 中手动分配到工作区

我想以编程方式将服务主体分配给所有工作区。

有什么办法吗?

请帮忙

谢谢

最佳答案

是的,您可以使用Power BI REST API并调用Update Group Useradd the service principal to the workspace :

请求:

PUT https://api.powerbi.com/v1.0/myorg/groups/f089354e-8366-4e18-aea3-4cb4a3a50b48/users

请求正文:

{
"identifier": "1f69e798-5852-4fdd-ab01-33bb14b6e934",
"groupUserAccessRight": "Admin",
"principalType": "App"
}

要使用 API,您必须对自己进行身份验证,例如使用 ADALMSALHere is an example如何使用 MSAL 获取访问 token :

private static async Task<string> GetToken()
{
// TODO: Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
// and add using Microsoft.IdentityModel.Clients.ActiveDirectory

//The client id that Azure AD created when you registered your client app.
string clientID = "{Client_ID}";

//RedirectUri you used when you register your app.
//For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
// You can use this redirect uri for your client app
string redirectUri = "https://login.live.com/oauth20_desktop.srf";

//Resource Uri for Power BI API
string resourceUri = "https://analysis.windows.net/powerbi/api";

//OAuth2 authority Uri
string authorityUri = "https://login.microsoftonline.com/common/";

//Get access token:
// To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
// AuthenticationContext is part of the Active Directory Authentication Library NuGet package
// To install the Active Directory Authentication Library NuGet package in Visual Studio,
// run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.

// AcquireToken will acquire an Azure access token
// Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
AuthenticationContext authContext = new AuthenticationContext(authorityUri);
var token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri)).Result.AccessToken;

Console.WriteLine(token);
Console.ReadLine();

return token;
}

当您调用 API 时,必须将此 token 添加到请求 header 中:

//Add token to the request header
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

关于c# - 如何在 C# 中以编程方式将服务主体分配给工作区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69417374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com