Copied to clipboard

Flag this post as spam?

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


  • Robert Mulder 79 posts 272 karma points c-trib
    Sep 25, 2014 @ 16:43
    Robert Mulder
    0

    Memberservice

    I'm trying to get the currently logged in Member, but when I use the GetCurrentMember() method of the Member class:

    umbraco.cms.businesslogic.member.Member.GetCurrentMember();
    

    Visual Studio points out that:

    'umbraco.cms.businesslogic.member.Member' is obsolete: '"Use the MemberService and the Umbraco.Core.Models.Member models instead"'

    Which is all good and stuff, but the MemberService (via ApplicationContext.Services.MemberService) doesn't seem to have a method to get the curretn member.

    So... now what? Do I really need to

  • Charles Afford 1163 posts 1709 karma points
    Sep 28, 2014 @ 18:54
    Charles Afford
    0

    What version of Umbraco are you using? :)

  • Robert Mulder 79 posts 272 karma points c-trib
    Sep 29, 2014 @ 10:41
    Robert Mulder
    0

    I'm currently using version 7.1.4

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Sep 29, 2014 @ 12:17
    Jeroen Breuer
    0

    Do you only need to get the current member to view some data or also change it. Otherwise you could use this: 

    var member = Umbraco.TypedMember(id)

    //or

    var member =  Members.GetById(id)

    You can get the id by getting the current identity.

    Jeroen

  • Robert Mulder 79 posts 272 karma points c-trib
    Sep 29, 2014 @ 12:31
    Robert Mulder
    0

    Yeah, that's kind of my point. It's pretty easy to just say:

    ApplicationContext.Services.MemberService.GetByUsername(Context.User.Identity.Name);
    

    But having to provide the identity name like this makes me wonder why there isn't just a GetCurrentMember() method on the member service? Was it omitted for a reason? It was obviously there on the now obsolete old Member object...

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Sep 29, 2014 @ 12:33
    Jeroen Breuer
    0

    I'm not really sure why it's missing. Maybe create an issue for it here: http://issues.umbraco.org/issues#newissue=yes

    Jeroen

  • Robert Mulder 79 posts 272 karma points c-trib
    Oct 01, 2014 @ 09:31
    Robert Mulder
    0

    Added the PR https://github.com/umbraco/Umbraco-CMS/pull/494

    For now I'll just use a utils function to do it I suppose. Thanks anyway.

  • Robert Mulder 79 posts 272 karma points c-trib
    Oct 01, 2014 @ 16:35
    Robert Mulder
    0

    It seems the Umbraco gods are not in my favor. The MemberService is apparently not meant for this purpose and instead I'm supposed to use the MembershipHelper (in Razor or SurfaceControllers available via the Members property)

    This is all great, but does anyone know how to get to the Member properties using the MembershipHelper? For instance, when using the Members.GetCurrentMember() method as is suggested I get an IPublishedContent object.

    This object obviously does not have properties for UserName, Email, IsApproved, etc. Looking at the Umbraco sources I notice that the actual type returned by the GetCurrentMember() method is MemberPublishedContent WHICH IS INTERNAL.... AARRGHHH...

    So to determine whether or not the currently logged in user has comments set I need to do something like this:

    var theMember = Members.GetCurrentMember();
    var theRealMember = ApplicationContext.Current.Services.MemberService.GetById(theMember.Id);
    var theComments = theRealMember.Comments;
    

    Getting the same user twice just because they're different types does not seem at all logical to me. Does anyone know if I'm missing something completely (or why there are multiple ways to get members that return completely different and incompatible types)?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 01, 2014 @ 17:45
    Rusty Swayne
    1

    I've been using the Umbraco.Web.Security.MembershipHelper

    var membershipHelper = new MembershipHelper(UmbracoContext);
    
    var memberId = membershipHelper.GetCurrentMemberId();
    
    var member = ApplicationContext.Current.Services.MemberService.GetById(memberId);
    

    @robert - I agree, it seems like there should be a better way.

  • Shannon Deminick 1524 posts 5270 karma points MVP 2x
    Oct 02, 2014 @ 02:56
    Shannon Deminick
    100

    The issue is logged here:

    http://issues.umbraco.org/issue/U4-5456

    Should be fixed up in final release of 7.2, I'll update the priority to 'major'

  • Robert Mulder 79 posts 272 karma points c-trib
    Oct 02, 2014 @ 09:26
    Robert Mulder
    0

    Thanks Shannon, I'll wait for 7.2 then :)

  • Shannon Deminick 1524 posts 5270 karma points MVP 2x
    Oct 02, 2014 @ 11:58
    Shannon Deminick
    0

    As Asbjørn mentions though, you can access them all dynamically:

     var member = Members.GetById(item.MemberId);
     string membernumber = member.AsDynamic().UserName;
    

    The properties you can access are:

    UserName, Email, Comments, PasswordQuestion, IsApproved, IsLockedOut, LastLockoutDate, CreationDate, LastLoginDate, LastActivityDate, LastPasswordChangedDate

  • Shannon Deminick 1524 posts 5270 karma points MVP 2x
    Oct 02, 2014 @ 13:01
    Shannon Deminick
    0

    It's also worth noting that I closed the PR from @robert because the PR was using the IMemberService to check the currently logged in member. So it's true that in that case

    The MemberService is apparently not meant for this purpose

    But if you want to query member data that's in the db then you can of course use IMemberService - though you'd generally only want to do that in a controller since you don't want to be exposing back office data to the front-end.

    If you want to know the status of the current member and work with the current members' data then the MembershipHelper is what you need to use. If you are working with the data on the front-end/razor you'll probably want to work with IPublishedContent and the MembershipHelper is what you want.

    Anyways, that's all fixed, the details are at the bottom: http://issues.umbraco.org/issue/U4-5456

Please Sign in or register to post replies

Write your reply to:

Draft