Copied to clipboard

Flag this post as spam?

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


  • Trevor Husseini 20 posts 151 karma points
    Jan 27, 2021 @ 02:07
    Trevor Husseini
    0

    Is there a functional equivalent to context.BeginRequest in v8?

    Aloha!

    I'm having some trouble re-writing some code for Umbraco v8 that was originally implemented for v7.

    In short, I have an Http Module that rewrites a bunch of paths in order to accommodate a custom UsersMembershipProvider. The intent of the provider is to allow us to enforce stricter business rules regarding the user's password. Here's the original implementation:

    using System;
    using System.Web;
    
    namespace Custom.Umbraco.Security.Modules
    {
        public class PasswordRequirementsModule : IHttpModule
        {
            public void HandleLoginRequest(object sender, EventArgs e)
            {
                var context = ((HttpApplication)sender).Context;
                var url = context.Request.Path.ToLower();
    
                if (url.ToLower().Contains("/umbraco/backoffice/umbracoapi/authentication/postlogin"))
                {
                    context.RewritePath("/umbraco/Api/CustomAuthentication/AuthorizedPostLogin");
                }
            }
    
            public void HandleSectionsRequest(object sender, EventArgs e)
            {
                var context = ((HttpApplication)sender).Context;
                var url = context.Request.Path.ToLower();
    
                if (url.Contains("/umbraco/backoffice/umbracoapi/section/getsections"))
                {
                    context.RewritePath("/umbraco/backoffice/UmbracoApi/CustomSection/GetAuthorizedGetSections");
                }
            }
    
            public void HandleDashboardRequest(object sender, EventArgs e)
            {
                var context = ((HttpApplication)sender).Context;
                var url = context.Request.Path.ToLower();
                var queryString = context.Request.QueryString;
    
                if (url.Contains("/umbraco/backoffice/umbracoapi/dashboard/getdashboard"))
                {
                    context.RewritePath("/umbraco/backoffice/UmbracoApi/CustomDashboard/GetAuthorizedDashboard?section=" + queryString["section"]);
                }
            }
    
            public void Dispose()
            {
                //throw new NotImplementedException();
            }
    
            public void Init(HttpApplication context)
            {
                context.BeginRequest += new EventHandler(HandleLoginRequest);
                context.BeginRequest += new EventHandler(HandleSectionsRequest);
                context.BeginRequest += new EventHandler(HandleDashboardRequest);
            }
        }
    }
    

    My approach was to create a Component so that I can hook into the execution pipeline via the Initialize() and Terminate() methods, however, I don't know of any service that is executed for every single Backoffice request. Below is my 1st pass which only executes in the context of content being saved.

    using System;
    using System.Web;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Services.Implement;
    
    namespace Custom.Umbraco.Security.Components
    {
        public class PasswordRequirementsModule : IComponent
        {
            public void HandleLoginRequest(object sender, EventArgs e)
            {
                var context = ((HttpApplication)sender).Context;
                var url = context.Request.Path.ToLower();
    
                if (url.ToLower().Contains("/umbraco/backoffice/umbracoapi/authentication/postlogin"))
                {
                    context.RewritePath("/umbraco/Api/CustomAuthentication/AuthorizedPostLogin");
                }
            }
    
            public void HandleSectionsRequest(object sender, EventArgs e)
            {
                var context = ((HttpApplication)sender).Context;
                var url = context.Request.Path.ToLower();
    
                if (url.Contains("/umbraco/backoffice/umbracoapi/section/getsections"))
                {
                    context.RewritePath("/umbraco/backoffice/UmbracoApi/CustomSection/GetAuthorizedGetSections");
                }
            }
    
            public void HandleDashboardRequest(object sender, EventArgs e)
            {
                var context = ((HttpApplication)sender).Context;
                var url = context.Request.Path.ToLower();
                var queryString = context.Request.QueryString;
    
                if (url.Contains("/umbraco/backoffice/umbracoapi/dashboard/getdashboard"))
                {
                    context.RewritePath("/umbraco/backoffice/UmbracoApi/CustomDashboard/GetAuthorizedDashboard?section=" + queryString["section"]);
                }
            }
    
            public void Initialize()
            {
                ContentService.Saving += this.HandleLoginRequest;
                ContentService.Saving += this.HandleSectionsRequest;
                ContentService.Saving += this.HandleDashboardRequest;
            }
    
            public void Terminate()
            {
                ContentService.Saving -= this.HandleLoginRequest;
                ContentService.Saving -= this.HandleSectionsRequest;
                ContentService.Saving -= this.HandleSectionsRequest;
            }
        }
    }
    

    Does anyone know of either a better way of accomplishing this or of a service that is guaranteed to be used for every request made to the Backoffice? Any advice is much appreciated!

Please Sign in or register to post replies

Write your reply to:

Draft