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.
-
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
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.
-
Check the external user provider. If the provider is not Google, the script throws an exception.
RequestResponseif (Context.ExternalUserInfo?.Provider == "Google") -
Get the external user information.
RequestResponsevar info = Context.ExternalUserInfo; -
If the user email is
nullor empty, throw an exception.RequestResponseif (string.IsNullOrEmpty(info.Email)) { throw new InvalidOperationException("Provider was 'Google' but email was 'null'."); } -
If the email does not end with
'@yahoo.com', throw an exception.RequestResponseif (!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.