Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Mar 10, 2015 @ 21:50
    jake williamson
    0

    confusion over the 'MemberService' and 'UserService' and getting the current umbraco user

    hey out there,

    i've found lots of pointers online but no concrete solution to what i'm trying to achieve. i'm also getting a lot of 'obsolete' warnings with code i'm finding online...

    i have a form in my website that i want to show an additional checkbox if an admin user is logged into the umbraco back office. i don't have any membership stuff set up on this site.

    so, which service do i use?! i've tried both and can't seem to get the currently logged in umbraco user using either!

    based on what i'm finding, it should be something like:

    var memberService = ApplicationContext.Current.Services.MemberService;
    var member = memberService.GetByUsername(HttpContext.Current.User.Identity.Name);
    
    var userService = ApplicationContext.Current.Services.UserService;
    var user = userService.GetByUsername(HttpContext.Current.User.Identity.Name);
    

    but neither of these return me a user!

    can someone provide a any clarity?!

    cheers,

    jake

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 10, 2015 @ 21:56
    Jan Skovgaard
    0

    Hi Jake

    Where have you found the userService? I have not been able to find anything about it in the documentation section.

    However - In Umbraco terms Users are people who can access the Umbraco backoffice and Members are people who can access a login restricted area on the public website.

    /Jan

  • jake williamson 207 posts 873 karma points
    Mar 11, 2015 @ 10:06
    jake williamson
    0

    hey jan,

    thanks for clarifying that - i hadda feelin that would be the case as in 'users' are umbraco back office and 'members' are website users.

    the main pointers i've found for the user service from another forum post:

    https://our.umbraco.org/forum/developers/api-questions/50075-how-to-get-the-current-user-with-UserService

    i went through pretty much the same steps as outlined in the post, i.e. visual studio telling me 'umbraco.BusinessLogic.User.GetCurrent()' was obsolete and i should 'Use the UserService instead'.

    the main problem i'm having is that 'HttpContext.Current.User.Identity.Name' (which i thought uses the asp.net membership stuff to get the guid of the currently logged in user) is always returning empty, regardless of if i'm logged into umbraco or not...

    which says to me that i need to get the currently logged in umbraco user in a different way - the question is how ;)

    the search continues...

    cheers,

    jake

  • jake williamson 207 posts 873 karma points
    Mar 23, 2015 @ 20:30
    jake williamson
    101

    got it! bit more googling and came up with:

    public IUser GetCurrentUser()
    {
        var auth = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
    
        if (auth == null) return null;
    
        var user = ApplicationContext.Current.Services.UserService.GetByUsername(auth.Name);
    
        return user;
    }
    

    works a treat ;)

Please Sign in or register to post replies

Write your reply to:

Draft