BurakKontas.OAuth2 1.0.1

Suggested Alternatives

BurakKontas.OAuth2.Google

Additional Details

This package seperated into OAuth Providers.

dotnet add package BurakKontas.OAuth2 --version 1.0.1                
NuGet\Install-Package BurakKontas.OAuth2 -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BurakKontas.OAuth2" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BurakKontas.OAuth2 --version 1.0.1                
#r "nuget: BurakKontas.OAuth2, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BurakKontas.OAuth2 as a Cake Addin
#addin nuget:?package=BurakKontas.OAuth2&version=1.0.1

// Install BurakKontas.OAuth2 as a Cake Tool
#tool nuget:?package=BurakKontas.OAuth2&version=1.0.1                

OAuth 2.0 Client

This project provides a C# client for interacting with OAuth 2.0 services. Below you'll find information on how to use and configure.

Features

  • Google OAuth 2.0 login and access token retrieval
  • Token renewal support
  • OAuth 2.0 login URL generation

Requirements

  • .NET 6.0 or newer
  • FluentValidation
  • Xunit
  • Moq
  • FluentAssertions

Installation

NuGet Packages

The project includes the necessary NuGet packages. Ensure the following packages are installed:

  • FluentValidation
  • Xunit
  • Moq
  • FluentAssertions

Configuration

To use the Google OAuth 2.0 client, you need to create a GoogleOAuthData object containing the required configuration details. Here is an example configuration:

var googleOAuthData = new GoogleOAuthData
{
    GoogleUrl = new Uri("https://accounts.google.com/o/oauth2/v2/auth?"),
    GoogleTokenUrl = new Uri("https://oauth2.googleapis.com/token"),
    ClientId = "test-client-id",
    ClientSecret = "test-client-secret",
    RedirectUri = new Uri("https://example.com/callback"),
    AccessType = GoogleAccessType.Online,
    Prompt = GooglePrompt.Consent,
    Scope = new List<GoogleScopes> { GoogleScopes.OpenId, GoogleScopes.Email },
    ResponseType = GoogleResponseType.Code,
    State = Guid.NewGuid().ToString(),
    GrantType = GoogleGrantType.AuthorizationCode
};

Usage

Creating the Client

You can create an instance of GoogleClient using the GoogleOAuthData object and an HttpClient:

var httpClient = new HttpClient();
var googleClient = new GoogleClient(googleOAuthData, httpClient);

Logging In

To log in using an authorization code, use the LoginAsync method of any client:

const string code = "test-auth-code";
var loginResponse = await googleClient.LoginAsync(code);
Console.WriteLine($"Access Token: {loginResponse?.AccessToken}");

Token Renewal

To get a new access token using a refresh token, use the RefreshTokenAsync method of any client:

const string refreshToken = "test-refresh-token";
var response = await googleClient.RefreshTokenAsync(refreshToken);

Console.WriteLine($"Access Token: {response?.AccessToken}");

Generating Login URL

To generate the URL needed to start the OAuth 2.0 login process, use the GetLoginUrl method of any client:

var url = await googleClient.GetLoginUrl();

Console.WriteLine($"Login URL: {url}");

Contributors

BurakKontas - Project Owner and Lead Developer

License

This project is licensed under the MIT License.

TODO

  • Implemented
    • Google OAuth2 Implementation
  • Roadmap
    • Facebook OAuth2 Implementation
    • Apple OAuth2 Implementation
    • X OAuth2 Implementation
    • Github OAuth2 Implementation
    • Discord OAuth2 Implementation
    • Linkedin OAuth2 Implementation
    • Steam OAuth2 Implementation
    • Twitch OAuth2 Implementation
    • Epic Games OAuth2 Implementation
    • Sony Playstation OAuth2 Implementation
    • Xbox OAuth2 Implementation
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.