LoginSignup
4
4

More than 3 years have passed since last update.

Easy Auth で認証したユーザIDを入手する

Last updated at Posted at 2020-03-30

Easy Auth で認証したユーザIDを入手する

# 以下の記事が書かれた時の版数は .NET Core 3.1 (3.1.201), ASP.NET Core 3.1 (3.1.3) となります.

Azure App Service に組み込まれた Easy Auth を使った Azure AD 認証連携、設定が簡単すぎて感動的. ただ、User.Identity.Name が空だったのでコードを書いた.

Visual Studio 2019 の新しいプロジェクトの作成で「ASP.NET Core Web アプリケーション」を選び、ASP.NET Core 3.1 の Web アプリケーション(モデル ビュー コントローラ) を選んで生成されるソースコードがまず出発地点.

次に Startup.cs を開き、先頭に using System.Security.Principal; を追加して、Configure メソッドの app.UseAuthorization();app.UseEndpoints(endpoints => の間に

app.Use(async (context, next) =>
{
    if (context.Request.Headers.ContainsKey("X-MS-CLIENT-PRINCIPAL-NAME"))
    {
        var azureAppServicePrincipalNameHeader = context.Request.Headers["X-MS-CLIENT-PRINCIPAL-NAME"][0];
        var identity = new GenericIdentity(azureAppServicePrincipalNameHeader);
        context.User = new GenericPrincipal(identity, null);
    }
    await next.Invoke();
});

を埋め込む. 要するに Easy Auth はリクエストヘッダの X-MS-CLIENT-PRINCIPAL-NAME にユーザ ID を入れてくれている.

あとは Views/Home/Index.cshtml を開いて <h1 class="display-4">Welcome</h1><h1 class="display-4">Welcome @User.Identity.Name</h1> に書き換えるだけ.

デプロイして、確認. 画面に Azure AD にログインしたユーザ ID が表示された. OK!!

4
4
2

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
4
4