I need information about the logged in user in the EntityFramework layer. I also need this information in the IPermissionChecker implementation.
I have read this page: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Abp-Session">http://www.aspnetboilerplate.com/Pages/ ... bp-Session</a>
And I have implemented the IAbpSession interface like so:
class AbpSession : IAbpSession
{
public long? UserId
{
get
{
//var userId = Thread.CurrentPrincipal.Identity.GetUserId();
return null;
}
}
public int? TenantId { get; private set; }
}
The Thread.CurrentPrincipal.Identity.GetUserId() line (commented out above) always returns null. How can I get identity of the currently logged in user in the implementation of IAbpSession?
I am authenticating using FormsAuthentication.SetAuthCookie. This works, because the user can access something marked with [Authorise].
5 Answer(s)
-
0
Hi,
Did you try to get UserId from cookie? You can write it to the cookie when user logged in and then get it from the cookie. You can also use ASP.NET's Session to store/retrieve UserId. In these cases, you should implement AbpSession in web layer and use HttpContext.Current...
-
0
Thank you for your reply.
So internally, whenever ABP wants to find out who is logged in (to show the correct menu items, or to send the id to the PermissionChecker etc), it will always use the UserID from the IAbpSession implemtation, which it will grab from the IoC?
-
0
Yes, ABP uses IAbpSession to understand if a user is logged in (if IAbpSession.UserId is not null) and to get current UserId and TenantId.
If you don't implement AbpSession, then it uses NullAbpSession (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/Runtime/Session/NullAbpSession.cs">https://github.com/aspnetboilerplate/as ... Session.cs</a>) via NullAbpSession.Instance (see property injection: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Dependency-Injection#DocPropertyInjection">http://www.aspnetboilerplate.com/Pages/ ... yInjection</a>).
If you implement IAbpSession (module-zero does that) and register it to DI then it uses yours. How to do it? Simple:
Create your class:
public class MyAbpSession : IAbpSession, ITransientDependency
It can be singleton if needed. That's it. It will be automatically registered to DI and ABP will use it.
-
0
How to get user details on CSHTML page?
-
0
Hi,
This is not possible by default but you can add a property to your viewbase here <a class="postlink" href="https://github.com/aspnetboilerplate/module-zero-template/blob/master/src/AbpCompanyName.AbpProjectName.WebMpa/Views/AbpProjectNameWebViewPageBase.cs">https://github.com/aspnetboilerplate/mo ... ageBase.cs</a>.
You can inject UserManager in your ViewPageBase and set current user's data in it's constructor.