Copied to clipboard

Flag this post as spam?

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


  • Sean Håkansson 66 posts 192 karma points
    Sep 25, 2017 @ 12:41
    Sean Håkansson
    0

    Umbraco backoffice IP logging

    Hi I would like to log server side what IPs are visiting our backoffice URL(s).

    What files do I need to edit and insert my own logic? Umbraco/Views/Default.chtml looks like it might be the backoffice "master layout" ?

    Thanks, Sean

  • Martin Aguirre 42 posts 210 karma points
    Sep 25, 2017 @ 14:44
    Martin Aguirre
    1

    Hi, Maybe you must add some backend code like this.

    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using System.Web;
    using Umbraco.Core.Models.Membership;
    
    internal static class AuditHelper
    
    {
    internal static string RemoteHostName
    {
        get
        {
            string hostName = String.Empty;
    
            try
            {
                hostName = Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]).HostName;
            }
            catch(Exception)
            {
                hostName = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
    
            return hostName;
        }
    }
    
  • Sean Håkansson 66 posts 192 karma points
    Sep 25, 2017 @ 15:32
    Sean Håkansson
    0

    Awesome, thanks Martin!

    Would you call this method from the Default view or would you attach it the backed to an event? I don't like editing files that will possibly get overwritten by an update, but I don't know how I can extend this view. Only every time a request is sent to the back office.

  • Martin Aguirre 42 posts 210 karma points
    Sep 25, 2017 @ 15:49
    Martin Aguirre
    0

    Put a class in the App code folder.

    i.e.: ApplicationEvents.cs

    In these class manage funtions or methods to catch

    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web;
    using System.Web.Http;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    using Umbraco.Web.Trees;
    
    namespace YourNamespace.App_Code
    {
        public class ApplicationEvents : ApplicationEventHandler
    
        { 
    
        }
    
    
         protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
               base.ApplicationStarted(umbracoApplication, applicationContext);
    
                ContentService.Saving += ContentService_Saving;
                ContentService.Trashing += ContentService_Trashing;
                ContentService.Publishing += ContentService_Publishing;
                ContentService.Saved += ContentService_Saved;
                ContentService.Published += ContentService_Published;
                ContentService.Moving += ContentService_Moving;
    
    
            }
    
    
    
    
    
         private void ContentService_Moving(IContentService sender, MoveEventArgs<IContent> e)
            {
    
            // manage event
            }
    
    
        private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
            {
            // manage event
            }
    
          private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
            {
    
            // manage event     
            }
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Sep 25, 2017 @ 15:38
    Nicholas Westby
    100

    You could probably implement Application_BeginRequest in a class that inherits from UmbracoApplication and that is referenced in your Global.asax. You'd then simply have to look at the beginning of the path to ensure it starts with umbraco.

  • Sean Håkansson 66 posts 192 karma points
    Sep 25, 2017 @ 15:43
    Sean Håkansson
    0

    Thanks, I think this is the best option. As long as I don't do the lookup before checking the URL path, performance should be negligible right?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Sep 25, 2017 @ 15:57
    Nicholas Westby
    0

    As long as I don't do the lookup before checking the URL path, performance should be negligible right?

    For the frontend of the site, performance should be negligible. I doubt the backend will slow that much (depends on how you do the logging), but if you really want to ensure optimal performance you could do the logging on a background thread.

Please Sign in or register to post replies

Write your reply to:

Draft