Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • SteveV 54 posts 240 karma points
    Jan 23, 2023 @ 13:12
    SteveV
    0

    Circumvent member login page when using special useragent string

    Is it possible to show a page that is password protected in Umbraco instead of redirecting the visitor to the login page when the useragent HTTP header has a custom string set?

    I'm using an external search engine (Swiftype) that crawls my website but obviously password protected pages are not getting indexed. However, in the settings of the crawler I can set a custom useragent string. The problem is that I don't know how I should cancel the redirect to the login page. Any ideas?

  • SteveV 54 posts 240 karma points
    Jan 24, 2023 @ 10:32
    SteveV
    0

    I was able to solve it myself. Improvements/suggestions always welcome.

    If you are going to use this code, don't forget to change the DocumentTypeAlias properties, the secret-crawler-useragent-id and TrySetTemplate(...)

    using System;
    using Umbraco.Core;
    using Umbraco.Web.Routing;
    
    namespace MyCustomer.Custom
    {
        public class CrawlerAccess : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
            }
    
            private void PublishedContentRequest_Prepared(object sender, EventArgs e)
            {
                var request = sender as PublishedContentRequest;
    
                if (request.PublishedContent != null
                    && !request.IsInitialPublishedContent
                    && request.InitialPublishedContent.DocumentTypeAlias == "article"
                    && request.PublishedContent.DocumentTypeAlias == "loginPage"
                    && request.RoutingContext.UmbracoContext.HttpContext.Request.UserAgent.Contains("secret-crawler-useragent-id"))
                {
                    request.PublishedContent = request.InitialPublishedContent;
                    request.TrySetTemplate("article");
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft