Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    May 31, 2016 @ 03:42
    Tim C
    0

    Show user id and user name in front end

    Is it possible to show whether a user (not member) is logged in and if so, their id and user name in the front end, ie in a template or partial view?

    Thanks

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    May 31, 2016 @ 05:28
    Nathan Woulfe
    0

    Hi Tim

    Absolutely should be possible.

    Use UmbracoContext.Current.Security.CurrentUser to retrieve the current user, Name and Id are properties of the returned User model. Something like the below should work.

    @{
        var user = UmbracoContext.Current.Security.CurrentUser;
        if (user != null) {        
            <p>Name: @user.ProfileData.Name</p>
            <p>ID: @user.ProfileData.Id</p>
        }
    }
    
  • Tim C 161 posts 528 karma points
    May 31, 2016 @ 06:39
    Tim C
    0

    Hmm, tried this on a template with

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    but user is null even though I am logged in to the back end CMS as a user on the same browser..

    Do I need to add any other references to the template?

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    May 31, 2016 @ 07:14
    Paul Seal
    101

    Try this in your partial

    @{ 
        var myUser = System.Web.Security.Membership.GetUser();
        var umbracoMember = ApplicationContext.Services.MemberService.GetByUsername(myUser.UserName);
    }
    
    @myUser.UserName
    @umbracoMember.Id
    
  • Tim C 161 posts 528 karma points
    May 31, 2016 @ 07:15
    Tim C
    0

    oops, no that's member details, I'm after User details!

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    May 31, 2016 @ 07:29
    Paul Seal
    0

    In that case you want this then Tim

    @{
        var myUser = System.Web.Security.Membership.GetUser();
        var umbracoUser = ApplicationContext.Services.UserService.GetByUsername(myUser.UserName);
    }
    
    @myUser.UserName
    @umbracoUser.Id
    
  • Tim C 161 posts 528 karma points
    May 31, 2016 @ 07:42
    Tim C
    0

    Sorry, that doesn't work

    are you sure

    var myUser = System.Web.Security.Membership.GetUser();
    

    doesn't just work with members?

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    May 31, 2016 @ 07:55
    Paul Seal
    0

    Ok, try this.

    @{
        var userName = System.Web.HttpContext.Current.User.Identity.Name;
        var umbracoUser = ApplicationContext.Services.UserService.GetByUsername(userName);
    }
    
    @userName
    @umbracoUser.Id
    
  • Tim C 161 posts 528 karma points
    May 31, 2016 @ 08:03
    Tim C
    0

    Sorry, no.

    I am using this in a partial view, is this correct?

  • colin gray 16 posts 56 karma points
    Feb 07, 2017 @ 14:25
    colin gray
    0

    Similarly, I wanted to display a picked User for a node. (v7) Inside my template I needed this (but it wasn't a partial)

    string OwnerS = Umbraco.Field("testPicker").ToString(); var OwnerName = ""; if (OwnerS != "") { int OwnerInt = Int32.Parse(OwnerS); OwnerName = ApplicationContext.Services.UserService.GetByProviderKey(OwnerInt).Name; } then rendered @OwnerName

  • Jason Elkin 38 posts 351 karma points MVP 3x c-trib
    Feb 07, 2017 @ 15:52
    Jason Elkin
    1

    I've had success in the past using this method:

    var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
    if (userTicket != null)
    {
        var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
    }
    

    From here.

    You can then do things like:

    private bool _IsAdminLoggedIn = false;
    if (!String.IsNullOrEmpty(currentUser.UserType.Alias) && currentUser.UserType.Alias == "admin")
    {
    _IsAdminLoggedIn = true
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft