Refactor and rename project to yawaflua.Discord.Net; add core entities and interfaces for Discord OAuth2 integration

This commit is contained in:
Dmitri Shimanski
2025-08-22 06:48:43 +03:00
parent 423bc8def0
commit e0d2b65fff
37 changed files with 867 additions and 382 deletions

View File

@@ -1,41 +1,64 @@
# x3rt.DiscordOAuth2
# yawaflua.Discord.Net
[![NuGet](https://img.shields.io/nuget/v/x3rt.DiscordOAuth2.svg)](https://www.nuget.org/packages/x3rt.DiscordOAuth2/)
[![NuGet](https://img.shields.io/nuget/dt/x3rt.DiscordOAuth2.svg)](https://www.nuget.org/packages/x3rt.DiscordOAuth2/)
[![NuGet](https://img.shields.io/nuget/v/yawaflua.Discord.Net.svg)](https://www.nuget.org/packages/yawaflua.Discord.Net/)
[![NuGet](https://img.shields.io/nuget/dt/yawaflua.Discord.Net.svg)](https://www.nuget.org/packages/yawaflua.Discord.Net/)
A **simple** library to handle Discord OAuth2 authentication.
Meant to serve as an alternative to
the [AspNet.Security.OAuth.Providers](https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers) library that
gives more control to the user and one that can be used by people that are trying to avoid ASP.NETs cluttered and bulky Identity system.
yawaflua.Discord.Net is a library for integrating Discord OAuth2 into your .NET applications. It provides a simple and easy-to-use interface for authenticating users with Discord, retrieving user information, and managing sessions.
## Features
- **OAuth2 Authentication**: Easily authenticate users with Discord using OAuth2.
- **User Information**: Retrieve user information such as username, avatar, and guilds.
- **Session Management**: Create and manage sessions for authenticated users.
- **Dependency Injection**: Seamlessly integrate with your .NET application's dependency injection system.
## Usage
```csharp
// Configure OAuth ClientID and ClientSecret globally
DiscordOAuth.Configure(0123456789, "ClientSecret", "OptionalBotToken");
```
```csharp
var scopes = new ScopesBuilder(OAuthScope.Identify);
var oAuth = new DiscordOAuth("https://example.com/Login", scopes);
var url = oAuth.GetAuthorizationUrl("state");
/* Redirect user to url via preferred method */
```
```csharp
// Your callback method
if (DiscordOAuth.TryGetCode(HttpContext, out var code))
// Configure configuration to DI
builder.Services.AddSingleton<DiscordConfig>(
new DiscordConfig
{
ClientId = "your-client-id",
ClientSecret = "shhhhh!",
RedirectUri = "https://example.com/Login",
Scopes = new ScopesBuilder(OAuthScope.Identify, OAuthScope.Guilds),
Token = "MyVeryCool.BotToken.For.Discord" // Optional, for bot interactions
});
builder.Services.AddSingleton<IDiscord, DiscordOAuth>();
// And use it in you very cool controller or smth
public class MyController (IDiscord discord) : ControllerBase
{
var scopes = new ScopesBuilder(OAuthScope.Identify);
var oAuth = new DiscordOAuth("https://example.com/Login", scopes);
var token = await oAuth.GetTokenAsync(code);
var user = await oAuth.GetUserAsync(token);
ulong userId = user.Id;
// ...
public async Task<IActionResult> Login()
{
var url = discord.GetAuthorizationUrl("state");
return Redirect(url);
}
public async Task<IActionResult> Callback()
{
if (discord.TryGetCode(HttpContext, out var code))
{
var token = await discord.GetTokenAsync(code);
var user = await discord.GetUserAsync(token);
var session = discord.CreateSession();
var guilds = await session.GetGuildsAsync();
// Do something with the user and guilds, like storing them in a database or session
// ...
return Ok();
}
return BadRequest("Invalid code");
}
}
```
## Feedback
DiscordOAuth2 is a work in progress and any feedback or suggestions are welcome.
This library is released under the [MIT License](LICENSE).
`yawaflua.Discord.Net` is a library still in development, and your feedback or support is very welcome. If you have any issues, suggestions, or questions, please open an issue on the [GitHub repository](https://github.com/yawaflua/Discord.Net/)
This library is released under the [Apache 2.0](LICENSE).