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.

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

RequestResponse
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.

    RequestResponse
    if (Context.ExternalUserInfo?.Provider == "Google")
    
  2. Get the external user information.

    RequestResponse
    var info = Context.ExternalUserInfo;
    
  3. If the user email is null or empty, throw an exception.

    RequestResponse
    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.

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

Setup

  • Create, publish, and enable the User pre-registration script.

Do you have some feedback for us?

If you have suggestions for improving this article,