Identity server 4 get access toke on asp net webform net 4 5


ASP.NET is a popular programming language used for building web applications. It provides a for developing dynamic websites, web services, and web applications. One of the key features of ASP.NET is its to handle user authentication and authorization. In this article, we will explore how to use Identity Server 4 to get an access token in an ASP.NET Web Forms application targeting .NET 4.5.

Setting up Identity Server 4

Before we can using Identity Server 4 in our ASP.NET Web Forms application, we need to set it up. Identity Server 4 is an OpenID Connect and OAuth 2.0 framework for ASP.NET Core. To Identity Server 4, we can use the NuGet package manager in Visual Studio. Open the NuGet package manager console and run the :

Install-Package IdentityServer4

Configuring Identity Server 4

Once Identity Server 4 is installed, we need to configure it in our ASP.NET Web Forms application. This involves setting up the client and resource definitions. The client definition specifies the client application that will be requesting the access token, while the resource definition specifies the protected resource that the client application wants to access.

To configure Identity Server 4, we need to create a new that inherits from the IdentityServer4.Configuration.IIdentityServerBuilder interface. In this class, we can define the client and resource definitions using the AddClient and AddApiResource methods respectively. Here's an example:

public class IdentityServerConfig
    public static void Configure(IIdentityServerBuilder builder)
        builder.AddClient("client_id", "client_secret")


In the above example, we define a client with a client ID and , a redirect URI, and the desired scopes. We also define an API resource with the same scope.

Getting an Access Token

Now that we have Identity Server 4 configured, we can use it to get an access token in our ASP.NET Web Forms application. To do this, we need to make a request to the Identity Server's token endpoint with the appropriate client credentials and scopes.

Here's an example of how to get an access token using the IdentityModel library:

using IdentityModel.Client;

public async Task GetAccessToken()
    var client = new HttpClient();

    var discoveryDocument = await client.GetDiscoveryDocumentAsync("");

    var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
        Address = discoveryDocument.TokenEndpoint,
        ClientId = "client_id",
        ClientSecret = "client_secret",
        Scope = "api_scope"

    return tokenResponse.AccessToken;

In the above example, we create an instance of the class and retrieve the discovery document from the Identity Server. We then make a request to the token endpoint using the client credentials and desired scopes. The access token is in the tokenResponse object.


In this article, we have explored how to use Identity Server 4 to get an access token in an ASP.NET Web Forms application targeting .NET 4.5. We have seen how to set up Identity Server 4, configure it, and make a request to get an access token. By following these steps, you can easily implement authentication and authorization in your ASP.NET Web Forms application using Identity Server 4.

Rate this post

Leave a Reply

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

Table of Contents