Access current UserId and TenantId with OData controller


Access current UserId and TenantId with OData controller



I've added Odata to my asp.net core boilerplate project as per the documentation:https://aspnetboilerplate.com/Pages/Documents/OData-AspNetCore-Integration



How do I access the Current TenantId and UserId within the AbpODataEntityController?




1 Answer
1



You can check this sample code.


public class MyODataController : Abp.AspNetCore.OData.Controllers.AbpODataEntityController<User, long>, ITransientDependency
{
private readonly UserManager _userManager;

public MyODataController(IRepository<User, long> repository, UserManager userManager) : base(repository)
{
_userManager = userManager;
}

public long? GetCurrentUserId()
{
return _userManager.AbpSession.UserId;
}

public int? GetCurrentTenantId()
{
return _userManager.AbpSession.TenantId;
}
}



PS: don't forget to inherit from ITransientDependency so that it registers to dependency service.





Thanks, I've done as you suggested, but _userManager.AbpSession.UserId & _userManager.AbpSession.TenantId are both returning null. I've noticed I can make odata calls via the browser without any token authentication, is there something I need to do to make odata calls authenticate with a valid user?
– DLedge
Jul 3 at 3:16






@DLedge Add [AbpMvcAuthorize] attribute to your controller.
– aaron
yesterday


[AbpMvcAuthorize]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height