# How do I get UserId from Keycloak using ASP.NET Web Api?

**URL:** <https://forum.keycloak.org/t/how-do-i-get-userid-from-keycloak-using-asp-net-web-api/31360>\
**Category:** Getting advice\
**Tags:** authentication\
**Created:** [May 9, 2026, 10:27am UTC](https://forum.keycloak.org/t/how-do-i-get-userid-from-keycloak-using-asp-net-web-api/31360 "2026-05-09T10:27:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![newUsr](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@newUsr](https://forum.keycloak.org/u/newUsr)\
**Post date:** [May 9, 2026, 10:27am UTC](https://forum.keycloak.org/t/how-do-i-get-userid-from-keycloak-using-asp-net-web-api/31360/1 "2026-05-09T10:27:17Z")

</div>

Currently I’m working on pet project.  
I’ve already done configuring to Angular + [ASP.NET](http://ASP.NET) Project. But I tried search for information on the internet, nothing but configuring your project to your realm and client-scopes, etc.  
How to do I get userId like service, repo or api, to use it in controller to store and get some user wishlist, or get orders for example?

---

<div class="post-metadata">

**Author:** ![mbonn](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/mbonn/32/5755_2.png) [@mbonn](https://forum.keycloak.org/u/mbonn)\
**Post date:** [May 12, 2026, 9:04am UTC](https://forum.keycloak.org/t/how-do-i-get-userid-from-keycloak-using-asp-net-web-api/31360/2 "2026-05-12T09:04:44Z")

</div>

To do an authentication using JWT bearer Tokens in [ASP.NET](http://ASP.NET) Web API, you need the Microsoft.AspNetCore.Authentication.JwtBearer package:

> **[Microsoft.AspNetCore.Authentication.JwtBearer 10.0.7](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer/)**
>
> ASP.NET Core middleware that enables an application to receive an OpenID Connect bearer token.
> 
> This package was built from the source code at https://github.com/dotnet/dotnet/tree/b16286c2284fecf303dbc12a0bb152476d662e44

Annotate the controller class with `[Authorize]` attribute to secure all endpoints within the controller, e.g.

`[Route("api/[controller]")]`  
`[ApiController]`  
`[Authorize]`  
`public class MySuperDuperController : ControllerBase`  
`{`  
` //`  
`}`

> **[Securing .NET Microservices and Web Applications - .NET](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/secure-net-microservices-web-applications/)**
>
> Security in .NET Microservices and Web Applications - Get to know the authentication options in ASP.NET Core web applications.

> **[About authorization in .NET microservices and web applications - .NET](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/secure-net-microservices-web-applications/authorization-net-microservices-web-applications)**
>
> Security in .NET Microservices and Web Applications - Get an overview of the main authorization options in ASP.NET Core applications - role-based and policy-based.

Within your endpoint implementation methods, you can access a ClaimsPrincipal object using `this.User`This ClaimsPricipal object holds the claims released by Keycloak.

`[HttpGet("")]`  
`public async Task<ActionResult<IEnumerable<Object>>> GetSomething()`  
`{`  
` this.User.Claims`…  
`}`

> **[ClaimsPrincipal Class (System.Security.Claims)](https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsprincipal?view=netframework-4.8.1&devlangs=csharp&f1url=%3FappId%3DDev17IDEF1%26l%3DEN-US%26k%3Dk%28System.Security.Claims.ClaimsPrincipal%29%3Bk%28DevLang-csharp%29%26rd%3Dtrue)**
>
> An IPrincipal implementation that supports multiple claims-based identities.
