0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure WebAppsで利用するEntra ID認証にCustomClaimsを追加する

Posted at

はじめに

Azure Web Appsにはいくつかの認証プロバイダを用いた自動認証が提供されています。
(Azure Portalからポチポチするだけで認証実装出来てめっちゃ便利)

中でも、Microsoft Entra ID認証は利用者がM365を利用してる場合一番の選択肢になると思います。
この記事では、アプリ側でEntra IDの情報を利用する方法と、デフォルトでは含まれていないプロパティをjwtに含める方法を解説します。
※少し古い情報なので現在(2025/06)は変わっているかもしれません。

Entra ID作成

Entra ID認証を使うのでEntra IDアプリを作成します。
基本的な手順なのでここでは割愛します。

発行者のURLのみ以下となるように設定します。
https://login.microsoftonline.com/<テナントID>/v2.0

手順

ソースコードに認証機能を追加

Programs.csに認証を有効化するコードを追加します。
builderの後ろにAddAuthentication、appの後半にUseAuthenticationを追加。

// 追加
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();

var app = builder.Build;
// いろいろappに追加
app.UserRouting();

app.UseAuthorization();
app.UseAuthentication(); // 追加

app.MapRazorRapges();
app.Run();

Entra IDでカスタムトークンを追加

トークン構成>オプション要求の追加から、アプリに渡す値を追加できる
なぜかdepartmentなど一般的な設定値が存在しない
GraphAPIを別途呼び出して取得するか、以下のURLでPSからポリシー付けたら行けるっぽい
https://stackoverflow.com/questions/72462198/include-in-job-title-in-jwt-token-from-aad

PowerShellでカスタムポリシーをアプリに追加

例)規定では存在しないジョブタイトルと会社名プロパティを取得できるようにするコマンド

# モジュール追加
Install-Module -Name AzureADPreview

Connect-AzureAD

# ポリシー追加(例としてジョブタイトルと会社名を追加)
New-AzureADPolicy -Definition @('{"ClaimsMappingPolicy": {"Version": 1,"IncludeBasicClaimSet": "true","ClaimsSchema":[{"Source": "user","ID": "jobTitle","JwtClaimType": "jobTitle"}, {"Source": "user","ID": "companyName","JwtClaimType": "companyName"}]}}') -DisplayName "AddBasicClaims" -Type "ClaimsMappingPolicy"

# アプリにポリシーを付与
# ObjectId of the ServicePrincipal :アプリのオブジェクトID(エンタープライズアプリから取得)
# ObjectId of the Policy:7行目で追加したAADポリシーのid、実行後に表示される
Add-AzureADServicePrincipalPolicy -Id <ObjectId of the ServicePrincipal> -RefObjectId <ObjectId of the Policy>

マニフェストから「acceptmappedClaims」をtrueに変更

規定値はnullなので、trueに変更して上記のプロパティをとれるように。

おわりに

この操作をすることでEntra ID認証時にほかのEntra IDプロパティを取得できるようになりました。
GUIで取れるようになってくれたらいいのに・・・

その他コマンド

既存のサービスプリンシパルを取得

Get-AzureADServicePrincipal -Filter "DisplayName eq 'アプリ名'";

サービスプリンシパルを削除

Remove-AzureADServicePrincipalPolicy -Id <id> -PolicyId <id>

アプリに対するすべての同意を削除

Connect-MgGraph -TenantId <TenantId> -Scopes "Application.ReadWrite.All", "Directory.ReadWrite.All", "DelegatedPermissionGrant.ReadWrite.All", "AppRoleAssignment.ReadWrite.All"

# Get Service Principal using objectId
$sp = Get-MgServicePrincipal -ServicePrincipalId '<Servcie Principal Id>'

# Get all delegated permissions for the service principal
$spOAuth2PermissionsGrants= Get-MgOauth2PermissionGrant -All| Where-Object { $_.clientId -eq $sp.Id }

# Remove all delegated permissions
$spOauth2PermissionsGrants |ForEach-Object {
  Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $_.Id
  }

# Get all application permissions for the service principal
$spApplicationPermissions = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $Sp.Id -All | Where-Object { $_.PrincipalType -eq "ServicePrincipal" }

# Remove all application permissions
$spApplicationPermissions | ForEach-Object {
Remove-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $Sp.Id  -AppRoleAssignmentId $_.Id
 }
 
pause


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?