x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar48posts68karma

    How to get current member in a Razor macro.

    Jason Evans started this topic October 3, 2012 @ 05:06

    Hi there.

    I'm unable to get the current member inside a Razor macro.

    I have tried the following methods:

    • Calling Member.GetCurrentMember(), but this returns NULL.
    • Calling Membership.GetUser(), but this returns NULL

    Basically I need to get the current member in orde to do some processing using that member's ID.

    Are the above the only ways of trying to get the current member, or is there an alternative?

    Cheers.
    Jas.


  • Avatar1986posts3671karma
    Comment with ID: 127829
    Fuji Kusaka posted this reply October 3, 2012 @ 07:55

    Hi Jason,

    When using razor remember to add those and run a check to see if a member is well login.

    @using umbraco.cms.businesslogic.member
    @using System.Web 
    @using System.Web.Security

    @{
         var currentUser = Member.GetCurrentMember();
     
          if (currentUser!=null)
           {   
                  var userName = currentUser.Text;
                            
                 @userName
           }
     
      }

  • Avatar613posts1015karma
    Comment with ID: 127854
    Dan Diplo posted this reply October 4, 2012 @ 10:18

    You can also access member properties via standard ASP.NET provider model, so you can do this:

    if (User.Identity.IsAuthenticated)
    {
        <p>Name is @User.Identity.Name</p>
    }
    

  • Jason Evans posted this reply October 4, 2012 @ 10:20

    Hi Fuji.

    Thank you for your reply. I managed to get assistance with this issue from someone, who pointed out that I needed to right-click on the root "Content" node in the Umbraco content tree, and then choose "Republish".

    After I did that, all of the images which were originally missing now appeared. So after manually running SQL scripts for the images, I need to manually "Republish" the content.

    EDIT: Please ignore the above comment, it was meant for another question I had asked! Doh.


  • Jeroen Breuer posted this reply October 4, 2012 @ 06:18

    If you use the Member API it does a lot of calls to the database. This example can help: http://our.umbraco.org/forum/developers/extending-umbraco/27626-Accessing-Member-Information?p=1#comment105493.

    Jeroen


  • Jason Evans posted this reply October 5, 2012 @ 10:03

    Hi guys.

    Right I've been away for a couple of days, so sorry for the delay.

    This turned out to be a total DOH! moment. Turns out that the Umbraco client site that I'm working one does not display a login page by default (don't ask), so each time I was testing my code, I wasn't actualyl logged in - I did not know that, since it never asked me to log in!!! Argh.

    I have to manually navigate to the login page, enter a username and passwed and hey presto all the Member API stuff now works. Humph.

    Thanks for all your efforts though guys.

    Cheers.
    Jas. 


Please login or Sign up To post replies