OAuth 2.0 is an industry-standard authorization framework that enables secure delegated access to protected resources without exposing user credentials. In ASP.NET Core applications, OAuth 2.0 is commonly used for social login (Google, Microsoft, Facebook), third-party API integration, and securing enterprise-grade Web APIs. Implementing OAuth 2.0 correctly ensures scalable, secure authentication and authorization in distributed systems and cloud-native architectures.
This article explains how to implement OAuth 2.0 in ASP.NET Core, configure external login providers, secure APIs using access tokens, and apply production-ready security practices.
Understanding OAuth 2.0 Flow in ASP.NET Core
OAuth 2.0 involves the following components:
Resource Owner (User)
Client Application (ASP.NET Core app)
Authorization Server (OAuth provider such as Google or Microsoft)
Resource Server (Protected API)
The most common flow used in web applications is the Authorization Code Flow. The client application redirects the user to the OAuth provider for authentication. After successful login, the provider sends an authorization code, which is exchanged for an access token.
Step 1: Register Application with OAuth Provider
Before implementation, register your ASP.NET Core application with the OAuth provider (for example, Google Cloud Console or Microsoft Entra ID). After registration, you receive:
Client ID
Client Secret
Redirect URI
These credentials are required to configure OAuth in ASP.NET Core.
Step 2: Install Required Packages
Install authentication packages via NuGet:
Microsoft.AspNetCore.Authentication.Google
Microsoft.AspNetCore.Authentication.JwtBearer (for API protection)
Step 3: Configure OAuth Authentication in Program.cs
Example using Google OAuth:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
});
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Step 4: Add Configuration in appsettings.json
"Authentication": {
"Google": {
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret"
}
}
Store secrets securely using environment variables or secret managers in production environments.
Step 5: Protect Controllers with Authorize Attribute
[Authorize]
[HttpGet("profile")]
public IActionResult GetProfile()
{
return Ok(User.Identity.Name);
}
When accessing this endpoint, unauthenticated users are redirected to the OAuth provider.
Securing APIs with OAuth 2.0 Access Tokens
For securing ASP.NET Core Web APIs, configure JWT Bearer authentication:
Join the conversation! Your thoughts help the community grow.