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 146 posts 502 karma points
    Jun 04, 2016 @ 13:28
    Tim C
    0

    I'd like to show the user id of the currently logged in user : I can get their name

    // logged in?
                var wrapper = new HttpContextWrapper(HttpContext.Current);
                var ticket = wrapper.GetUmbracoAuthTicket();
                isLoggedIn = 0;
                sessionId = "";
                if (ticket!=null){
                    if(ticket.GetRemainingAuthSeconds() > 0){
                        isLoggedIn = 1;
                    }
                    loggedInUsername = ticket.Name.ToString();
                    sessionId = wrapper.Session.SessionID.ToString();
                }else{
                    loggedInUsername = "";
                }
    

    but I'd also like to get their user id so I can store extra user into in a separate database and look it up on the page.

    I did try

    @Umbraco.GetCurrentUmbracoUser().Id
    

    but I think that's from an older version of Umbraco as it crashed Umbraco 7.4

  • Sebastiaan Janssen 4850 posts 14395 karma points MVP admin hq
    Jun 05, 2016 @ 08:22
    Sebastiaan Janssen
    0

    Have you tried: var user = Umbraco.Web.UmbracoContext.Current.Security.CurrentUser;? :)

  • Tim C 146 posts 502 karma points
    Jun 05, 2016 @ 09:27
    Tim C
    0

    Doesn't work

    Compiler Error Message: CS1061: 'Umbraco.Web.UmbracoHelper' does not contain a definition for 'Web' and no extension method 'Web' accepting a first argument of type 'Umbraco.Web.UmbracoHelper' could be found (are you missing a using directive or an assembly reference?)
    

    No I need to be 'using' another namespace or is it no longer available in 7.4?

  • Sebastiaan Janssen 4850 posts 14395 karma points MVP admin hq
    Jun 05, 2016 @ 11:43
    Sebastiaan Janssen
    0

    Hmm, are you doing this from a View?

    This should work:

    using Umbraco.Core.Security;
    using Umbraco.Core.Models.Membership;
    
    var authenticationTicket = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
    IUser user = null;
    if (authenticationTicket != null)
    {
        user = UmbracoContext.Current.Application.Services.UserService.GetByUsername(authenticationTicket.Name);
    }
    else
    {
        // not logged into backoffice
    }
    
    // use it if not null
    if(user != null)
    {
        var name = user.Name;
    }        
    
  • Tim C 146 posts 502 karma points
    Jun 05, 2016 @ 12:36
    Tim C
    0

    Sorry

    Compiler Error Message: CS1061: 'System.Web.HttpContextWrapper' does not contain a definition for 'GetUmbracoAuthTicket' and no extension method 'GetUmbracoAuthTicket' accepting a first argument of type 'System.Web.HttpContextWrapper' could be found (are you missing a using directive or an assembly reference?)

  • Sebastiaan Janssen 4850 posts 14395 karma points MVP admin hq
    Jun 05, 2016 @ 14:03
    Sebastiaan Janssen
    1

    Do you have using Umbraco.Core.Security;?

  • Tim C 146 posts 502 karma points
    Jun 05, 2016 @ 21:01
    Tim C
    0

    Yes but user.Name returns their name not id.

  • Sebastiaan Janssen 4850 posts 14395 karma points MVP admin hq
    Jun 05, 2016 @ 21:14
    Sebastiaan Janssen
    102

    Try user.Id instead then. :)

  • Tim C 146 posts 502 karma points
    Jun 06, 2016 @ 03:53
    Tim C
    0

    I thought I had guessed that and it hadn't worked, but tried again and it has - that''s it!

    Thanks for your help

Please Sign in or register to post replies

Write your reply to:

Draft