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)
-
0
Use dependency injection to inject ISessionAppService and use it. Do not create SessionAppService like new SessionAppService() since it breaks dependency injection.
-
0
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
-
0
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.
-
0
actually the both
-
0
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.
-
0
thank you