User pre-registration script example
Version: 3.4
This is an example of a User pre-registration script to be executed before a new user is registered to Sitecore Content HubTM. It only allows a certain email address type if the external provider is Google.
Use case
- A new user is to be registered.
- The script checks if the provider is Google. In that case, only e-mail addresses ending with @Stylelabs.com are allowed.
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("@stylelabs.com"))
{
throw new ForbiddenException("You are not allowed to access the system. Please contact the administrator.");
}
}
Script explanation
-
Check the external user provider. Only the 'Google' provider is of interest.
RequestResponseif (Context.ExternalUserInfo?.Provider == "Google")
-
Get the external user information.
RequestResponsevar info = Context.ExternalUserInfo;
TipFor more information about the
ExternalUserInfo
, please refer to the API reference notes. -
If the user email is null or empty, throw an
InvalidOperationException
.RequestResponseif (string.IsNullOrEmpty(info.Email)) { throw new InvalidOperationException("Provider was 'Google' but email was 'null'."); }
-
If the email does not end with '@stylelabs.com', throw a
ForbiddenException
.RequestResponseif (!info.Email.EndsWith("@stylelabs.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 with the source code above.