Check user backoffice session from frontend view?

Hi,

This method seems to work - a bit more work than you might want.

Basically create the service BackofficeUserAccessor

I found a small mistake in the interface - the return should be ClaimsIdentity.

public interface IBackofficeUserAccessor
{
  ClaimsIdentity BackofficeUser { get; }
}

Ensure you’ve registered this in your Program.cs

Then just inject into your view:

@using Umbraco.Cms.Core.Security
@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@inject UmbracoScratchpad.IBackofficeUserAccessor _backofficeUserAccessor
@{
Layout = “layout.cshtml”;

var currentUser = _backofficeUserAccessor.BackofficeUser;

}
@if(currentUser != null)
{
Hi there - @(currentUser.GetUserName())
}
else
{
You’re not logged into the CMS!
}

HTH

Steve

1 Like