.NET Quickstart

This guide will help you integrate Descope's .NET SDK into your backend application. Follow the steps below to get started.

Use this pre-built prompt to get started faster.

Install Backend SDK

Navigate to the .NET project directory which contains your .csproj file and install the SDK with dotnet using the following command:

Terminal
dotnet add package Descope

Set up Environment file

Create a appsettings.json file in the root directory of your project with your Descope Project ID, which can be found on the Project Settings Page of the console.

If you plan to use Management functions, include a Descope Management Key here as well, which can be found on the Company Settings Page of the console.

appsettings.json
{
  "Descope": {
    "ProjectId": "__ProjectID__",
    "ManagementKey": "DESCOPE_MANAGEMENT_KEY"
  }
}

Setup Backend SDK

You'll need to configure a DescopeClientOptions object using the values from your appsettings.json, then create the client using one of the two approaches below.

If you're using a custom domain with your Descope project, set the BaseUrl property on DescopeClientOptions (e.g. BaseUrl = "https://auth.company.com").

Program.cs
using Descope;
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
  .AddJsonFile("appsettings.json")
  .Build();

var options = new DescopeClientOptions
{
    ProjectId = config["Descope:ProjectId"],          // Required
    ManagementKey = config["Descope:ManagementKey"],  // Optional, for management APIs
    BaseUrl = "__BaseURL__",           // Optional, auto-detected; only set to override (e.g. custom domain)
    JwksCacheDuration = TimeSpan.FromMinutes(5)    // Optional, how long public signing keys are cached (default: 5 minutes)
};

Note

ValidateSession verifies tokens locally using Descope's public signing keys (JWKS), which the SDK fetches and caches for the duration set by JwksCacheDuration (default: 5 minutes). Most validations hit this cache and make no network call.

On high-traffic services you can raise this value (e.g. TimeSpan.FromMinutes(30)) to reduce key-fetch requests. This is safe even during key rotation: if a token is signed by an unknown key ID, the SDK immediately re-fetches keys and retries once.

Option 1: Dependency Injection

Best for ASP.NET Core apps. AddDescopeClient registers IDescopeClient as a singleton in the DI container, so you can inject it anywhere into your services. This keeps configuration in one place, hands lifetime management to the framework, and makes testing easier since IDescopeClient can be swapped for a mock in your unit tests.

Program.cs
builder.Services.AddDescopeClient(options);
MyService.cs
public class MyService
{
    private readonly IDescopeClient descopeClient;

    public MyService(IDescopeClient client)
    {
        descopeClient = client;
    }
}

Option 2: Factory (instance-based)

Best for console apps, background workers, or when you need to manually control the client's lifetime. Create the client once and reuse that instance so key caching works across calls.

Program.cs
var descopeClient = DescopeManagementClientFactory.Create(options);

Implement Session Validation

Note

If you need more granular control over session validation and prefer to use built-in Microsoft packages, see our .NET JWT Validation Guide for details on validating session tokens directly.

You will need to fetch the session token from the Authorization header of each request, and use the SDK to validate the token.

The frontend SDK will store the session token in either a cookie or your browser's local storage. If using a cookie, the token will be sent to your app server automatically with every request.

The ValidateSession function can be used to verify a user's session as shown below. This either validates the sessions or throws an error, depending on if the JWT is valid or not.

// Validate the session. Will return an error if expired
try
{
    var sessionToken = await descopeClient.Auth.ValidateSessionAsync(sessionJwt);
}
catch (DescopeException e)
{
    // Handle the error
}

Once you've implemented the basic session validation, you can enhance your application with these additional features:

Additional Resources

Have You Implemented the Frontend Yet?

When integrating Descope into your application, you have three options depending on how much control you want over your frontend authentication experience and session management:

OptionDescriptionBest For
Use Descope FlowsDesign your authentication screens and flows visually in the Descope Console with little or no frontend code. We handle all session management for you.Fastest setup with minimal custom frontend work.
Use Descope Client SDKsBuild your own login screens and authentication experiences in your frontend using code, while relying on Descope's SDKs to manage sessions (login, logout, refresh).Customizable UX with simplified session handling.
Use Descope Backend SDKsBuild your own frontend and your own backend APIs for authentication. You fully manage sessions, tokens, and authentication logic yourself.Maximum flexibility and control, at the cost of more engineering effort.
Was this helpful?

On this page