Copied to clipboard

Flag this post as spam?

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


  • J 446 posts 863 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 1870 posts 6395 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 446 posts 863 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 1870 posts 6395 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 7100 karma points c-trib
    Nov 15, 2023 @ 22:05
    Nicholas Westby
    0

    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 1870 posts 6395 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 7100 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")).

Please Sign in or register to post replies

Write your reply to:

Draft