Get current user in Umbraco version 7.3.5
Asked Answered
G

2

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("[email protected]").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

Ganda answered 12/2, 2016 at 13:48 Comment(0)
M
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);
}
Monserratemonsieur answered 12/2, 2016 at 14:32 Comment(2)
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);"Ganda
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.Monserratemonsieur
R
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?

Rothwell answered 12/2, 2016 at 14:21 Comment(3)
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.Ganda
What namespace is your UmbracoContext coming from?Rothwell
@Rothwell You'll find UmbracoContext in namespace Umbraco.Web, check our.umbraco.com/apidocs/csharp/api/… for more information.Marijuana

© 2022 - 2024 — McMap. All rights reserved.