Redirect unauthorized users to a custom page

Redirect unauthorized users to a custom page

Posted on March 11, 2014 0 Comments
Redirect unauthorized users to a custom page

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

Configuring the Front-End Login Url

Since Sitefinity 5.3 you have the ability to set a FrontEndUrl property, which is defined at the Site level and can be also granularly configured per site in a Multi Site instance. The FrontEndUrl is url of a page, that you will be redirected to if you access a protected frontend page in you web site.

This way you can directly control the logic of how to prompt users for login, when they are trying to access a restricted resource or action. 

 For single site the setting can be located under Sitefinity backend ->Administration ->Settings ->Advanced -. Project ->Default site -> FrontEndLoginPageUrl:

FrontEndLoginPageUrl_SingleSite

 
 

When you are working with Multisite Management you could have separate FrontEndUrl for each site, and this is configured for each site when setting the site properties:

 

For more information and an example use case for configuring this property you can look into the Display the Reply buttons in Forum threads and Forum posts section of this article from our documentation.

Extending the logic

Alternatively, if you want a more custom-tailored solution, since Sitefinity 5.2 there has been an event for unauthorized access on front end page, where you could redirect the response depending on your needs. The example below is showing how to perform the redirect. It is not just a configuration, but it is much more flexible.

protected void Application_Start(object sender, EventArgs e)
        {
            //subscribe to Bootstrapper.Initialized event
            Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
        }
 
        void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
            if (e.CommandName == "Bootstrapped")
            {
                //use UnauthorizedPageAccessEvent
                EventHub.Subscribe<IUnauthorizedPageAccessEvent>(new Telerik.Sitefinity.Services.Events.SitefinityEventHandler<IUnauthorizedPageAccessEvent>(OnUnauthorizedAccess));
            }
        }
 
        void OnUnauthorizedAccess(IUnauthorizedPageAccessEvent unauthorizedEvent)
        {
            var url = unauthorizedEvent.Page.Url.TrimStart('~');
            //for this specific page redirect to CustomerLoginPage
            if (unauthorizedEvent.Page.Title.Contains("Unauthorized page"))
                unauthorizedEvent.HttpContext.Response.Redirect("~/customer-login");
            //for all other pages redirect to the Sitefinity login page
            //if you do not use the else clause you will be redirected to the Sitefinity login page in all other cases different that the above one
            else
                unauthorizedEvent.HttpContext.Response.Redirect("~/sitefinity");
        }
   

You have the ability to redirect the unauthorized users to a specific page or redirect them to other Sitefinity pages. 

The source code above should be placed inside the Application_Start method of a Global.asax.cs file. To add such file in your Sitefinity project follow the steps below:

1. Open your project in Visual Studio
2. Right click on your project and select Add -> New item..
3. Search for Global.asax

A Global.asax.cs file could be found attached. 

Global.asax.cs

progress-logo

The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation