2

Ok, so I'm working on converting a webApi from aspnet to aspnetcore.

I have the following inside of an HTTP post.

var name = ControllerContext.RequestContext.Principal.Identity.Name;
widget.CreatedBy = name;
//Other properties
DataContext.Widgets.Add(entity);

The problem I'm running into is that RequestContext doesn't seem to exist in aspnetcore. I've been searching for the better part of a day for an equivalence.

1 Answer 1

4

In ASP.NET Core, from within a controller this is done like:

var claimsPrincipal = User;

Reference: Migrate from ClaimsPrincipal.Current

Sign up to request clarification or add additional context in comments.

4 Comments

So far Im getting null for name this way, but i think its the right track.
For the User.Name property to be set, ClaimType used for the user name claim must match what your auth is setting it to: learn.microsoft.com/en-us/dotnet/api/…
I suppose this answer is correct. I can plainly see the info i want on user.claims[9]. But im not having any luck updating the claimtype for name. Ive tried numerous sites and sources and just gone down rabbit holes with no results.I suppose i'll just do a query on claims for "schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" but that seems ugly.
@Seth You can just do var name = User.FindFirstValue(ClaimTypes.Name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.