Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current member in a Razor macro

Tags:

macros

umbraco

I have asked this question on the Our Umbraco forums, however I wanted to also increase my chances of getting a solution by posting the same question here.

The issue I have is that, inside a Razor macro, I'm unable to get the current Member who is accessing the site. I have tried the following methods:

  • Calling Member.GetCurrentMember(), but this returns NULL.
  • Calling Membership.GetUser(), but this returns NULL
  • Calling UmbracoEnsuredPage.CurrentUser returned NULL;

Is there another way to get the current Member seeing how the above methods do not work in my case?

like image 550
Jason Evans Avatar asked Oct 03 '12 15:10

Jason Evans


3 Answers

var m = Membership.GetUser(); 

That should work, just verified it myself on 4.7.1; it will return NULL if you are not logged in as a member, but when you log in it should get you what you want.

like image 128
E.J. Brennan Avatar answered Sep 20 '22 03:09

E.J. Brennan


Just a slight change from @E.J.Brennan if the NULL is an issue you can check if you are logged on before trying to GetUser():

if (umbraco.library.IsLoggedOn())
{
  m = Membership.GetUser();
}
like image 30
amelvin Avatar answered Sep 20 '22 03:09

amelvin


Starting from v7 you can use the MembershipHelper

@Members.CurrentUserName
@Members.GetCurrentMember()
@Members.GetCurrentMemberId()
like image 34
dampee Avatar answered Sep 21 '22 03:09

dampee