Microsoft Dev Blogs

Using PostgreSQL with .NET and Entra ID

thumbnail

Table of Contents

  1. Getting started with PostgreSQL
  2. Adding PostgreSQL to the API
  3. Managing access tokens with Entra ID
  4. Next Steps

Getting started with PostgreSQL

  • Create a new application using PostgreSQL as the database.
  • Add the PostgreSQL hosting component to the AppHost project.
  • Create a PostgreSQL resource and add it as a reference to the API project.

Adding PostgreSQL to the API

  • Add the Npgsql package to the API project.
  • Use the .NET Aspire package for logging and telemetry.
  • Interact with the PostgreSQL database in the API project.

Managing access tokens with Entra ID

  • Request an access token to act in place of a persistent password.
  • Use the Microsoft.Authentication.TokenCache package to request access tokens.
  • Configure the NpgsqlConnectionStringBuilder to request new access tokens when needed.

Next Steps

Explore more advanced features of PostgreSQL in .NET and learn how to secure your application with Entra ID for Managed Identity.