Karthick
Karthick

Reputation: 826

Get user claims in a class asp.vnext beta4

In the asp.net vnext, I am getting the user claims in the Controller using the following code:

 var claimsIdentity = User.Identity as ClaimsIdentity;
        var c = claimsIdentity.FindFirst(ClaimTypes.Name);
        var temp = c.Value.ToString();

but using the same, I could not get it in my class (which used to store in DB). I am getting the following error (in User) as

The name 'User' does not exist in the current context

I tried Thread.CurrentPrincipal without success. Any help?

Upvotes: 0

Views: 775

Answers (1)

Joe Audette
Joe Audette

Reputation: 36706

typically you would get access to User from the current HttpContext.

your class can declare a dependency on Microsoft.AspNet.Hosting.IHttpContextAccessor in its constructor to get one passed in by dependency injection. Then you can get it like this:

ClaimsPrincipal user = httpContextAccessor.HttpContext.User;

Upvotes: 1

Related Questions