Jason Evans
Jason Evans

Reputation: 29186

How to get current member in a Razor macro

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:

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

Upvotes: 6

Views: 7258

Answers (4)

Oleksandr Skrypnyk
Oleksandr Skrypnyk

Reputation: 394

Now it's even easier, in the surfaceController you can use just one line:

var member = ApplicationContext.Current.Services.MemberService.GetById(Members.GetCurrentMemberId());

If Members MembershipHElper isn't accessible:

var memberShipHelper = new MembershipHelper(UmbracoContext.Current);
var member = ApplicationContext.Current.Services.MemberService.GetById(memberShipHelper.GetCurrentMemberId());

Upvotes: 0

dampee
dampee

Reputation: 3437

Starting from v7 you can use the MembershipHelper

@Members.CurrentUserName
@Members.GetCurrentMember()
@Members.GetCurrentMemberId()

Upvotes: 3

amelvin
amelvin

Reputation: 9051

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();
}

Upvotes: 4

E.J. Brennan
E.J. Brennan

Reputation: 46839

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.

Upvotes: 13

Related Questions