---
title: "ユーザーサインインスクリプト"
slug: "user-sign-in-script"
abstract: "ユーザーがContent Hubにログインした際にグループの情報を更新するWebスクリプトの例です。"
---
# ユーザーサインインスクリプト

以下のウェブスクリプトの例は、ユーザーがSitecore Content Hubにログインした際に実行されます。提供されたクレームに基づいてユーザーのグループを更新します。

> !重要この例では*Everyone*ユーザーグループを使用しています。組織のユーザーグループ名に基づいてスクリプトをカスタマイズせずに、自分のContent Hubインスタンスで使用しないでください。

> !注**始める前に**
> 
> -   サインイン用のクレームマッピング設定はスキーマエディタで設定されていることを確認してください。

## 文字

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

if (Context.AuthenticationSource != AuthenticationSource.External)
{
 return;
}

// collect group names from the claims
List <string> groups = new List <string> ();
if (Context.ExternalUserInfo?.Claims == null)
{
 groups.Add(Stylelabs.M.Sdk.Constants.UserGroup.Groups.Everyone);
}
else
{
 foreach (Stylelabs.M.Scripting.Types.V1\_0.User.Claim group in Context.ExternalUserInfo.Claims)
 {
 if (group.Type == "MySpecialGroupType")
 {
 groups.Add(group.Value);
 }
 }
}

// load the group ids
List <long> groupIds = (await MClient.Users.GetUserGroupIdsAsync(groups).ConfigureAwait(false))?.Values.ToList();

var user = Context.User;

// ensure UserGroupToUser relation is loaded
await user.LoadMembersAsync(null, new RelationLoadOption("UserGroupToUser")).ConfigureAwait(false);

// update the relation UserGroupToUser with ids of the collected groups
user.GetRelation("UserGroupToUser").SetIds(groupIds);

// update the user
await MClient.Entities.SaveAsync(user).ConfigureAwait(false);

## 脚本の説明

このセクションでは、スクリプトを実行順に順に進め、各パートについて説明します。番号付きの項目はシーケンスを記述しており、実行命令ではありません。

1.  認証ソースが外部であれば、外部の主張に基づいてユーザーグループが更新されていることを確認してください。
    
    if (Context.AuthenticationSource != AuthenticationSource.External)
    {
     return;
    }
    
2.  外部のクレームからグループ情報を収集するためにグループ名のリストを作成しましょう。クレームがない場合は、デフォルトのEveryoneグループを追加します。
    
    List <string> groups = new List <string>();
    if (Context.ExternalUserInfo?.Claims == null)
    {
     groups.Add(Stylelabs.M.Sdk.Constants.UserGroup.Groups.Everyone);
    }
    else
    {
     foreach (Stylelabs.M.Scripting.Types.V1\_0.User.Claim group in Context.ExternalUserInfo.Claims)
     {
     if (group.Type == "MySpecialGroupType")
     {
     groups.Add(group.Value);
     }
     }
    }
    
3.  GetUserGroupIdsAsyncメソッドを使って収集されたユーザーグループのIDを取得します。これらのIDはユーザーのグループ関係の更新に使用されます。
    
    List <long> groupIds = (await MClient.Users.GetUserGroupIdsAsync(groups).ConfigureAwait(false))?.Values.ToList();
    
4.  コンテキストからターゲットユーザーエンティティを取得し、UserGroupToUser関係が読み込まれます。この関係は、ユーザーが特定のグループに所属していることを表します。
    
    var user = Context.User;
    
    // ensure UserGroupToUser relation is loaded
    await user.LoadMembersAsync(null, new RelationLoadOption("UserGroupToUser")).ConfigureAwait(false);
    
5.  収集されたグループのIDでUserGroupToUser関係を更新し、ユーザーを適切なグループにリンクさせます。
    
    user.GetRelation("UserGroupToUser").SetIds(groupIds);
    
6.  ユーザーエンティティを保存し、更新されたグループメンバーシップを永続化します。
    
    await MClient.Entities.SaveAsync(user).ConfigureAwait(false);
    

## セットアップ

-   *ユーザーサインイン*スクリプトを作成、公開、有効化[します](https://doc.sitecore.com/ch/ja/developers/cloud-dev/work-with-scripts.html "Work with scripts")。

## サインインスクリプトを無効にする

サインインスクリプトにランタイムエラーやユーザー検証の不一致が含まれている場合、ユーザーはロックアウトされることがあります。もしそうなったら、REST APIやSDKを使ってスクリプトを無効化し、M.Script.Enabledを*false*に変更してください。

以下の例は、Web SDKを使ってスクリプトを無効化しています:

var loadConfig = new EntityLoadConfiguration(
 CultureLoadOption.None,
 new PropertyLoadOption(
 ScriptingConstants.Scripting.Properties.Enabled),
 RelationLoadOption.None);

var script = await MClient.Entities.GetAsync( your script id , loadConfig).ConfigureAwait(false);
script?.SetPropertyValue("M.Script.Enabled", false);

await MClient.Entities.SaveAsync(script).ConfigureAwait(false);