0

I'm writing an aspnet core 2.0 application. Using a bearer token authentication I have the User property inside the controller with the correct identity. How can I access the User property or the ClaimPrincipal in the separated .net standard project for business logic?

ASP.NET Core project:

//ASP.NET Core project
public class DocumentController : Controller
{
    public IActionResult Index()
    {
        System.Security.Claims.ClaimsPrincipal cp = User;

        /* check for claims in ClaimsPrincipal */

        DocumentBL docBL = new DocumentBL();
        docBL.BLmethodForDocument();

        return View();
    }
}

Separated .net standard project: BL.dll

My ASP.NET Core project depends on BL.dll

//Separated project: BL.dll
public class DocumentBL
{
    public void BLmethodForDocument()
    {
        System.Security.Claims.ClaimsPrincipal cp = ???????????

        /* check for claims in ClaimsPrincipal */

        //rest of business logic...
    }
}

How can I fix the BLmethodForDocument to work with current ClaimsPrincipal WITHOUT passing it to the bl method as parameter (and WITHOUT passing it to the DocumentBL constructor)?

Can not use System.Security.Claims.ClaimsPrincipal.Current nor System.Threading.Thread.CurrentPrincipal they are always null.

Cyrus
  • 2,261
  • 2
  • 22
  • 37
  • HttpContext.Current.Request.User – Chetan Feb 13 '18 at 01:17
  • 1
    I would rather suggest passing the User as a parameter to the Method in BL project. – Jaliya Udagedara Feb 13 '18 at 01:31
  • Possible duplicate of https://stackoverflow.com/questions/38291374/getting-the-claimsprincipal-in-a-logic-layer-in-an-aspnet-core-1-application – Kalyan Feb 13 '18 at 03:30
  • 1
    Possible duplicate of [Getting the ClaimsPrincipal in a logic layer in an aspnet core 1 application](https://stackoverflow.com/questions/38291374/getting-the-claimsprincipal-in-a-logic-layer-in-an-aspnet-core-1-application) – Kalyan Feb 13 '18 at 03:57
  • @Kalyan it is not the same, because when I set `services.AddTransient(s => s.GetService().HttpContext.User);` to the Startup.cs in my ASP.NET Core project, I still can not access the current ClaimPrincipal in separated business logic project. – Cyrus Feb 13 '18 at 07:47
  • @ChetanRanpariya `HttpContext.Current.Request.User` does not exists. I think you means `HttpContext.Current.User` but in `Microsoft.AspNetCore.Http.HttpContext.Current.User` from `Microsoft.AspNetCore.Http.Abstractions` assembly does not exists the `public static HttpContext Current { get; set; }` property like in .NET v4.7 and earlier .NET Framewrok versions... – Cyrus Feb 13 '18 at 08:02
  • 2
    You need to embrace dependency injection. In your business layer too. Make dependencies explicit in your business layer, and you no longer need to ask yourself how to get some dependency within a method. – poke Feb 13 '18 at 08:32
  • 1
    @poke thank you, you helped me. The solution is add my `DocumentBL` to Startup.cs `ConfigureServices`: `services.AddTransient();` and `services.AddSingleton();` and `services.AddTransient(s => s.GetService().HttpContext.User);` – Cyrus Feb 13 '18 at 09:28

0 Answers0