Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

MessageProvider

Describes the provider for accessing Site Messages configured in the Admin Console.

Description

The provider for accessing Site Messages configured in the Admin Console. Using this provider also gives the additional benefit of making unit testing much easier. Before your test harness runs, you can change the current MessageProvider to a provider that does not access the database to return site messages. This makes testing easier because it removes a dependency on an external resource.

Properties

Current

Holds the current message provider. This is the property you should use to access the current message provider and any of the site messages. The default provider is MessageProvider.

static MessageProvider Current { get; } = new MessageProvider

In addition, the MessageProvider includes a property for each of a large number of site messages. For example, the MessageProvider.SignInInfo_UserNamePassword_Combination property returns a message stating that the credentials used to log in are incorrect. All of the property names attempt to describe the message content.

Methods

SetCurrent(MessageProvider)

Sets the current message provider. This allows the default message provider can be changed.

static void SetCurrent(MessageProvider messageProvider)

Parameters

  • messageProvider – The message provider to set as the current message provider. This message provider will be used by the rest of the application.

GetMessage(string, string, string)

Finds and returns a site message by name. If the site message is not found, the default value and description are used to create the missing site message.

virtual string GetMessage(string name, string defaultValue, string description = "")

Parameters

  • name – The name of the site message to return.
  • defaultValue – The text to use for the site message if it cannot be found.
  • description – The description to use for the site message if it cannot be found.

Returns

The site message text. If the site message is not found, the default value is returned.

Example

The example below uses a number of site messages from the MessageProvider to indicate if adding a new session is currently valid. The messages are accessed using the current message provider.

Code Sample

[DependencyName(nameof(ValidateContext))]
public sealed class ValidateContext : HandlerBase<AddSessionParameter, AddSessionResult>
{
  private readonly Lazy<IAuthenticationService> authenticationService;
     
  private readonly Lazy<IUserProfileUtilities> userProfileUtilities;
     
  public ValidateContext(Lazy<IAuthenticationService> authenticationService, Lazy<IUserProfileUtilities> userProfileUtilities)
  {
      this.authenticationService = authenticationService;
      this.userProfileUtilities = userProfileUtilities;
  }
     
  public override int Order => 500;
     
  public override AddSessionResult Execute(IUnitOfWork unitOfWork, AddSessionParameter parameter, AddSessionResult result)
  {
      var userProfile = unitOfWork.GetTypedRepository<IUserProfileRepository>().GetByUserName(parameter.UserName);
     
      if (userProfile == null)
      {
    return this.CreateErrorServiceResult(result, SubCode.AccountServiceUserProfileNotFound, MessageProvider.Current.SignInInfo_UserNamePassword_Combination);
      }
     
      if (userProfile.IsDeactivated)
      {
    return this.CreateErrorServiceResult(result, SubCode.Deactivated, MessageProvider.Current.SignInInfo_User_IsDeactivated);
      }
     
      if (userProfile.IsPasswordChangeRequired)
      {
    return this.CreateErrorServiceResult(result, SubCode.PasswordExpired, MessageProvider.Current.SignInInfo_UserNamePassword_ChangeRequired);
      }
     
      if (this.authenticationService.Value.IsLockedOut(userProfile.UserName))
      {
    return this.CreateErrorServiceResult(result, SubCode.LockedOut, MessageProvider.Current.SignInInfo_UserLockedOut);
      }
     
      if (!this.userProfileUtilities.Value.IsAllowedForWebsite(userProfile, SiteContext.Current.WebsiteDto))
      {
    return this.CreateErrorServiceResult(result, SubCode.AccountServiceUserNotAllowedForWebsite, MessageProvider.Current.SignInInfo_UserNotAllowedForWebsite);
      }
     
      result.UserProfile = userProfile;
     
      return this.NextHandler.Execute(unitOfWork, parameter, result);
  }
}