Authorization in .NET SDK

This documentation provides an overview of how to configure and authorize requests to the Aspose Barcode Cloud API using JWT and external authorization methods. It covers key code examples and settings needed to enable secure and authorized API requests.

Note: For manage authorization you should have a Client Id and Client Secret. How to get it described in Quick Start Section

Overview

Aspose Barcode Cloud API supports two main authorization methods:

  • JWT (JSON Web Token) - Allows token-based authentication where tokens are obtained using Client Id and Client Secret.
  • External Authorization - Permits manual setting of JWT tokens for custom authorization.

JWT Authorization

When using JWT, the API classes automatically obtains a token and includes it in each API request.

Setting Up JWT Authorization

To set up JWT authorization, specify the ClientId and ClientSecret:

var config = new Configuration
{
    ClientId = "<Your-Client-Id>",
    ClientSecret = "<Your-Client-Secret>",
    AuthType = AuthType.JWT
};

var generateApi = new GenerateApi(config);

Upon setup, the internal SDK methods handles token generation and token refresh:

  • The RequestToken method requests a token using ClientId and ClientSecret.
  • The token is added to the Authorization header for each request.

External Authorization

External authorization allows you to manually manage tokens. Assign your JWT token directly to JwtToken and leave ClientId and ClientSecret blank.

Setting Up External Authorization

Example configuration with External Authorization:

var config = new Configuration
{
    AuthType = AuthType.ExternalAuth,
    JwtToken = "<Your-External-Jwt-Token>"
};

var recognizeApi = new RecognizeApi(config);

In this mode, the API sends the provided JWT in the Authorization header without attempting to generate or refresh the token.

How to Fetch a Token without SDK Internal Methods

To fetch a new token, send a POST request to https://id.aspose.cloud/connect/token with the application/x-www-form-urlencoded content type. In the request body, specify the following parameters:

grant_type=client_credentials
client_id=<Your-Client-Id>
client_secret=<Your-Client-Secret>

A Code example for fetching token:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Snippets;

internal static class Program
{
    public static async Task Main(string[] args)
    {
        var clientId = "Client Id from https://dashboard.aspose.cloud/applications";
        var clientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";

        //Check the clientId is changed to not break github ci pipeline
        if(clientId.StartsWith("Client Id"))
        {
            Console.WriteLine("Client Id not changed. Skip this snippet test.");
            return;

        }
        using var client = new HttpClient
        {
            BaseAddress = new Uri("https://id.aspose.cloud/")
        };

        var payload = new FormUrlEncodedContent(new[]
        {
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
        new KeyValuePair<string, string>("client_id", clientId),
        new KeyValuePair<string, string>("client_secret", clientSecret)
    });

        var response = await client.PostAsync("connect/token", payload);
        response.EnsureSuccessStatusCode();

        var strData = await response.Content.ReadAsStringAsync();

        JsonNode data = JsonSerializer.Deserialize<JsonNode>(strData)!;

        Console.WriteLine("Token reciewed successfullly.");
        // Uncomment next line to view token
        // Console.WriteLine(data["access_token"]!.GetValue<string>());
    }
}

Conclusion

Configuring authorization in the Aspose.BarCode.Cloud SDK for .NET allows secure, authenticated access to barcode-related functionalities. Choose JWT authorization wiht client id and secret for automatic token management or external for custom token handling.

With these examples and explanations, you should be able to set up authorization for the Aspose.BarCode.Cloud SDK for .NET.