TmaAuth 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TmaAuth --version 1.0.1
NuGet\Install-Package TmaAuth -Version 1.0.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="TmaAuth" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TmaAuth --version 1.0.1
#r "nuget: TmaAuth, 1.0.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.
// Install TmaAuth as a Cake Addin
#addin nuget:?package=TmaAuth&version=1.0.1

// Install TmaAuth as a Cake Tool
#tool nuget:?package=TmaAuth&version=1.0.1

Telegram Mini App Authentication Extensions for ASP.NET Core

This library provides an easy-to-use extension for integrating Telegram Mini App authentication into your ASP.NET Core applications. It leverages custom authentication schemes to secure your applications using Telegram's user data.

Features

  • Easy integration with ASP.NET Core's authentication system.
  • Supports customization of Telegram authentication settings through TelegramAuthenticationOptions.
  • Utilizes claims-based identity to enhance application security and user management.

Getting Started

Installation

To install the Telegram Authentication Extensions, you can use the following NuGet command:

dotnet add package tmaauth --version 1.0.0

Usage

To secure an API endpoint using the Telegram Mini App authentication scheme, simply use the Authorize attribute:

            builder.Services.AddAuthentication()
            .AddTelegramMiniAppToken(options =>
            {
                options.BotToken = builder.Configuration["TelegramOptions:BotToken"];
            });

            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy(TmaPolicy.TmaUserPremiumPolicy, policy =>
                {
                    policy.AddAuthenticationSchemes(TmaTokenDefaults.AuthenticationScheme);
                    policy.RequireClaim(ClaimTypes.NameIdentifier);
                    policy.RequireClaim(TmaClaim.IsPremium, "true");  
                });
            });
[Route("api/[controller]/[action]")]
[ApiController]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries =
    [
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    ];

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

    [HttpGet]
    [Authorize(AuthenticationSchemes = TmaTokenDefaults.AuthenticationScheme)]
    public IActionResult GetSecureData()
    {
        var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        var username = User.FindFirst(ClaimTypes.Name)?.Value;

        return Ok($"Secure data accessed by user ID {userId} with username {username}");
    }

    [HttpGet]
    [Authorize(AuthenticationSchemes = TmaTokenDefaults.AuthenticationScheme, Policy = TmaPolicy.TmaUserPremiumPolicy)]
    public IActionResult GetForPremium()
    {
        return Ok("This is data only for premium users.");
    }
}
Product Compatible and additional computed target framework versions.
.NET 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. 
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.0.3 0 5/27/2024
1.0.2 56 5/24/2024
1.0.1 100 5/7/2024