OAuth2Bridge 1.0.2

dotnet add package OAuth2Bridge --version 1.0.2                
NuGet\Install-Package OAuth2Bridge -Version 1.0.2                
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="OAuth2Bridge" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OAuth2Bridge --version 1.0.2                
#r "nuget: OAuth2Bridge, 1.0.2"                
#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 OAuth2Bridge as a Cake Addin
#addin nuget:?package=OAuth2Bridge&version=1.0.2

// Install OAuth2Bridge as a Cake Tool
#tool nuget:?package=OAuth2Bridge&version=1.0.2                

OAuth2Bridge - Discord OAuth2 Authentication

NUGET PACKAGE

OAuth2Bridge is a simple and powerful C# library that simplifies OAuth2 authentication with Discord. This library handles user authentication via Discord’s OAuth2 flow, making it easy to authenticate users and retrieve their data.

Features

  • Discord OAuth2 Authentication: Allows your application to authenticate users using Discord’s OAuth2 service.
  • Customizable Scopes: Easily configure the required Discord OAuth2 scopes for your application (e.g., Identify, Email, etc.).
  • User Info Retrieval: Retrieve user information after authentication, including their username, email, avatar, and more.
  • Easy Integration: Simple API designed for ease of use with minimal configuration.

Example

<img src="https://github.com/Hamzaless/OAuth2Bridge/blob/master/oauth3.png?raw=true" width="600" /> <img src="https://github.com/Hamzaless/OAuth2Bridge/blob/master/oauth4.png?raw=true" width="600" />

Installation

To get started, you can install the OAuth2Bridge library via NuGet:

Install-Package OAuth2Bridge

Usage

1. Basic Example

To authenticate a user, you just need to create an instance of OAuthServer, configure your OAuth credentials, and call the AuthenticateAsync method.

using OAuth2Bridge;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Run the async Auth method
            Auth().Wait();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    private static async Task Auth()
    {
        var logger = LoggerFactory.Create(builder => builder.AddConsole())
            .CreateLogger<OAuthLogger>();

        var oAuthLogger = new OAuthLogger(logger);

        // Create the OAuth server instance
        var server = OAuthServer.CreateServer("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET", 3465, oAuthLogger, "Your App Name");

        // Add necessary Discord scopes
        server.Scopes.Add(DiscordScopes.Email);
        server.Scopes.Add(DiscordScopes.Identify);

        try
        {
            // Start the authentication process
            var userInfo = await server.AuthenticateAsync(CancellationToken.None, @".\data\success.html");
            Console.WriteLine(JsonConvert.SerializeObject(userInfo, Formatting.Indented));
        }
        catch (OAuthException ex)
        {
            Console.WriteLine($"Authentication failed: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}

2. Customizing Scopes

You can customize the OAuth2 scopes to request specific permissions from the user. Some commonly used scopes include:

  • Identify - Access the user's username and avatar.
  • Email - Access the user's email address.
  • Guilds - Get the list of guilds the user is a member of.

Here’s how you can add scopes:

server.Scopes.Add(DiscordScopes.Guilds);
server.Scopes.Add(DiscordScopes.Email);

3. Retrieving User Info

Once authenticated, you can easily retrieve the authenticated user’s information:

public class UserInfo
{
    public string Id { get; set; }
    public string Username { get; set; }
    public string Avatar { get; set; }
    public string Email { get; set; }
}

You can use JsonConvert.SerializeObject to display the user's info in a readable format:

Console.WriteLine(JsonConvert.SerializeObject(userInfo, Formatting.Indented));

Configuration

You will need to configure the following details to authenticate with Discord’s OAuth2 service:

  • ClientId: Your Discord application's client ID.
  • ClientSecret: Your Discord application's client secret.
var server = OAuthServer.CreateServer("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET", 3465, oAuthLogger, "Your App Name");

License

OAuth2Bridge is licensed under the MIT License. See the LICENSE file for details.

1.0.2 Key Updates:

  • Removed references to timeout handling from the "Features" section.
  • Adjusted the examples and explanations to align with the removal of timeout functionality.
  • Simplified the AuthenticateAsync usage, as it now waits indefinitely for the callback.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.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.

Version Downloads Last updated
1.0.2 57 1/6/2025
1.0.1 52 1/6/2025
1.0.0 95 1/4/2025 1.0.0 is deprecated because it has critical bugs.

Fixed README & Fixed Timeout issues.