Asp net core 2 discord authentication oauth


ASP.NET is a popular programming language used for building web applications. It provides a for developing dynamic websites, web services, and web applications. In this article, we will explore how to implement Discord authentication using OAuth in ASP.NET Core 2.

What is OAuth?

OAuth is an open standard for authorization that allows users to grant access to their resources on one to another website without sharing their credentials. It is widely used by social media platforms and other web services to users.

Setting up Discord OAuth

To implement Discord authentication in ASP.NET Core 2, we need to set up an OAuth application in the Discord Developer . Follow these steps:

  1. Create a new application in the Discord Developer Portal.
  2. Generate a client ID and client secret for your application.
  3. Configure the redirect URL for your application.

Once you have set up the OAuth application in Discord, you can proceed with the ASP.NET Core implementation.

Implementing Discord OAuth in ASP.NET Core 2

To implement Discord authentication in ASP.NET Core 2, we need to install the required NuGet and configure the authentication middleware.

// Add the required NuGet packages
dotnet add package Microsoft.AspNetCore.Authentication
dotnet add package Microsoft.AspNetCore.Authentication.OAuth

// Configure the authentication middleware in Startup.cs
public void ConfigureServices(IServiceCollection services)
    services.AddAuthentication(options =>
        options.DefaultAuthenticateScheme = "Discord";
        options.DefaultChallengeScheme = "Discord";
    .AddOAuth("Discord", options =>
        options.ClientId = "YOUR_CLIENT_ID";
        options.ClientSecret = "YOUR_CLIENT_SECRET";
        options.CallbackPath = new PathString("/signin-discord");
        options.AuthorizationEndpoint = "";
        options.TokenEndpoint = "";
        options.UserInformationEndpoint = "";
        options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
        options.ClaimActions.MapJsonKey(ClaimTypes.Name, "username");
        options.ClaimActions.MapJsonKey("urn:discord:avatar", "avatar");
        options.SaveTokens = true;

In the above code, we configure the authentication middleware to use the Discord OAuth provider. We specify the client ID, client secret, callback path, and endpoints for authorization, token, and user information. We also map the required claims to the JSON keys returned by the Discord API.

Handling the Authentication Callback

configuring the authentication middleware, we need to handle the authentication callback in a . Here's an example:

public  SignInDiscord()
    var authenticationProperties = _signInManager.ConfigureExternalAuthenticationProperties("Discord", "/callback");
    return Challenge(authenticationProperties, "Discord");

public async Task Callback()
    var authenticateResult = await HttpContext.AuthenticateAsync("Discord");
    // Handle the authentication result and sign in the user
    // ...

In the above code, the SignInDiscord action redirects the user to the Discord authorization endpoint, initiating the authentication process. The Callback action handles the authentication callback and retrieves the authentication result. You can then handle the result and sign in the user to your application.


In this article, we explored how to implement Discord authentication using OAuth in ASP.NET Core 2. We learned OAuth, setting up Discord OAuth, configuring the authentication middleware, and handling the authentication callback. By following these steps, you can enable Discord authentication in your ASP.NET Core 2 web application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents