OAuth Client Library for C# (.NET): Implementing Secure Authorization
In modern web development, integration with external services (Google, GitHub, Facebook, etc.) via the OAuth 2.0 protocol has become the de facto standard. For the .NET platform, there are several mature solutions, but one of the most flexible and performant is the OAuth Client for C# (.NET) library, available via the NuGet package OAuthClient.Net. This library provides developers with a complete set of tools for implementing Authorization Code Flow, Implicit Grant, and Client Credentials Flow.
Key Features of the Library
- Support for all popular OAuth 2.0 providers — Google, GitHub, Microsoft, Facebook, VK, Yandex, and custom servers.
- Automatic token management — obtaining Access Token, Refresh Token, automatic refresh upon expiration.
- Built-in CSRF attack protection — generation and verification of state parameters.
- Asynchronous operation — all methods support async/await.
- Flexible configuration — setup via code or JSON file.
- Extensibility — ability to add custom providers through interfaces.
Installation and Setup
Installation is done via NuGet Package Manager:
Install-Package OAuthClient.Net -Version 2.1.0Or via .NET CLI:
dotnet add package OAuthClient.Net --version 2.1.0After installation, you need to import the namespace:
using OAuthClient;using OAuthClient.Models;using OAuthClient.Providers;Basic Example: Authorization via Google
Let's consider the simplest scenario — obtaining user data from Google after successful authorization.
public async Task<UserInfo> AuthenticateWithGoogleAsync(){ // Provider configuration var googleProvider = new GoogleOAuthProvider( clientId: "YOUR_GOOGLE_CLIENT_ID", clientSecret: "YOUR_GOOGLE_CLIENT_SECRET", redirectUri: "https://yourapp.com/oauth/callback" );
// Creating the client var oauthClient = new OAuthClient(googleProvider);
// Generating the URL for user redirect string authUrl = oauthClient.GetAuthorizationUrl( scope: new[] { "openid", "profile", "email" }, state: Guid.NewGuid().ToString() );
// After the user returns to the callback URL: // Extract the code from the "code" query parameter string authorizationCode = HttpContext.Request.Query["code"];
// Exchange the code for a token var tokenResponse = await oauthClient.ExchangeCodeForTokenAsync(authorizationCode);
// Getting user information var userInfo = await oauthClient.GetUserInfoAsync(tokenResponse.AccessToken);
return userInfo;}Working with Refresh Token
For long-lived sessions, the library automatically manages token refresh. Example of saving and restoring a session:
// Save the Refresh Token after the first authorizationstring refreshToken = tokenResponse.RefreshToken;// Save it in a database or secure storage
// Restore session: get a new Access Tokenvar newTokenResponse = await oauthClient.RefreshAccessTokenAsync(refreshToken);
// Automatic refresh (built-in logic)oauthClient.TokenRefreshed += (sender, args) =>{ // Log or save the new Refresh Token Console.WriteLine($"New Refresh Token: {args.NewRefreshToken}");};Custom OAuth Provider
If you need to connect to a service that is not in the list of built-in providers, the library allows you to create your own:
public class CustomProvi