しばらく、Microsoft Graph APIを触っていなかったのだが、Graph APIライブラリを使用して作成したアプリケーションのターゲット.NETフレームワークバージョンがサポートされなくなるため、再ビルドをしようとして、使用しているパッケージのバージョンを上げようと、パッケージをnuget.orgを見たら、Microsoft.Graph.AuthがDeprecate候補(元々Preview版だったけど)になっていたので、MSのサイトで調べたら、ここにAzure.Identityライブラリを使用しろやと書かれていた。
で、修正してみたのが以下のコード。(ユーザー名/パスワード プロバイダーの例)
/* Microsoft.Graph.Authは削除対象となったので、Azure.Identityに変更
この部分は不必要となった(以下見づらくて済まソ)
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(ServiceId)
.WithAuthority(TenantId)
.Build();
*/
// 必要とする権限(Microsoft.Graph.Auth⇒Azure.Identity) 指定フォーマットが変わった
//string [] scopes = new string[] { "https://graph.microsoft.com/User.Read", "https://graph.microsoft.com/Group.Read.All" };
string [] scopes = new string[] { "User.Read", "Group.Read.All" };
/* Graph API Authorization Providerの作成(Microsoft.Graph.Auth⇒Azure.Identity)
Providerは不要となり、GraphServiceClientにはCredentialとScopeを指定する。
var provider = new UsernamePasswordProvider(app,scopes);
// Graph API Clientの作成
var graphClient = new GraphServiceClient(provider);
*/
// Credentialの作成(Microsoft.Graph.Auth⇒Azure.Identity)
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var cred = new UsernamePasswordCredential(username,pwd,TenantId,ServiceId,options); // テナントIDの指定形式も変わっている(https://・・・が要らない)
// Graph API Clientの作成(Microsoft.Graph.Auth⇒Azure.Identity)
var graphClient = new GraphServiceClient(cred,scopes);
var me = await graphClient.Me
.Request()
//.WithUsernamePassword(username,secPwd) Microsoft.Graph.Auth⇒Azure.Identity
.GetAsync();
チョット見ない間にMSのページまで変更されていたので、焦りました・・・
ユーザー名/パスワード プロバイダーに関しては、SecureStringを使用しなくても良くなった点は良いね。





Users Today : 2
Users Yesterday : 37
Users Last 7 days : 186
Users Last 30 days : 1080
Users This Month : 1009
Users This Year : 4357
Total Users : 98872
Views Today : 3
Views Yesterday : 46
Views Last 7 days : 239
Views Last 30 days : 1280
Views This Month : 1203
Views This Year : 5260
Total views : 137706
Who's Online : 0