[英]Microsoft Graph API tests - get access on behalf of user
我有使用 Graph API (.Net Core 3.1) 為用戶創建擴展的代碼。 我有這個代碼的測試項目。 但是我需要以用戶身份進行身份驗證才能創建和使用 GraphServiceClient(用戶具有全局管理員角色)。
目標是擁有一個為User創建schemaExtension的工作代碼。
現在,要創建擴展,客戶端必須具有已授予門戶中注冊應用程序的委托權限Directory.AccessAsUser.All
。 但由於這是委托權限,我需要以用戶身份進行身份驗證(在測試代碼中)。 所以我的選項是身份驗證提供者:
List<string> scopes = new List<string> { "Directory.AccessAsUser.All" };
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(_appClientId.ToString())
.WithRedirectUri(_redirectUri)
.WithClientSecret(_appSecret) // or .WithCertificate(certificate)
.Build();
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);
_graphServiceClient = new GraphServiceClient(authProvider);
我得到一個例外:
Microsoft.Graph.Auth.AuthenticationException :代碼:authenticationChallengeRequired
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(_appClientId.ToString())
.WithRedirectUri(_redirectUri)
.WithClientSecret(_appSecret)
.Build();
OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
_graphServiceClient = new GraphServiceClient(authProvider);
我得到
空引用異常
當我嘗試實際創建架構時,在這一行:
SchemaExtension extension = await _graphServiceClient .SchemaExtensions.Request().AddAsync(schemaExtension);
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.Build();
InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);
我得到:
Microsoft.Identity.Client.MsalClientException :僅支持環回重定向 uri,但找到了 urn:ietf:wg:oauth:2.0:oob。 在應用注冊期間和創建 PublicClientApplication 對象時配置http://localhost或http://localhost:port 。
而這最后一個我完全不明白。 那么我怎樣才能使這個委托的身份驗證工作呢?
這是創建擴展但不依賴於授權的代碼:
SchemaExtension schemaExtension = new SchemaExtension
{
Id = schemaName.Trim(),
// Owner = _appClientId.ToString(),
Description = string.IsNullOrWhiteSpace(schemaDesc) ? string.Empty : schemaDesc.Trim(),
TargetTypes = new List<string>
{
"User"
},
Properties = new List<ExtensionSchemaProperty>
{
new ExtensionSchemaProperty
{
Name = "isGlobalAdmin",
Type = "Boolean"
},
new ExtensionSchemaProperty
{
Name = "isOrganizationAdmin",
Type = "Boolean"
}
}
};
SchemaExtension extension = await GraphClient.SchemaExtensions.Request().AddAsync(schemaExtension); // GraphClient here === _graphServiceClient in the code above
根據我的研究,不同的 Microsoft Graph 提供程序使用不同的協議,適用於不同的環境。 更多詳情請參考文檔
它使用OAuth 2.0 授權代碼流。 正常情況下,我們用於web app訪問web api的情況。 更多詳情請參考文檔
它使用OAuth 2.0 授權代碼流。 通常,我們將它用於桌面應用程序(例如 WPF)。 此外,請注意,當我們將提供程序與 MSAL.NET 一起使用時, 我們必須將“http://localhost”注冊為 AD 應用程序的公共客戶端(移動和桌面)重定向 URI 。 更多詳情請參考文檔
如果我們想使用Interactive provider來調用Microsoft graph,請參考以下步驟
static async Task Main(string[] args)
{
var clientId = "476944ed-e57c-4b2c-b18d-93b5dd5f1bca";
string[] scopes = { "Directory.AccessAsUser.All" };
#please provide the redirect url http://localhost when you create the client
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithRedirectUri("http://localhost")
.Build();
InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);
var graphClient = new GraphServiceClient(authProvider);
var schemaExtension = new SchemaExtension
{
Id = "courses",
Description = "Graph Learn training courses extensions",
TargetTypes = new List<string>()
{
"Group"
},
Properties = new List<ExtensionSchemaProperty>()
{
new ExtensionSchemaProperty
{
Name = "courseId",
Type = "Integer"
},
new ExtensionSchemaProperty
{
Name = "courseName",
Type = "String"
},
new ExtensionSchemaProperty
{
Name = "courseType",
Type = "String"
}
}
};
var result = await graphClient.SchemaExtensions.Request().AddAsync(schemaExtension);
foreach (var type in result.TargetTypes) {
Console.WriteLine(type);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.