如何在 Microsoft Graph API 版本 5.19 c# 中分配许可证?
当我在网上搜索时,我找不到可以在版本5或更高版本上使用的方法。
当您尝试升级到 v5 时,此库对本文进行了重大更改 https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md
第一步你需要像这样获取token
var interactiveBrowserCredential = new InteractiveBrowserCredential(interactiveBrowserCredentialOptions);
var graphServiceClient = new GraphServiceClient(interactiveBrowserCredential);
或者像自定义身份验证流程一样,可以创建 IAccessTokenProvider 的实现来完成
public class TokenProvider : IAccessTokenProvider
{
public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = default,
CancellationToken cancellationToken = default)
{
var token = "token";
// get the token and return it in your own way
return Task.FromResult(token);
}
public AllowedHostsValidator AllowedHostsValidator { get; }
}
创建 GraphServiceClientCreating 实例
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new YourAuthenticationProvider()); // Authorization method
var graphClient = new GraphServiceClient(authenticationProvider);
插入用于删除和添加的许可证。确保此数据在licensesToRemove或licensesToAdd中有价值
var licensesToRemove = new List<Guid?> { };
var license = new AssignedLicense { SkuId = "yourLicenceId" };
List<AssignedLicense> licensesToAdd = new List<AssignedLicense> { license };
进行查询
await graphClient.Users["yourUserId"].AssignLicense.PostAsync(new Microsoft.Graph.Users.Item.AssignLicense.AssignLicensePostRequestBody()
{
RemoveLicenses = licensesToRemove,
AddLicenses = licensesToAdd
});