Base solution for your next web application
Open Closed

How to get current authenticated user's information? #1021


User avatar
0
moustafa created

Hello i'm trying to get current authenticated user's information using this code

var r = new SessionAppService().GetCurrentLoginInformations(); string userName = r.Result.User.UserName;

but it's not work as r.Result always null

is that the right piece of code to accomplish this task?


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

    Use dependency injection to inject ISessionAppService and use it. Do not create SessionAppService like new SessionAppService() since it breaks dependency injection.

  • User Avatar
    0
    moustafa created

    hi , i appreciate your concern .. but as you know i have problem with mastering the structure of the dependency injection , so i wondering of you have time to provide me with code that help in my case

    thank you

  • User Avatar
    0
    eggersa created

    Do you try to get the user infomation within a mvc controller or a service? Because both, the AbpServiceBase and the AbpController base class provide you with a property named CurrentSession that gives you access to the current user.

  • User Avatar
    0
    moustafa created

    actually the both

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Actually, there is exactly same code in AspNet Zero. Check this: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Controllers/LayoutController.cs">https://github.com/aspnetzero/aspnet-ze ... troller.cs</a>

    As you see we injected session app service (<a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Controllers/LayoutController.cs#L22">https://github.com/aspnetzero/aspnet-ze ... ler.cs#L22</a>) and used it to get session info (<a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Controllers/LayoutController.cs#L36">https://github.com/aspnetzero/aspnet-ze ... ler.cs#L36</a>). Note: Here, you don't have to use AsyncHelper if your action is already async.

  • User Avatar
    0
    moustafa created

    thank you