Copied to clipboard

Flag this post as spam?

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


  • Chester Campbell 98 posts 209 karma points
    Apr 17, 2014 @ 19:52
    Chester Campbell
    0

    how to get the current user with UserService

    I just upgraded a new project from Umb 7.0.3 to 7.1.0 and now Visual Studio is telling me that the way I get current user is deprecated. Specifically, this:

    umbraco.BusinessLogic.User.GetCurrent()
    

    now is tagged with:

    Warning:
    'umbraco.BusinessLogic.User' is obsolete: "Use the UserService instead"
    

    Which I'm attempted to do however I can find no way to get the current user via the UserService. I see methods to get users by their id, their username, their email address, etc. Which are all great but don't help me as I don't have any of that information. I just want to get the currently logged in user for error logging purposes. What am I overlooking?

  • Charles Afford 1163 posts 1709 karma points
    Apr 21, 2014 @ 21:00
    Charles Afford
    0

    Hi Chester,

    Sorry no one replied.  Hope this does not put you of the forum :)

    You can take a look at: http://www.charlesafford.com/umbraco-membership-provider.aspx

    That should give you the basic idea.  Basically you want to use the .net membership API.

    Are you trying to get the member (logged in to the umbraco backend) or the member (user logged into the website)?

    If your answer you should use the MemberService()

    MemberService memberservice = new MemberService()

    Charlie :) 

     

  • Dan Mothersole 22 posts 109 karma points
    Apr 28, 2014 @ 14:43
    Dan Mothersole
    0

    using Umbraco.Core.Services;

    using Umbraco.Core.Persistence;

    var userService = new UserService(new RepositoryFactory());

    This will give access to the user service - I use IOC to pass in a userService but you can work that part out yourself.

    Once you have this userService look at the methods you can call - I don't see on for the current logged in member.

    Personal I grab the user by email - DL the source code form git and have a look through the services and see if another class has a method that matches your needs.

     

  • Chester Campbell 98 posts 209 karma points
    Apr 28, 2014 @ 17:30
    Chester Campbell
    1

    @Charles Afford ... I'm asking about the current User, not the current Member hence my using Umbraco's new UserService. But you're correct, I need to get the current User from the .Net membership provider and then use the UserService to get the ID. Here's what I ended up doing (wrapped in a helper method):

        private int GetCurrentUserId()
        {
            var userService = ApplicationContext.Current.Services.UserService;
            return userService.GetByUsername(HttpContext.Current.User.Identity.Name).Id;
        }
    

    @Dan Mothersole ... I know how to get the UserService instance and I have already looked through the available methods, hence my question as to how to do what I'm wanting to do. Also, I have downloaded the source and picked through it as well. Some interesting things in there, but didn't find the answer to what I was looking for.

  • Dan Lister 416 posts 1974 karma points c-trib
    Jun 16, 2014 @ 22:38
    Dan Lister
    5

    How about the following within the Umbraco.Web namespace?

    UmbracoContext.Current.Security.CurrentUser
  • Charles Afford 1163 posts 1709 karma points
    Jun 16, 2014 @ 23:00
    Charles Afford
    0

    Sorry Chester never got a notifcation you posted

  • John French 32 posts 83 karma points
    Aug 05, 2014 @ 08:24
    John French
    1

    You can do this..

    var current = UmbracoContext.Current;

    var user = current.UmbracoUser;

  • Flavio Spezi 129 posts 315 karma points
    Apr 02, 2015 @ 13:00
    Flavio Spezi
    1

    I thnk that in Umbraco 7.2.4 there is a problem.

    I used Umbraco.Web.UmbracoContext.Current.Security.CurrentUser to obtain current backoffice user. It was works in older Umbraco versions.
    But it does not works in "Umbraco 7.2.4".

    How can I solve it?

    Thanks

  • Murray Roke 503 posts 966 karma points c-trib
    Aug 18, 2015 @ 01:44
    Murray Roke
    0

    I want to know if the back office user is logged in on a front end template, I'm using umbraco 7.2.8

    None of the above work for me, so I've reverted to:

     if (umbraco.BusinessLogic.User.GetCurrent() != null)
     {
         // do admin type stuff here.
     }
    

    See why here: http://issues.umbraco.org/issue/U4-6496

  • Flavio Spezi 129 posts 315 karma points
    Aug 19, 2015 @ 07:02
    Flavio Spezi
    100

    I'm using this code:

    private Umbraco.Core.Models.Membership.IUser internalCurrentUser() {
        var userService = Umbraco.Core.ApplicationContext.Current.Services.UserService;
    
        // Snippet from: http://issues.umbraco.org/issue/U4-6342#comment=67-19466
        var httpCtxWrapper = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
        var umbTicket = httpCtxWrapper.GetUmbracoAuthTicket();
    
        if (umbTicket == null || umbTicket.Name.IsEmpty() || umbTicket.Expired) return null;
    
        var user = userService.GetByUsername(umbTicket.Name);
        return user;
    }
    
  • Ben Norman 167 posts 276 karma points
    Apr 30, 2016 @ 03:35
    Ben Norman
    0

    I am running 7.4.3 and I can use to get the current user in a class inheriting from UmbracoAuthorizedJsonController.

    var currentUser = Umbraco.UmbracoContext.Security.CurrentUser;
    
  • Mihai Savu 11 posts 75 karma points
    Jun 23, 2016 @ 14:17
    Mihai Savu
    0

    I'm using Umraco 7.4.3 as well and discovered that the current user is null right after login in backoffice.

    // user1 is null
    var user1 = UmbracoContext.Current.Security.CurrentUser;
    // user2 is also null
    var user2 = UmbracoContext.Current.UmbracoUser;
    

    However, the user is null only when using UmbracoContext.Current. When using umbraco.BusinessLogic then the user is not null.

    // user3 is not null
    var user3 = umbraco.BusinessLogic.User.GetCurrent();
    // user4 is not null
    var user4 = umbraco.helper.GetCurrentUmbracoUser();
    

    After a while (30 seconds or 1 minute maybe), I try to read user1 and user2 again and they are not null anymore!

    Is there a caching issue regarding the authenticated user?

  • Manish Agrawal 25 posts 218 karma points
    Aug 10, 2017 @ 11:36
    Manish Agrawal
    1

    Umbraco.UmbracoContext.Security.CurrentUser;

    this works for me.

Please Sign in or register to post replies

Write your reply to:

Draft