Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the ClaimsPrincipal in a logic layer in an aspnet core 1 application

Tags:

.net

asp.net

I'm writing an aspnet core 1 application. Using a bearer token authentication I have the User property inside the controller with the correct identity. However I cant seem to find a way to grab with identity as I did before using the ClaimPrincipal.Current static. What is the current best practice to get this data inside the BL layers with out passing the ClaimPrincipal object around?

like image 550
Eugene Krapivin Avatar asked Jul 10 '16 11:07

Eugene Krapivin


1 Answers

Further investigating this issue I've found that it is possible to use the native DI container to inject the ClaimsPrincipal where needed like that:

services.AddTransient<ClaimsPrincipal>(s =>     s.GetService<IHttpContextAccessor>().HttpContext.User); 

This feels kind of weird injecting it, however it is better than storing it in the CallContext.

like image 71
Eugene Krapivin Avatar answered Sep 20 '22 06:09

Eugene Krapivin