Authenticating API Gateway with Cognito in .NET Apps (Jun)

Authenticating API Gateway with Cognito in .NET Apps (Jun)

06 ago
|
Renato Gonçalves
|
Jun

06 ago

Renato Gonçalves

Jun

When building secure and scalable APIs on AWS, Amazon Cognito paired with API Gateway provides a powerful combination for authentication. Cognito handles the identity layer, while API Gateway manages the request routing and enforcement of authentication via JWT tokens. In this post, we’ll walk through how to authenticate .NET applications against an API Gateway endpoint secured by Cognito. This setup works great for web apps, desktop clients, and server-to-server communication.

Why Use

Cognito with API Gateway?

Amazon Cognito

Is a User Directory Service That Provides Secure user sign-up and sign-in

JWT token-based authentication

Integration with identity providers like Google, Facebook, and Microsoft

Federated access to AWS services API Gateway can directly validate JWT tokens issued by Cognito using a User Pool Authorizer. This allows you to secure your APIs without writing custom authentication logic. Step 1: Set Up Cognito Create a User Pool

Go to the Cognito console.

Choose “Create user pool” and configure fields like username, email, and password policies.

Create an App Client

Disable the client secret (for public clients like desktop or mobile apps).

Enable the ALLOW_USER_PASSWORD_AUTH and ALLOW_REFRESH_TOKEN_AUTH flows if you want to authenticate with username/password directly.

(Optional) Hosted UI

Use Cognito's hosted UI if you want OAuth2-style login redirects.

Step 2: Create and Configure API Gateway Create a REST API or HTTP API In the API Gateway console, choose to create a new HTTP API.

Name it something like UserServiceAPI.

Define a Secure Endpoint

Add a new route: GET /profile

This endpoint will return user profile data and should only be accessible to authenticated users.

Add a Cognito Authorizer

Go to the Authorizers section.

Choose "Cognito" and select the user pool you created earlier.





Give the authorizer a name like CognitoUserPoolAuthorizer.

Secure the /profile Route In the route settings, attach the Cognito authorizer to the /profile endpoint.

This ensures that any request to /profile must include a valid JWT token issued by your Cognito User Pool.

Step 3: Get a JWT Token from Cognito in .NET You can authenticate a user and retrieve tokens via HTTP using Cognito’s /oauth2/token endpoint. Here’s how to do that using HttpClient in a .NET app: var httpClient = new HttpClient(); var parameters = new Dictionary { { "grant_type", "password" }, { "client_id", "" }, { "username", "" }, { "password", "" }, }; var content = new FormUrlEncodedContent(parameters); var response = await httpClient.PostAsync("https://.auth..amazoncognito.com/oauth2/token", content); var json = await response.Content.ReadAsStringAsync(); var tokenResponse = JsonSerializer.Deserialize(json); public class TokenResponse { public string Access_token { get; set; } public string Id_token { get; set; } public string Refresh_token { get; set; } public string Token_type { get; set; } public int Expires_in { get; set; } } Tip: You can also use MSAL.NET if you’re using the OAuth2 code flow with redirect URIs and Cognito’s hosted UI. Step 4: Send Authenticated Requests to API Gateway After obtaining the token from Cognito, use it to call the protected /profile endpoint: var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",



tokenResponse.Id_token); var response = await client.GetAsync("https://your-api-id.execute-api.us-east-1.amazonaws.com/prod/profile"); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); Console.WriteLine(result); } else { Console.WriteLine($"Request failed: {response.StatusCode}"); } Replace your-api-id with your presente API Gateway ID and region.

This example assumes the endpoint is deployed under the prod stage. Step 5: (Optional) Validate Tokens Manually If you’re processing the token yourself (e.g., in a Lambda function), use System.IdentityModel.Tokens.Jwt to parse and validate the token: var handler = new JwtSecurityTokenHandler(); var token = handler.ReadJwtToken(tokenResponse.Id_token); foreach (var claim in token.Claims) { Console.WriteLine($"{claim.Type}: {claim.Value}"); } Make Sure To Validate Signature (using the JWKS endpoint)

Expiration (exp)

Audience (aud)

Issuer (iss) Common Issues Missing Token: Ensure the Authorization header is set and the route is protected with the authorizer.

Invalid Token: Check that you're using the correct Cognito user pool and app client.

CORS Errors: If calling from a browser, configure CORS settings in API Gateway.

Token Expired: Use the refresh token to obtain a new access token.

Conclusion

By combining Amazon Cognito and API Gateway, you can add strong authentication to your APIs without maintaining your own user system. .NET apps can easily acquire and use tokens to call these APIs securely. Whether you're building a mobile app, a web frontend, or a backend service, this setup is clean, scalable, and secure.

References Amazon Cognito User

Pools

API Gateway Cognito Authorizer

Cognito OAuth2 Token Endpoint

System.IdentityModel.Tokens.Jwt

📌 Authenticating API Gateway with Cognito in .NET Apps (Jun)
🏢 Renato Gonçalves
📍 Jun

Postulate a este anuncio

Muestra tus habilidades a la empresa, rellenar el formulario y deja un toque personal en la carta, ayudará el reclutador en la elección del candidato.

Suscribete a esta alerta:

Recibe por email las nuevas ofertas de trabajo para: authenticating api gateway with cognito in .net apps (jun) / jun

Suscribete a esta alerta:

Recibe por email las nuevas ofertas de trabajo para: authenticating api gateway with cognito in .net apps (jun) / jun