Base solution for your next web application
Open Closed

Best practice to get user-information on all pages. #285


User avatar
0
samvh created

Hello,

I have a 'what would be best'-question, I hope you can help me with it.

I'm using a bootstrap topbar and a dropdown div to display the current logged-in user information. This bar and user information should be available on all pages.

What would be the best way to retrieve this information. I'm thinking about 3 solutions:

  1. Use the ISessionAppService and send GetCurrentLoginInformationsOutput to the shared _Layout view.
  2. Use the UserManager to obtain the User and store the needed values in a ViewModel that is used in the _Layout.
  3. Use the User.Identity in _Layout and extend it with the necessary fields.

I assume that 1. and 2. would be done in the ...ControllerBase so that all the Controllers can use the functionality.

Thanks !


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    I seperated header to a partial view and calling

    @Html.Action("Header", "Layout")
    

    from _Layout.cshtml. And this is the Header action:

    [ChildActionOnly]
            public PartialViewResult Header()
            {
                var headerModel = new HeaderViewModel
                {
                    LoginInformations = AsyncHelper.RunSync(() => _sessionAppService.GetCurrentLoginInformations()),
                    Languages = LocalizationManager.GetAllLanguages(),
                    CurrentLanguage = LocalizationManager.CurrentLanguage,
                    IsMultiTenancyEnabled = _multiTenancyConfig.IsEnabled
                };
    
                return PartialView("_Header", headerModel);
            }
    

    Hope it helps :)

  • User Avatar
    0
    samvh created

    Helped me very much :) Thanks!