Copied to clipboard

Flag this post as spam?

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


  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Mar 10, 2020 @ 21:05
    Tim Geyssens
    0

    Umbraco V8 - Get Current Member in RenderMvcController

    Been trying several things but can't seem to:

    Check if there is a member logged in Get the member currently logged in

    Anyone get a snippet?

    I did manage to find out how to get the backoffice user from a component:

      var identity = (UmbracoBackOfficeIdentity)System.Web.HttpContext.Current.User.Identity;
    
    
      var currentUSer = Current.Services.UserService.GetByProviderKey(identity.Id);
    
  • Harikrishna Parmar 24 posts 199 karma points
    Mar 11, 2020 @ 05:44
    Harikrishna Parmar
    100

    Hello Tim,

    This would help you to fix the issue: Umbraco V8: In RenderMvcController, You can directly access MemberHelper. Using this Helper you can get current username.

      if (Members.IsLoggedIn())
      {
           var user = Members.CurrentUserName;
           var services = Current.Services.MemberService;
           var servicesResult = services.GetByUsername(user);
       }
    
  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Mar 11, 2020 @ 08:30
    Tim Geyssens
    1

    Super, thanks for the response #hero

  • Harikrishna Parmar 24 posts 199 karma points
    Mar 11, 2020 @ 08:56
    Harikrishna Parmar
    0

    Thank you, Tim. Let me know if any kind of help. I would love to help in Umbraco.

  • Bjarne Fyrstenborg 1170 posts 3401 karma points MVP 4x c-trib
    Mar 11, 2020 @ 09:15
    Bjarne Fyrstenborg
    0

    Hi Tim

    In Umbraco v8 you can also inject services in the controller:

    public class MyController : RenderMvcController
    {
        private readonly IMemberService _memberService;
    
        public MyController(IMemberService memberService)
        {
            _memberService = memberService;
        }
    
        public ActionResult Index()
        {
            var member = _memberService.GetById(1234);
        }
    }
    

    or if you don't need to do CRUD operations, just inject MembershipHelper.

    public class MyController : RenderMvcController
    {
        private readonly MembershipHelper _memberHelper;
    
        public MyController(MembershipHelper memberHelper)
        {
            _memberHelper = memberHelper;
        }
    
        public ActionResult Index()
        {
            var currentMember = _memberHelper.GetCurrentMember();
        }
    }
    

    I haven't tested it, but I think it should work.

    /Bjarne

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Mar 11, 2020 @ 09:54
    Tim Geyssens
    0

    Thanks! Yeah comes in handy to inject the MembershipHelper :)

  • Jamie Attwood 146 posts 361 karma points
    Nov 24, 2020 @ 14:47
    Jamie Attwood
    0

    Does anyone know if there is an Interface for MembershipHelper? Can't seem to see that anywhere. It would be better if we could inject the abstraction, but it doesn't seem to exist.

  • Bjarne Fyrstenborg 1170 posts 3401 karma points MVP 4x c-trib
    Nov 24, 2020 @ 16:47
    Bjarne Fyrstenborg
    0

    Hi Jamie

    Currently there isn't an interface for MembershipHelper and UmbracoHelper, so you would have to inject MembershipHelper or UmbracoHelper or you can access UmbracoHelper via Members / Umbraco properties in controllers inheriting from Umbraco specific controllers.

    It is also possible to access MembershipHelper via UmbracoHelper, but this is now obsolete https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/UmbracoHelper.cs#L94-L98 so it is recommended to either inject MembershipHelper or access it via Members property.

    In Umbraco vNext I think this will change, but not sure if it going to change in Umbraco v8.

    /Bjarne

  • Jamie Attwood 146 posts 361 karma points
    Nov 24, 2020 @ 17:34
    Jamie Attwood
    0

    Great - thanks for your help Bjarne! MembershipHelper it is.

    Cheers,

    Jamie

Please Sign in or register to post replies

Write your reply to:

Draft