---
title: "フェデレーテッド認証の設定"
slug: "configure-federated-authentication"
abstract: "フェデレーテッド認証の設定方法を説明します"
---
# フェデレーテッド認証の設定

外部プロバイダーを通じてユーザーがSitecoreにログインできるようにするために、[フェデレーテッド認証を使用します](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/using-federated-authentication-with-sitecore.html "Using federated authentication with Sitecore") 。フェデレーテッド認証は、使用する外部プロバイダーによってSitecoreを特定の方法で設定する必要があります。フェデレーテッド認証の設定には、いくつかの作業が含まれます:

-   [アイデンティティプロバイダーの設定](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_section-idm53130183968568 "Configure an identity provider")
-   [Sitecoreユーザー名生成](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_id__Map_claims_and "Sitecore user name generation")
-   [地図の主張と役割](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_N1614939275100 "Map claims and roles")
-   [マップの性質](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_id__Map_properties "Map properties")
-   [ユーザーアカウントをConnectする](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_id__Connect_users_accounts "Connect a user account")
-   [仮想ユーザーと永続ユーザーの設定](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_id__Configure_virtual_and "Configure virtual and persistent users")
-   [サインインリンクを生成する](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-manager/configure-federated-authentication.html#UUID-b2f3f438-eb8f-bb36-2f60-3607ba58aaf7_id__Generate_sign-in_links "Generate sign-in links")

## アイデンティティプロバイダーの設定

使用するアイデンティティプロバイダーの設定が必要です。これを行う方法は、使用するプロバイダーによって異なります。主なユースケースはAzure Active Directory(Azure AD)を使用することです。 [OpenID ConnectとAzure Active Directoryを使ってウェブアプリケーションへのアクセスを承認するには](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-openid-connect-code) 、Azure ADの動作方法が説明されています。

アイデンティティプロバイダーの設定:

1.  configuration/sitecore/federatedAuthentication/identityProvidersノードをパッチ適用するには、identityProviderという名前の新しいノードを作成します。
    
2.  idとtype属性の値を入力します。typeは抽象クラスを実装しなければなりませんSitecore.Owin.Authentication.Configuration.IdentityProvider。Sitecoreにはデフォルトの実装があります –Sitecore.Owin.Authentication.Configuration.DefaultIdentityProvider。
    
3.  作成したノードの下に、param、caption、domain、transformations子ノードの値を入力します。
    
4.  configuration/sitecore/federatedAuthentication/identityProvidersPerSitesノードの下に、名前mapEntryの新しいノードを作成します。
    
5.  name属性とtype属性の値を入力します。name属性の値は各エントリごとに一意でなければなりません。typeはSitecore.Owin.Authentication.Collections.IdentityProvidersPerSitesMapEntry、Sitecore.Owin.Authentication、またはこれから継承しなければなりません。
    
6.  resolve 属性の値としてtrueを入力します。
    
7.  作成したノードの下に、sites(プロバイダーが動作するサイトの一覧)、identityProviders(プロバイダーのリスト)、externalUserBuilder子ノードの値を入力します。各externalUserBuilderノードのresolve属性の値としてtrueを入力してください。
    
    > !注これはSXAサイトにも当てはまります。
    

### プロバイダーのコードを追加

owin.identityProvidersパイプライン用の新しいプロセッサを作成しなければなりません。

新しいプロセッサを作成するには:

1.  Sitecore.Owin.Authentication.Pipelines.IdentityProviders.IdentityProvidersProcessorクラスを継承する。
2.  設定中のidentityProviderの名前でIdentityProviderNameプロパティを上書きしてください。
3.  ProcessCoreメソッドを上書きしてください。

### owin.identityProvidersパイプラインと統合してください

次に、コードをowin.identityProvidersパイプラインに統合する必要があります。

例えば、このサンプルはAzure ADをアイデンティティプロバイダーとして使用しています:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
 <sitecore role:require="Standalone or ContentDelivery or ContentManagement">
 <pipelines>
 <owin.identityProviders>
 <processor type="Sitecore.Owin.Authentication.YourIdentityProviders.AzureAd, YourAssembly" resolve="true" />
 </owin.identityProviders>
 </pipelines>
 </sitecore>
</configuration>

## Sitecoreユーザー名生成

ユーザー名はSitecoreインスタンス全体で一意でなければなりません。異なる外部プロバイダーのユーザー名をSitecoreのユーザー名として使うことはできません。なぜなら、それがユーザー名の一意性を保証するわけではないからです。

DefaultExternalUserBuilderクラスは、与えられた外部ユーザー名に対して一連のユーザー名を作成します。そして、Sitecoreにまだ存在しない最初の名前を使用します。その列の値は、外部ユーザー名と指定されたアイデンティティプロバイダーに設定されたSitecoreドメインのみに依存します。

## 地図の主張と役割

プロバイダーはクレームを発行し、それぞれのクレームに1つ以上の値を与えます。Sitecoreは外部認証プロセス中に認証済みユーザーに対して発行されたクレームを読み取ります。特定のクレームを持つID(クライアントまたはユーザー)に一部のリソースへのアクセスを制限できます。

外部ユーザーはクレームを持つユーザーです。クレームをロールにマッピングすることで、Sitecoreの役割ベースの認証システムが外部ユーザーを認証できます。

主張を役割にマッピングするために:

1.  <identityProvider>ノードを に追加します。configuration/sitecore/federatedAuthentication/identityProviders.
    
2.  <identityProvider>ノードに<transformations hint="list:AddTransformation">ノードを追加します。
    
3.  子ノードとして変換ノードを追加します。
    
    例えば、変換ノードは次のようになります:
    
    <transformation type="Sitecore.Owin.Authentication.Services.DefaultTransformation, Sitecore.Owin.Authentication">
     <sources hint="raw:AddSource">
     <claim name="groups" value="f04b11c5-323f-41e7-ab2b-d70cefb4e8d0" />
     <claim name="groups" value="40901f21-29d0-47ae-abf5-184c5b318471" />
     </sources>
     <targets hint="raw:AddTarget">
     <claim name="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" value="Sitecore\\Developer" />
     </targets>
     <keepSource>true</keepSource>
    </transformation>
    
    -   typeはSitecore.Owin.Authentication.Services.Transformation階級から継承しなければなりません。
        
    -   この例では、変換は名前http://schemas.microsoft.com/ws/2008/06/identity/claims/roleと値Sitecore\\Developerの請求を、groupと同時にf04b11c5-323f-41e7-ab2b-d70cefb4e8d0と40901f21-29d0-47ae-abf5-184c5b318471値を持つ二つの請求を持つ恒等式に追加します。
        
    -   keepSource==trueは元の請求(この例では2つのgroup請求)が削除されないことを指定します。デフォルトはfalseであり、変換が恒等式にうまく適用された場合、元の請求は <targets> ノードに記載されたものに置き換えられます。
        
    -   keepSource==true元の請求(この例では2つのgroup請求)は削除されないことを指定します。Sitecoreでは、Sitecore.Owin.Authentication.Services.DefaultTransformationに対してkeepSourceのデフォルト値はfalseであり、変換が恒等式にうまく適用された場合、元の請求は <targets> ノードに記載されたものに置き換えられます。
        
        > !注アイデンティティサーバー上で、Sitecore.Plugin.IdentityProviders.DefaultClaimsTransformationの場合、keepSourceはデフォルト値trueです。
        

> !注sitecore/federatedAuthentication/sharedTransformationsノードでクレーム変換を指定する場合、これらの変換はすべてのアイデンティティプロバイダーに対して行われます。

## マップの性質

ユーザープロファイルに保存されているSitecoreのユーザープロパティにアイデンティティクレームをマッピングする必要があります。

propertyInitializerノードはsitecore\\federatedAuthenticationノードの下に、マップのリストを格納します。各マップには内部のソースノードとターゲットノードがあります。これらのノードにはnameとvalueの2つの属性があります。プロパティはこれらのプロパティの値を設定することでマッピングします。

もしクレームがソースノードのname属性(および指定されている場合はvalue)に一致する場合、ターゲットノードのname属性によって指定されたユーザープロパティのvalue属性は、マッチしたクレームの値に設定されます(targetノードでvalue属性が指定されていない場合)。

例えば:

<propertyInitializer type="Sitecore.Owin.Authentication.Services.PropertyInitializer, Sitecore.Owin.Authentication">
 <maps hint="list">
 <map name="status to UserStatus"
 type="Sitecore.Owin.Authentication.Services.DefaultClaimToPropertyMapper, Sitecore.Owin.Authentication">
 <data hint="raw:AddData">
 <source name="status" value="first" />
 <target name="UserStatus" value="1" />
 </data>
 </map>
 </maps>
</propertyInitializer>

この例では、ソースnameとvalue属性がUserStatusターゲット名と値1にマッピングされています。

> !重要設定ファイルを分割する場合は、マップノードにname属性を追加し、ノードがすべてのファイルで一意であることを確実にしてください。これは設定パッチのやり方Sitecore原因です。

## 地図のカスタムユーザープロパティ

カスタムユーザープロパティを、Sitecore.Owin.Authentication.Services.DefaultClaimToPropertyMapperが保存できる2か所にマッピングできます:

-   ユーザーが仮想であれば、プロパティがカスタムかどうかに関わらず、Sitecore.Security.Accounts.Userクラスにプロパティとして保存.RuntimeSettings.Properties。
-   ユーザーが永続的であれば、Sitecore.Security.Accounts.Userクラスにプロパティとして保存.Profile.CustomProperties。

デバッグや開発目的でこれらのプロパティを見ることは可能ですが、それらはプライベートプロパティであり、直接アクセスされることは期待されていません。

カスタム機能の場合、これらのプロパティにアクセスする場合は、プロパティのインデクサーProfileSitecore.Security.Accounts.Userクラスからアクセスできます。これにより、ユーザーが仮想的であれ永続的であれ、正しい位置からユーザープロパティを解決できます。

## ユーザーアカウントをConnectする

アカウント接続により、複数の外部アカウント間でプロフィールデータを共有でき、一方で永続的なアカウントがもう一方に割り当てられます。永続化されたユーザーにロールが割り当てられている場合、フェデレーテッド認証はそれを外部アカウントと共有します。

以下の状況下では、アカウントへの接続は自動的に行われます。Sitecoreは認証済みユーザーからサインアウトし、新しい永続的または仮想アカウントを作成し、その後認証を行います。

-   ユーザーはすでにサイト上で認証済みです。
-   ユーザーは外部プロバイダーで同じサイトにサインインします。
-   外部アイデンティティと既存の永続アカウントとの間にはすでに接続がありません。ASP.NET Identityでは、 signInManager.ExternalSignIn(...) SignInStatus.Failureを返します。

外部アイデンティティをすでに認証済みのアカウントにバインドするには、依存性注入を使ってSitecore.Owin.Authentication.Services.UserAttachResolverクラスをオーバーライドする必要があります。以下の手順はその例を示しています:

1.  Sitecore.Owin.Authentication.Services.UserAttachResolverクラスの拡張:
    
    using System;
    
    using System.Threading.Tasks;
    
    using Microsoft.Owin;
    
    using Sitecore.Owin.Authentication.Services;
    
    using Sitecore.Text;
    
    namespace Sitecore.Owin.Authentication.Samples.Services
    
    {
    
     public class SampleUserAttachResolver : UserAttachResolver
    
     {
    
     public override UserAttachResolverResult Resolve(UserAttachContext context)
    
     {
    
     IFormCollection formData = Task.Run(async () => await context.OwinContext.Request.ReadFormAsync()).Result;
    
     string consentResult = formData"uar\_action";
    
     UserAttachResolverResultStatus resultStatus;
    
     if (Enum.TryParse(consentResult, true, out resultStatus))
    
     {
    
     return new UserAttachResolverResult(resultStatus);
    
     }
    
     string redirectUrl = new UrlBuilder("/dialogs/consent") { "returnUrl" = context.ReturnUrl }.ToString();
    
     context.OwinContext.Response.Redirect(redirectUrl);
    
     return new UserAttachResolverResult(UserAttachResolverResultStatus.DelayedResolve);
    
     }
    
     }
    
    }
    
    ResolveメソッドはUserAttachContextを値引数として受け取り、コントローラにリクエストを送信し、呼び出したコントローラからの応答を処理します。
    
2.  MVCコントローラーとレイアウトを作成してエンドポイントを作成します。
    
    MVCコントローラー:
    
    using System.Web.Mvc;
    
    namespace Sitecore.Owin.Authentication.Samples.Controllers
    
    {
    
     public class ConsentController : Controller
    
     {
    
     public ActionResult Index()
    
     {
    
     this.ViewBag.User = this.HttpContext.User.Identity.Name;
    
     this.ViewBag.ReturnUrl = this.Request.Params"ReturnUrl";
    
     return this.View();
    
     }
    
     }
    
    }
    
    レイアウト:
    
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <title>Consent window</title>
     </head>
    
     <body>
     <p>
     The @ViewBag.User user is already logged in. Would you like to attach to
     the user or create new record?
     </p>
    
     <br />
    
     <form method="POST" action="@ViewBag.ReturnUrl">
     <button type="submit" name="uar\_action" value="attach">Attach</button>
    
     <button type="submit" name="uar\_action" value="new">New</button>
     </form>
     </body>
    </html>
    
3.  拡張クラスをSitecoreに登録するには、新しいサービス構成クラスを作成します:
    
    using Microsoft.Extensions.DependencyInjection;
    
    using Sitecore.DependencyInjection;
    
    using Sitecore.Owin.Authentication.Samples.Services;
    
    using Sitecore.Owin.Authentication.Services;
    
    namespace Sitecore.Owin.Authentication.Samples.Infrastructure
    
    {
    
     public class ServicesConfigurator : IServicesConfigurator
    
     {
    
     public void Configure(IServiceCollection serviceCollection)
    
     {
    
     serviceCollection.AddSingleton<UserAttachResolver, SampleUserAttachResolver>();
    
     }
    
     }
    
    }
    
4.  作成したクラスをカスタム設定ファイルで定義し、<sitecore> ノードの下に次のノードを追加します:
    
    <services>
    
     <configurator
     type="Sitecore.Owin.Authentication.Samples.Infrastructure.ServicesConfigurator, Sitecore.Owin.Authentication.Samples" />
    
    </services>
    

## プログラム的アカウント接続管理

Sitecoreはアカウント接続にASP.NET Identityを使用しているため、アカウント接続はASP.NET Identity APIと同じ方法で処理されます。

1.  OwinコンテキストからUserManagerオブジェクトを取得する:
    
    using Sitecore.Owin.Authentication.Extensions;
    
    // ...
    
    IOwinContext context = HttpContext.Current.GetOwinContext();
    
    UserManager<ApplicationUser> userManager = context.GetUserManager();
    
2.  CRUD操作には以下の方法を用いてください:
    
    Task<IdentityResult> AddLoginAsync(ApplicationUser user, UserLoginInfo login);
    
    Task<IdentityResult> RemoveLoginAsync(ApplicationUser user, UserLoginInfo login);
    
    Task<IList<UserLoginInfo>> GetLoginsAsync(ApplicationUser user);
    
    Task<ApplicationUser> FindAsync(UserLoginInfo login);
    

## 仮想ユーザーと永続ユーザーの設定

Sitecoreは仮想ユーザーをサポートしています。外部プロバイダーを通じてユーザーを認証すると、Sitecoreは適切なアクセス権を持つ仮想ユーザーを作成・認証します。

しかし、仮想ユーザーを利用することにはいくつかの欠点があります。ユーザープロファイルデータはセッション間で永続化できません。なぜなら、仮想ユーザープロファイルはユーザーセッションが続く間のみ存在するためです。したがって、各外部ユーザーごとに実際の永続ユーザーを作成するべきです。ユーザーが初めて外部認証を使用すると、Sitecoreは新しいユーザーを作成・永続化し、このユーザーを外部IDプロバイダーとそのプロバイダーのユーザーIDにバインドします。次にユーザーが同じ外部プロバイダーと認証情報で認証した際、Sitecoreはすでに作成され永続化されたユーザーを見つけて認証します。

### ユーザービルダー

identityProvidersPerSites/mapEntryノードにはexternalUserBuilderノードが含まれています。このようなユーザービルダーを追加してください:

-   Sitecore.Owin.Authentication.Services.ExternalUserBuilderから継承するクラスを指定します。ユーザービルダーは外部ユーザー情報に基づいてSitecoreユーザーを作成する責任があります。
    
    永続ユーザーまたは仮想ユーザーを作成するためのデフォルトの実装は、isPersistentUserコンストラクタパラメータに基づいています。
    
    <externalUserBuilder type="Sitecore.Owin.Authentication.Services.DefaultExternalUserBuilder, Sitecore.Owin.Authentication">
     <IsPersistentUser>true</IsPersistentUser>
    </externalUserBuilder>
    

> !注ユーザービルダーを実装する際、データベース内でユーザーを作成するために使ってはいけません。 ApplicationUserクラスのインスタンスを作成するだけで十分です。

### 現場へのビルダーの適用

ユーザービルダーを定義するサイトのidentityProvidersPerSitesノード内でmapEntryを見つけ、externalUserBuilderノードを指定します。例えば:

<mapEntry type="Sitecore.FederatedAuthentication.Collections.IdentityProvidersPerSitesMapEntry, Sitecore.FederatedAuthentication">
 <sites hint="list">
 <site>shell</site>
 <site>admin</site>
 <site>website</site>
 </sites>
 <identityProviders hint="list:AddIdentityProvider">mapEntry
 <identityProvider ref="federatedAuthentication/identityProviders/identityProvider@id='AzureAd'" />
 <identityProvider ref="federatedAuthentication/identityProviders/identityProvider@id='IdentityServer'" />
 </identityProviders>
 <externalUserBuilder type="Sitecore.Owin.Authentication.Services.DefaultExternalUserBuilder, Sitecore.Owin.Authentication">
 <IsPersistentUser>true</IsPersistentUser>
 </externalUserBuilder>
</mapEntry>

上記の例では、Sitecoreビルダーをshell、admin、websitesサイトに適用します。

適用される建設業者は、該当サイトの建設業者を上書きします。

## サインインリンクを生成する

Sitecoreサイト向けに外部アイデンティティプロバイダーを設定した後、getSignInUrlInfoパイプラインを通じてそれらのURLを生成できます。このパイプラインは、各アイデンティティプロバイダーの追加情報を含むサインインURLのリストを取得します。

> !重要POSTリクエストではサインインリンクのみを使用してください。

サインインリンクを生成するには:

-   以下の例のようにgetSignInUrlInfoパイプラインを使用します:
    
    using Sitecore.Pipelines.GetSignInUrlInfo;
    /\*
    ...
    \*/
    var args = new GetSignInUrlInfoArgs(site: "website", returnUrl: "/");
    GetSignInUrlInfoPipeline.Run(corePipelineManager, args);
    

args.ResultはSitecore.Data.SignInUrlInfoオブジェクトの集合を含みます。これらのオブジェクトは以下の性質を持ちます。

-   Href – URLです。
-   IdentityProvider – 識別プロバイダーの名前です。例えば、リンクのCSSクラスとして使うことができます。
-   Caption – アイデンティティ提供者のキャプション。これをリンクテキストとして使うべきです。

> !注Sitecore依存注入を使って、BaseCorePipelineManagerクラスの実装を取得してください。