HexaEightMiddleware 1.6.12

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

// Install HexaEightMiddleware as a Cake Tool
#tool nuget:?package=HexaEightMiddleware&version=1.6.12                

HexaEight Middleware

How To Use This Library

Sample Middleware Code (Startup and Configuration Section)

public void ConfigureServices(IServiceCollection services)
{
	
	services.Add(new ServiceDescriptor(typeof(HexaEightResource), new HexaEightResource("<client id>", "<token server url>")));
	services.AddSingleton<IAuthenticationService, AuthenticationMiddleware>();
	
	services.AddAuthentication();
	services.AddAuthorization();
        services.AddCors();
	services.AddControllers();
	
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
	if (env.IsDevelopment())
	{
		app.UseDeveloperExceptionPage();
	}

	app.UseCors(x => x
		.AllowAnyMethod()
		.AllowAnyHeader()
		.SetIsOriginAllowed(origin => true)
		.AllowCredentials());

	app.UseMiddleware<HexaEightEngine.Middleware>();
	app.UseRouting();
	app.Use(async (context, next) =>
	{
		await next();

		if (context.Response.StatusCode == (int)HttpStatusCode.Unauthorized)
		{
			context.Response.StatusCode = (int)HttpStatusCode.OK;
			await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes("UnAuthorized - Access Denied"));
                }

	});

	app.UseAuthentication();
	app.UseAuthorization();

	app.UseEndpoints(endpoints =>
	{
		endpoints.MapControllers();
	});
}

Sample Weather Controller Code with Authorization for Get and Post

[HttpGet("{resource}")]
[Authorize(AuthenticationSchemes = "Bearer")]
public IEnumerable<WeatherForecast> Get(string resource)
{
	var rng = new Random();
	return Enumerable.Range(1, 5).Select(index => new WeatherForecast
	{
		Date = DateTime.Now.AddDays(index),
		TemperatureC = rng.Next(-20, 55),
		Summary = resource + " "  + HttpContext.User.Identity.Name.ToString()  +  " : " + Summaries[rng.Next(Summaries.Length)]
	})
	.ToArray();
}

[HttpPost("{resource}")]
[Authorize(AuthenticationSchemes = "Bearer")]
public async Task<IEnumerable<WeatherForecast>> Post(string resource)
{

	string body = "";
	using (StreamReader stream = new StreamReader(HttpContext.Request.Body))
	{
		body = await stream.ReadToEndAsync();
	}

	Console.WriteLine(body);

	var rng = new Random();
	return Enumerable.Range(1, 5).Select(index => new WeatherForecast
	{
		Date = DateTime.Now.AddDays(index),
		TemperatureC = rng.Next(-20, 55),
		Summary = resource + " :: " + HttpContext.User.Identity.Name.ToString() + " : " + Summaries[rng.Next(Summaries.Length)]
		})
		.ToArray();
	}

}


Sample Authentication Middleware Code For Additional Authorization

public class AuthenticationMiddleware : IAuthenticationService
{

	// context.user.Identity contains all information about the authenticated user
	// Resource Servers can perform additional authorization at this layer for advanced authorization scenarios

	public Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string scheme)
        {
            try
            {

                if (context.User.Identity.IsAuthenticated && context.User.Identity.AuthenticationType == "HexaEight Identity")
                {
		 
                    Console.WriteLine(context.User.Claims.FirstOrDefault(c => c.Type == "Sender").Value);
                    AuthenticationTicket at = new AuthenticationTicket(context.User, "HexaEight");

                    return Task.FromResult(AuthenticateResult.Success(at));
                }
                return Task.FromResult(AuthenticateResult.Fail("UnAuthorized"));
            }
            catch
            {
                return Task.FromResult(AuthenticateResult.Fail("UnAuthorized"));
            }


        }

        public Task ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)
        {
            context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            return Task.CompletedTask;
        }

        public Task ForbidAsync(HttpContext context, string scheme, AuthenticationProperties properties)
        {
            throw new NotImplementedException();
        }

        public Task SignInAsync(HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
        {

            Console.WriteLine(context.User.Identity.Name);
            Console.WriteLine(principal.Identity.Name);
            return Task.CompletedTask;
        }

        public Task SignOutAsync(HttpContext context, string scheme, AuthenticationProperties properties)
        {
            throw new NotImplementedException();
	}
}

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 is compatible. 
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.6.58 106 6/29/2024
1.6.57 107 6/29/2024
1.6.56 108 6/23/2024
1.6.55 117 5/4/2024
1.6.54 126 5/4/2024
1.6.53 109 4/18/2024
1.6.52 89 4/18/2024
1.6.51 99 4/17/2024
1.6.50 113 4/16/2024
1.6.49 247 3/1/2024
1.6.48 521 11/2/2023
1.6.47 413 11/2/2023
1.6.46 462 11/1/2023
1.6.45 452 10/29/2023
1.6.44 423 10/28/2023
1.6.43 478 10/15/2023
1.6.42 524 9/27/2023
1.6.41 527 9/18/2023
1.6.40 546 9/13/2023
1.6.39 579 8/21/2023
1.6.38 561 8/20/2023
1.6.37 595 6/12/2023
1.6.35 545 5/14/2023
1.6.34 667 5/14/2023
1.6.33 626 5/14/2023
1.6.32 657 3/29/2023
1.6.31 670 3/29/2023
1.6.30 697 3/28/2023
1.6.29 689 3/28/2023
1.6.28 700 2/18/2023
1.6.27 708 2/14/2023
1.6.26 750 2/12/2023
1.6.25 768 2/5/2023
1.6.24 760 2/3/2023
1.6.23 735 2/2/2023
1.6.22 744 2/2/2023
1.6.21 716 1/31/2023
1.6.20 757 1/29/2023
1.6.19 755 1/29/2023
1.6.18 736 1/29/2023
1.6.17 728 1/29/2023
1.6.16 776 1/26/2023
1.6.15 754 1/22/2023
1.6.14 692 1/21/2023
1.6.13 727 1/21/2023
1.6.12 794 1/21/2023
1.6.11 765 1/18/2023
1.6.10 795 1/15/2023
1.6.9 720 1/15/2023
1.6.8 780 1/15/2023