Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Connie DeCinko 931 posts 1160 karma points
    Jul 13, 2015 @ 17:56
    Connie DeCinko
    0

    How to display Member Name in Razor

    I am trying to display the currently logged in user name or login name for the currently logged in member. Such as you might do if you wanted the page to say Hello Connie. I cannot reliably get my member data. It seems to be displaying the information for the last person who logged into my site. How do I correctly display this in my Razor file?

    I have this currently:

    @using umbraco.cms.businesslogic.member

    You are logged in as @Member.GetCurrentMember().LoginName.ToString()

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 13, 2015 @ 19:18
    Dennis Aaen
    0

    Hi Connie,

    If want some thing that says you are logged in as Your name then you should take a look at the built in Login Status Razor code snippet.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers
    
    @{
        var loginStatusModel = Members.GetCurrentLoginStatus();
    
        Html.EnableClientValidation();
        Html.EnableUnobtrusiveJavaScript();
        Html.RequiresJs("/umbraco_client/ui/jquery.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
    
        var logoutModel = new PostRedirectModel();
    
        @*
            Here you can specify a redirect URL for after logging out, by default umbraco will simply
            redirect to the current page. Example to redirect to the home page:
    
            logoutModel.RedirectUrl = "/"; 
        *@
    }
    
    @* NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed *@
    @Html.RenderJsHere()
    
    @if (loginStatusModel.IsLoggedIn)
    {
        <p>You are currently logged in as @loginStatusModel.Name</p>
    
        using (Html.BeginUmbracoForm<UmbLoginStatusController>("HandleLogout"))
        {
             <fieldset>
                 <legend>Logout</legend>
                 <button>Logout</button>
             </fieldset>
    
            @Html.HiddenFor(m => logoutModel.RedirectUrl)
        }
    }
    

    Hope this helps,

    /Dennis

  • Matthew Kirschner 323 posts 611 karma points
    Jul 15, 2015 @ 15:09
    Matthew Kirschner
    0

    Hi, Connie.

    The most succinct approach I've found is to use the Umbraco MembershipHelper (see the documentation here).

    Your code in the view might look something like this:

    @if (Members.IsLoggedIn())
    {
        <p>You are logged in as @Members.GetCurrentMemberProfileModel().Name</p>
    }
    

    Where Members is a reference that automatically ships with your UmbracoViewPage.

Please Sign in or register to post replies

Write your reply to:

Draft