---
title: "ユーザー事前登録スクリプト"
slug: "user-pre-registration-script"
abstract: "新しいユーザー登録時にメールアドレスの種類を検証するウェブスクリプトの例です。"
---
# ユーザー事前登録スクリプト

この例は、新しいユーザーがSitecore Content Hubに追加される前に実行される *ユーザー事前登録* ウェブスクリプトを示しています。入力を検証し、外部プロバイダーがGoogleでメールドメインがyahoo.comかどうかなど特定の基準に基づいてアクセスを制限します。

> !注**始める前に**
> 
> -   事前登録プロセスで必要なユーザーグループやプロファイルを設定してください。
>     
> -   事前登録ワークフローのスクリプトロジックとスキーマプロパティが一致していることを確認してください。
>     

## 文字

if (Context.ExternalUserInfo?.Provider == "Google")
{
 var info = Context.ExternalUserInfo;
 if (string.IsNullOrEmpty(info.Email))
 {
 throw new InvalidOperationException("Provider was 'Google' but email was 'null'.");
 }

 if (!info.Email.EndsWith("@yahoo.com"))
 {
 throw new ForbiddenException("You are not allowed to access the system. Please contact the administrator.");
 }
}

## 脚本の説明

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

1.  外部のユーザープロバイダーを確認してください。もしプロバイダーがGoogleでなければ、スクリプトは例外を投げます。
    
    if (Context.ExternalUserInfo?.Provider == "Google")
    
2.  [外部ユーザー情報を](https://doc.sitecore.com/ch/ja/developers/cloud-dev/overview.html "Overview")入手しましょう。
    
    var info = Context.ExternalUserInfo;
    
3.  ユーザーメールがnullまたは空の場合は例外を投げてください。
    
    if (string.IsNullOrEmpty(info.Email))
    {
     throw new InvalidOperationException("Provider was 'Google' but email was 'null'.");
    }
    
4.  メールの末尾が '@yahoo.com'でない場合は例外を投げてください。
    
    if (!info.Email.EndsWith("@yahoo.com"))
    {
     throw new ForbiddenException("You are not allowed to access the system. Please contact the administrator.");
    }
    

## セットアップ

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