Telegram.MiniApp.Auth 1.2.1

dotnet add package Telegram.MiniApp.Auth --version 1.2.1
                    
NuGet\Install-Package Telegram.MiniApp.Auth -Version 1.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Telegram.MiniApp.Auth" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Telegram.MiniApp.Auth" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Telegram.MiniApp.Auth" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Telegram.MiniApp.Auth --version 1.2.1
                    
#r "nuget: Telegram.MiniApp.Auth, 1.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Telegram.MiniApp.Auth@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Telegram.MiniApp.Auth&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Telegram.MiniApp.Auth&version=1.2.1
                    
Install as a Cake Tool

Logo

Telegram Mini App Auth

Telegram Mini Apps Official Documentation

Install

Install Telegram.MiniApp.Authentication using NuGet Package Manager:

Install-Package Telegram.MiniApp.Authentication

Or via the .NET CLI:

dotnet add package Telegram.MiniApp.Authentication

Usage

Backend

  • Add Authentication:
builder.Services.AddTelegramMiniAppInHeader(options =>
{
	options.BotToken = builder.Configuration.GetValue<string>("BotToken")!;
});
  • Add Authorization:
  // Library provides default policy:
  builder.Services.AddAuthorization(options =>
  {
      // Simple TMA Policy. Only checks tma_user_id claims
	  options.AddDefaultTmaPolicy();
	  
	  // For users with Telegram Premium. Checks tma_user_id and tma_is_premium claims
      options.AddTmaPremiumPolicy();
  });


  // Or you can customize the policy the way you want:
  builder.Services.AddAuthorization(options =>
  {
      options.AddPolicy("MyPolicy", policy =>
      {
          policy.AddAuthenticationSchemes(TmaDefaults.AuthenticationScheme);
          // your settings
      });
  });

After successful authorization, records the following data in the Claims(if not null):

  • tma_user_id - A unique identifier for the telegram user.
  • tma_username - Username of the telegram user.
  • tma_first_name - First name of the telegram user.
  • tma_last_name - Last name of the telegram user.
  • tma_chat_instance - Global identifier, uniquely corresponding to the chat.
  • tma_is_premium - user is a Telegram Premium user or not.

You can get claims in the usual way, or use extension methods provided by the library:


// Get TMA claims as TmaUserPrincipals
if (HttpContext.User.TryGetTmaUser(out var tmaUser))
{
    //Using
}

// Get all initData as TmaInitData
if (HttpContext.TryGetTmaInitData(out var initData))
{
    //Using
}

Frontend

Set window.Telegram.WebApp.initData with prefix 'Tma ' in Authorization Header (Or another header, what you set in TmaAuthenticationOptions.TokenHeaderName)

example:

        fetch('endpoint-url', {
            headers: {
                'Authorization': `TMA ${window.Telegram.WebApp.initData}`
            }
        })

Solution

  • Telegram.MiniApp.Authentication - Nuget package
  • tests/TestWebApi - .NET Web Api app for auth testing

Authors

By Konstantin Orlov with Kutumov Nikita support.

Address for TON donations, we appreciate any "thanks" 😃

UQD0zFgp0p-eFnbL4cPA6DYqoeWzGbCA81KuU6BKwdFmf8jv

Connect in Telegram

2025

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 464 1/15/2025
1.2.0 115 1/13/2025
1.1.5 153 10/15/2024
1.1.4 119 10/11/2024
1.1.3 111 10/9/2024
1.1.2 114 10/8/2024
1.1.1 107 10/8/2024
1.1.0 100 10/8/2024