Bat.AspNetCore 10.0.1

dotnet add package Bat.AspNetCore --version 10.0.1
                    
NuGet\Install-Package Bat.AspNetCore -Version 10.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="Bat.AspNetCore" Version="10.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bat.AspNetCore" Version="10.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Bat.AspNetCore" />
                    
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 Bat.AspNetCore --version 10.0.1
                    
#r "nuget: Bat.AspNetCore, 10.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.
#:package Bat.AspNetCore@10.0.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=Bat.AspNetCore&version=10.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Bat.AspNetCore&version=10.0.1
                    
Install as a Cake Tool

For use Bat.AspNetCore just do it :

1- Install Bat.AspNetCore on your project

2- Use it in your project for example :

// Configure the Services.
builder.Services.AddBatAuthentication();

builder.Services.AddBatJwtConfiguration(_jwtSettings);

builder.Services.AddMemoryCache();

builder.Services.Configure<JwtSettings>(_configuration.GetSection("JwtSetting"));
builder.Services.AddTransient<IJwtService, JwtService>();

builder.Services.AddBatSwagger(_swaggerSetting);


// Configure the HTTP request pipeline.
app.UseMiddleware<BatJwtParserMiddleware>();

app.UseMiddleware<BatEnableRequestBufferingMiddleware>();

app.UseMiddleware<BatExceptionHandlingMiddleware>();

app.UseBatJwtConfiguration();

OR

public class JwtParserMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IJwtService _jwtService;
    private readonly JwtSettings _jwtSettings;

    public JwtParserMiddleware(RequestDelegate next, IJwtService jwtService, IOptions<JwtSettings> jwtSettings)
    {
        _next = next;
        _jwtService = jwtService;
        _jwtSettings = jwtSettings.Value;
    }

    public async Task Invoke(HttpContext context)
    {
        var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
        try
        {
            if (token != null)
            {
                var userClaims = _jwtService.GetClaimsPrincipal(token, _jwtSettings);
                if (userClaims != null) context.User = userClaims;
            }
            else
            {
                if (context.User.Claims.Any())
                {
                    context.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
                    context.Response.ContentType = "application/Json";
                    var bytes = Encoding.ASCII.GetBytes(new { isSuccess = false, message = "UnAuthorized Access To Api !. Token Not Sent.", resultCode = 401 }.SerializeToJson());
                    await context.Response.Body.WriteAsync(bytes);
                }
            }

            await _next(context);
        }
        catch (Exception e)
        {
            byte[] bytes;
            if (e.Message.Contains("Lifetime validation failed"))
            {
                #region Expired Token
                //var validationTime = _jwtService.GetTokenExpireTime(token, _jwtSettings);
                bytes = Encoding.UTF8.GetBytes(new 
                {
                    resultCode = 1001,
                    isSuccess = false,
                    message = "JWT Token Lifetime has Expired. Please Login again!",
                }.SerializeToJson());
                #endregion
            }
            else
            {
                #region Another Exception
                bytes = Encoding.UTF8.GetBytes(new 
                {
                    resultCode = 1002,
                    isSuccess = false,
                    message = "Invalid JWT Token signature. Please refresh your token." +
                    Environment.NewLine + e.Message
                }.SerializeToJson());
                #endregion
            }

            context.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
            context.Response.ContentType = "application/Json";
            await context.Response.Body.WriteAsync(bytes);
        }
    }
}
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.  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 is compatible.  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
10.0.1 170 12/25/2025
10.0.0 166 12/24/2025
8.0.11 381 11/26/2024
8.0.1 314 1/22/2024
8.0.0 289 12/1/2023
7.0.3 451 3/1/2023
7.0.2 452 2/4/2023
7.0.0 511 12/6/2022
6.0.22 311 8/19/2023
6.0.21 250 8/19/2023
6.0.1 847 3/27/2022
1.1.6 597 12/26/2021
1.1.5 772 7/12/2021
1.1.4 709 4/8/2021
1.1.3 629 4/8/2021
1.1.2 658 4/3/2021
1.1.1 708 4/3/2021
1.1.0 649 3/26/2021
1.0.7 628 2/4/2021
1.0.4 790 10/19/2020

- Upgrade to Net10.0 and C#14.0
- Add new File extension methods for read and write files to host
- Add JwtService for create and validate JwtTokens
- Custom Middleware