My friend Nik had another question for me today.

He wanted to know how to get the current logged in Umbraco user when you are in the Content Service Saving event.

So I knocked up this example for him and thought it would be a good idea to create a blog post for it so it doesn't get lost in slack messages.

using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Events;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Web;
using Umbraco.Web.Security;

namespace CodeShare.Core.Composing
{
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class SavingEventComposer : ComponentComposer<SavingEventComponent>
    { }

    public class SavingEventComponent : IComponent
    {
        private readonly IUmbracoContextFactory _umbracoContextFactory;

        public SavingEventComponent(IUmbracoContextFactory umbracoContextFactory)
        {
            _umbracoContextFactory = umbracoContextFactory;
        }

        public void Initialize()
        {
            ContentService.Saving += ContentService_Saving;
        }

        private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
        {
            IUser currentUser = null;

            // use the umbraco context factory to ensure the umbraco context which we need to get the auth ticket of the user
            using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
            {
                var userTicket = umbracoContextReference.UmbracoContext.HttpContext.GetUmbracoAuthTicket();
                if (userTicket != null)
                {
                    currentUser = Current.Services.UserService.GetByUsername(userTicket.Identity.Name);
                }
            }
        }

        public void Terminate()
        {
            // Nothing to terminate
        }
    }
}

Again, I hope someone else finds this useful.

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.