---
title: "User pre-registration script"
slug: "user-pre-registration-script"
abstract: "Web script example that validates the email address types when a new user is to be registered."
---
# User pre-registration script

This example shows a *User pre-registration* web script that is executed before a new user is added to Sitecore Content Hub. It validates input and restricts access based on specific criteria, such as whether the external provider is Google and the email domain is yahoo.com.

> [!note]
> **Before you begin**
> -   Configure the user groups or profiles required during the pre-registration process.
>
> -   Ensure schema properties align with the script logic for pre-registration workflows.

## Script

```csharp
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.");
  }
}

```

## Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

1. Check the external user provider. If the provider is not Google, the script throws an exception.

    ```csharp
    if (Context.ExternalUserInfo?.Provider == "Google")
    ```

2. Get the [external user information](https://doc.sitecore.com/ch/en/developers/cloud-dev/overview.html "Overview").

    ```csharp
    var info = Context.ExternalUserInfo;
    ```

3. If the user email is `null` or empty, throw an exception.

    ```csharp
    if (string.IsNullOrEmpty(info.Email))
    {
      throw new InvalidOperationException("Provider was 'Google' but email was 'null'.");
    }
    ```

4. If the email does not end with `'@yahoo.com'`, throw an exception.

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

## Setup

- [Create](https://doc.sitecore.com/ch/en/developers/cloud-dev/work-with-scripts.html "Work with scripts"), publish, and enable the *User pre-registration* script.