+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Redirect unauthorized users to a custom page

by Stefani Tacheva

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 granluarly 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

10 comments

Leave a comment
  1. Bob Maes Feb 13, 2013

    Where can I find the FrontEndUrl setting?

    Kind regards,

    bob

  2. Bob Maes Feb 14, 2013

    Found it, I think...

    Administration -> Settings -> Advanced -> Project -> DefaultSite

    There you have the properties FrontEndLoginPageUrl / FrontEndLoginPageId.

    Kind regards,

  3. Steve Aug 21, 2013
    is frontend url supposed to DO something, because in 6.1 it does nothing...all unauthorized requests go to /Sitefinity/Login
  4. David Sep 19, 2013
    Steve, did you restart the app? This works for me in my current 6.1 project.
  5. Craig Mar 11, 2014

    Is the IUnauthorizedPageAccessEvent triggered when a user requests a protected document, image, or other content item?

  6. Bishoy hanna Sep 10, 2014

    Its not working with me in sitefinity7.0
    I tried to 

    - Set front page url

    - front page id 

    - rebuild the app

  7. bishoy Hanna Sep 10, 2014
    It worked after deactivating the Multisite Module
  8. Stefani Tacheva Sep 10, 2014
    Hi all,

    For Multisite you can set a page for public user login, by expanding Advanced and performing one of the following:
    To select a page from the site, select Select a page…, click Select a page button, select a page from the list and click Done.
    To manually enter the URL of the public login page, select Enter URL and enter an URL that must be of the same domain.

    Kind Regards,
    Stefani Tacheva
    Enterprise Support Officer
    Sitefinity Team
  9. Nidhi Nov 02, 2015
    For multilingual site, it always redirect to the english page.
  10. Stefani Nov 19, 2015

    In MultiSite you need to set a FrontEndUrl, which is defined at the Site level and is configured per site.

    Leave a comment