Copied to clipboard

Flag this post as spam?

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


  • J 447 posts 864 karma points
    Apr 04, 2023 @ 10:03
    J
    0

    How to get logged in member details?

    I have created a member using the admin office and see it listed under members.

    How could i do the following (in Razor)

    1. Check if the user is logged in or not?
    2. Display their name instead of email?
    3. Configure members session to have a max time of 30 mins before auto logout?

    Thanks

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Apr 04, 2023 @ 10:46
    Huw Reddick
    0

    Check if the user is logged in or not?

    Where do you want to do this? In a view or in a controller?

    Display their name instead of email?

    Use the IMemberService to get the Member you should then be able to get the Name property

    Configure members session to have a max time of 30 mins before auto logout? You need to add this to the umbraco startup.cs

    services.ConfigureApplicationCookie(options => { options.ExpireTimeSpan = TimeSpan.FromMinutes(30); });
    
  • J 447 posts 864 karma points
    Apr 04, 2023 @ 10:58
    J
    0
    1. Where do you want to do this? In a view or in a controller?

    In a View/Razor page.

    1. Use the IMemberService to get the Member you should then be able to get the Name property

    Could i do this in the View as well?

    1. services.ConfigureApplicationCookie(options => { options.ExpireTimeSpan = TimeSpan.FromMinutes(30); });

    Ok, is there no way to do this from the config file in case i need to change it in future? (I suppose i could add a variable to the config file to read from) but just in case there is this option?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Apr 04, 2023 @ 12:33
    Huw Reddick
    102

    You should be able to do the following to get the currently logged in users name in a view

    @inject IMemberManager membermanager
    
    @{
        var currUser = membermanager.GetCurrentMemberAsync().Result;
        if (currUser != null)
        {
            var usersname = currUser.Name;
        }
    }
    

    is there no way to do this from the config file

    Not that I am aware of

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Nov 15, 2023 @ 22:05
    Nicholas Westby
    1

    No idea if this is the right way of doing it, but here's something else you can do to access additional member properties:

    @inject IMemberManager MemberManager
    @inject IMemberService MemberService
    
    @{
        var currUser = MemberManager.GetCurrentMemberAsync().GetAwaiter().GetResult();
        if (currUser != null)
        {
            var usersname = currUser.Name;
            var member = MemberService.GetById(int.Parse(currUser.Id));
            var someValue = member.GetValue<string>("someProperty");
        }
    }
    

    It seems silly to me that there would need to be two services involved in accessing the details of the currently logged in member, so somebody more informed can feel free to correct this.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Nov 16, 2023 @ 06:56
    Huw Reddick
    1

    You can just use the membermanager

    member = MemberManager.AsPublishedMember(currUser)

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Nov 22, 2023 @ 04:20
    Nicholas Westby
    0

    Interesting, that does seem to work. Seems a bit like the difference between a published content and a non-published content entity.

    That is, MemberManager.AsPublishedMember returns a "published" member that you can access data with (including in a typed way if you cast the IPublishedContent to a models-builder generated class for that member type, such as Member).

    And MemberService.GetById returns a non-published member that you can set data on (e.g., memberInstance.SetValue("firstName", "SomeFirstName")).

  • Luke Alderton 192 posts 509 karma points
    Aug 08, 2024 @ 13:19
    Luke Alderton
    0

    Very aware of how long it's been since last reply but found this Implementation to be quite clean:

    Int32? intMemberId = Context.User?.Identity?.GetUserId<Int32>();
    IMember? objMember = intMemberId != null && intMemberId > 0 ? 
    MemberService.GetById((Int32)intMemberId) : null;
    
    if (objMember != null)
    {
        ViewData["pageTitle"] = "Welcome " + objMember.Name;
    }
    
  • Garðar Þorsteinsson 118 posts 564 karma points
    Aug 08, 2024 @ 13:30
    Garðar Þorsteinsson
    1

    Hi Luke,

    Be very aware on how you use the MemberService.

    It will use calls straight to the DB and is very slow compared to IMemberManager.

    Its recommanded to never use IContentService or IMemberService in views or anywhere the client can call it.

    There are multiple ways of doing this but here is one where we use IMemberManager to check if there is a member loggedin and then we fetch the member. Then we can also use IMemberManager to fetch the IPublishedContent version of the member to access the umbraco content.

    if (_memberManager.IsLoggedIn())
    {
        var currentMember = await _memberManager.GetCurrentMemberAsync();
    
        if (currentMember != null)
        {
            var publishedMember = _memberManager.AsPublishedMember(currentMember);
    
            var accessUmbracoProperty = publishedMember.Value<string>("someAlias");
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft