SITECORE MANIA 開発者向け技術ブログ

その他

齊藤

齊藤

AzureADログイン時に作成されるユーザー名をメールアドレスにする

前回執筆した「AzureADアカウントを利用してSitecore(サイトコア)管理画面にログインしてみる」の追加情報的な記事になります。

前回と同様、プロジェクトで利用したSitecoreのバージョンは10.1.1、Solrは8.4.0。SIF2.3.0を利用しています。

ユーザー名がランダムな文字列になっている画像

前回「ログイン時にAzureADからユーザー情報を渡す」と記述していましたが、Sitecoreユーザーマネージャーで表示されるユーザー名はランダムなままでした。
今回はAzureADでログインした際に作成されるユーザー名をAzureADから受け取ったメールアドレス(UPN)を利用して「ドメイン名\メールアドレス(UPN)」となるようにカスタマイズしたいと思います。


 

おおまかな手順は以下の2つになります。

  • DefaultExternalUserBuilderを継承してクラスを定義する
  • configファイルで継承したクラスを利用するように設定変更する

順に説明していきます。

 


 

DefaultExternalUserBuilderを継承してクラスを定義する

プロジェクトの任意の場所にcsファイルを作成し、コードを記述していきます。
今回は「プロジェクト名\SitecoreProcessor\CreateUniqueUser.cs」としました。

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Sitecore.Diagnostics;
using Sitecore.Owin.Authentication.Identity;
using Sitecore.Owin.Authentication.Services;
using Sitecore.SecurityModel.Cryptography;
using System;
using System.Linq;

namespace XXXXX.Website.SitecoreProcessor
{
    /// 
    /// ADログイン時にユーザー名を生成する
    /// 
    public class CreateUniqueUser : DefaultExternalUserBuilder
    {
        public CreateUniqueUser(ApplicationUserFactory applicationUserFactory, IHashEncryption hashEncryption) : base(applicationUserFactory, hashEncryption) { }

        protected override string CreateUniqueUserName(UserManager <ApplicationUser> userManager, ExternalLoginInfo externalLoginInfo)
        {
            Assert.ArgumentNotNull((object)userManager, nameof(userManager));
            Assert.ArgumentNotNull((object)externalLoginInfo, nameof(externalLoginInfo));
            var identityProvider = this.FederatedAuthenticationConfiguration.GetIdentityProvider(externalLoginInfo.ExternalIdentity);
            if (identityProvider == null)
                throw new InvalidOperationException("Unable to retrieve identity provider for given identity");
            var domain = identityProvider.Domain;
            var username = "";

            var userNameClaim = externalLoginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type.Equals("email"));

            if (userNameClaim != null)
            {
                username = userNameClaim.Value;

                return domain + "\\" + username;
            }
            return domain + "\\" + externalLoginInfo.DefaultUserName;
        }
    }
}

Sitecoreでデフォルトで利用している、CreateUniqueUserメソッドをオーバーライドしています。
デフォルトのAzureADモジュールではAzureADログイン時に渡されてくるUPN(ユーザープリンシパルネーム)がメールアドレスとして設定されているので、そのメールアドレスを元にユーザーIDを決定しています。

もしemailが空っぽだった場合は、ユーザーの姓名がユーザーIDとして割り振られます(37行目)


 

configファイルで継承したクラスを利用するように設定変更する

上記で作成したコードを読み込むようにconfigを作成します。

<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
  <sitecore>
    <federatedAuthentication type="Sitecore.Owin.Authentication.Configuration.FederatedAuthenticationConfiguration, Sitecore.Owin.Authentication">
      <identityProvidersPerSites hint="list:AddIdentityProvidersPerSites">
        <mapEntry name="all sites" type="Sitecore.Owin.Authentication.Collections.IdentityProvidersPerSitesMapEntry, Sitecore.Owin.Authentication" resolve="true">
          <externalUserBuilder type="XXXXX.Website.SitecoreProcessor.CreateUniqueUser, XXXXX.Website" resolve="true">
            <IsPersistentUser>false</IsPersistentUser>
          </externalUserBuilder>
        </mapEntry>
      </identityProvidersPerSites>
    </federatedAuthentication>
  </sitecore>
</configuration>

このファイルは「C:\inetpub\wwwroot\★サイト名★\App_Config\Sitecore\Owin.Authentication\Sitecore.Owin.Authentication.config」がベースになっています。
上記コードの8行目「externalUserBuilder」の箇所をCreateUniqueUserに変更することで外部プロバイダでログインしたユーザーはユーザーIDをカスタマイズして作成されるようになります。

9行目の「IsPersitentUser」で永続ユーザー/仮想ユーザーで作成するかを決めています。「false」なら仮想ユーザーで作成します。
永続ユーザーで作成するとSitecoreのユーザーマネージャーにユーザー情報が格納されますが、仮想ユーザーで作成するとログインしたユーザー情報はSitecore側に保持されなくなり、一時的なメモリ上にユーザー情報が保存されるようになります。


 

設定後はリビルド→発行を行って動作確認を行います。
シークレットウインドウ等で再度AzureADログインを行ってください(既に今までログインしていた時のユーザー情報が残っている場合は、削除を行う必要があります)。

ユーザー名がランダムな文字列ではなく、ユーザーのメールアドレス(UPN)の形になりました!
ユーザーIDはSitecoreへアクセスした際のログ情報やCMS上での管理(バージョン作成時など)に使用されますので、ランダムなものよりは認識できるもののほうが良いような気がします。



※エントリーの内容・画像等は、公開時点での情報に基づきます。
※Sitecoreのバージョンによって実装されている機能が異なります。

この記事を読んだ人はこちらの記事も読んでます

Sitecore導入に関するご相談・資料ダウンロード

導入をご検討・ご依頼の方や、サービスについてご不明点がございましたらお気軽にお問い合わせください。

5分でわかる!CMS比較資料ダウンロード

クリック・タップで拡大、試し読みできます
掲載コンテンツ
  • 世界のCMSについて
  • CMSポイント全体像
  • 大規模向けCMSの優れた機能
  • Web マーケティング...etc