7

I'm trying to access the logged in Umbraco User, not member, but I cant get it work.

I have tried with the following methods but none work, they all return null:

umbraco.BusinessLogic.User.GetCurrent()
UmbracoContext.UmbracoUser
UmbracoContext.Security.CurrentUser
umbraco.helper.GetCurrentUmbracoUser()

I can access the user, for example Name, with the code below:

UmbracoContext.Application.Services.UserService.GetByEmail("name@company.com").Name

I have tried this code being logged in as a user and member, only user, only member and not logged in at all and it always returns the same result, null.

I have tried the code in a SurfaceController and UmbracoApiController with the same result. There is no problem getting the logged in member with Membership.GetUser(); Has anyone else experienced this?

Using: Umbraco version 7.3.5 assembly: 1.0.5858.25602

Ogglas
  • 62,132
  • 37
  • 328
  • 418

2 Answers2

10

Yeah I've tried to access the current backend user in numerous ways before and found the only way is to do:

var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
if (userTicket != null)
{
    var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418
Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • Worked flawlessly with one minor edit. Current seems to have been replaced so now I only used "var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);" – Ogglas Feb 12 '16 at 14:41
  • 1
    Ah weird, it's working for me in 7.3.6 with that. But glad it helped! Just checked your version on 7.3.6 and it works as well so I've approved that edit. – Ryan McDonough Feb 12 '16 at 14:42
5

I have just yesterday implemented some code to get the current user using:

UmbracoContext.Current.Security.CurrentUser

That is not listed as one of the options you have tried?

ProNotion
  • 3,662
  • 3
  • 21
  • 30
  • Tried this as well but for some reason I don't get Current for UmbracoContext. The error I get is: "'UmbracoContext.Current' cannot be accessed with an instance reference; qualify it with a type name instead". I'm not trying to new it or anything like that, I get this error when I try it in Immediate Window as well. – Ogglas Feb 12 '16 at 14:29
  • What namespace is your UmbracoContext coming from? – ProNotion Feb 08 '18 at 14:14
  • @ProNotion You'll find UmbracoContext in namespace Umbraco.Web, check https://our.umbraco.com/apidocs/csharp/api/Umbraco.Web.UmbracoContext.html for more information. – Nordis Feb 11 '19 at 09:51