How do i determine if a backoffice user is logged in from a razor view?

User.GetUmbracoIdentity() is always null probably since we haven’t Authenticated.

AuthenticationService.AuthenticateAsync doesn’t work since the cookie manager stops the flow if it’s not a backoffice route.

IBackOfficeSecurityAccessor BackofficeSecurityAccessor
doesn’t seem to help

Seems pretty impossible right now?


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/106857-how-do-i-determine-if-a-backoffice-user-is-logged-in-from-a-razor-view

Has anyone worked out how to do this in Umbraco v14+ ?
Especially if the same admin manages multiple domain names.

”This” being: work out if the current user is a backoffice user in order to show them an edit button on each page that will allow them to jump direclty to the umbraco edit page for the current page.

Hi @Myster

I came across your question because it was linked from another forum post.

In case you’re still looking for a solution (or in case you’ve ended up re-using the core backoffice auth in your frontend rendering), here’s a blog post that might be of relevance: https://kjac.dev/posts/using-umb_ucontext-with-umbraco-14-plus/

Hiya
In the TagHelpers project there is an our-user-include and our-user-exclude to apply to the razor markup to show or hide markup to the users on the frontend razor rendering.

Interesting to see @kjac approach. This is probably the RIGHT way to do it, considering he’s one of the smart chaps at Umbraco HQ core team.

Project

Usage

<h2>our-user-include and our-user-exclude</h2>
<p>
    This is a tag helper attribute that can be applied to any DOM element in the razor template or partial. It will show or hide its element and children on the page when passing a comma seperated string of user groups that the current logged in Umbraco backoffice user is in, for the exclude or include variants.
</p>
<p>
    There are two special User Groups you can use:
</p>
<ul>
    <li> * - All anonymous users</li>
    <li>? - All authenticated users</li>
    <li>Use the alias of the User Group</li>
</ul>

<h4>Include</h4>
<div our-user-include="admin">Only users in the <strong>✅ Admin</strong> group will see this.</div>
<div our-user-include="admin,editor">Only users in the <strong>✅ Admin</strong> or <strong>✅ Editor</strong> user group will see this.</div>
<div our-user-include="?">Only <strong>✅ Anonymous</strong> users will see this.</div>
<div our-user-include="*">Only <strong>✅ logged in</strong> users will see this.</div>


<h4>Exclude</h4>
<div our-user-exclude="editor">Everyone except <strong>❌ Editor</strong> users can see this.</div>
<div our-user-exclude="editor">Everyone except <strong>❌ Admin</strong> or <strong>❌ Editor</strong> users can see this.</div>
<div our-user-exclude="?">Everyone except <strong>❌ Anonymous</strong> users will see this.</div>
<div our-user-exclude="*">Everyone except <strong>❌ logged in</strong> users will see this.</div>

Source Code

TL:DR

  • Use the taghelper project above or directly inject & use IBackofficeUserAccessor as needed

I’m specifically talking about umbraco 14+ this package only targets up to v13.
If the marketplace page is correct.

Ah apologies, you could lift & shift the source code from the project into your own code in the meantime whilst you wait on me trying to get a v17 release sorted.

Or perhaps create your own logic or approach of a taghelper using the IBackofficeUserAccessor or perhaps the more correct way from Kenn ?