The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetupHttpContextMiddleware.cs 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Http;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Teknik.Configuration;
  9. using Teknik.Utilities;
  10. namespace Teknik.Middleware
  11. {
  12. public class SetupHttpContextMiddleware
  13. {
  14. private readonly RequestDelegate _next;
  15. public SetupHttpContextMiddleware(RequestDelegate next)
  16. {
  17. _next = next;
  18. }
  19. public async Task Invoke(HttpContext httpContext, Config config)
  20. {
  21. // Setup the HTTP Context for everything else
  22. // Generate the NONCE used for this request
  23. string nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(StringHelper.RandomString(24)));
  24. httpContext.Items[Constants.NONCE_KEY] = nonce;
  25. await _next(httpContext);
  26. }
  27. }
  28. // Extension method used to add the middleware to the HTTP request pipeline.
  29. public static class SetupHttpContextMiddlewareExtensions
  30. {
  31. public static IApplicationBuilder UseHttpContextSetup(this IApplicationBuilder builder)
  32. {
  33. return builder.UseMiddleware<SetupHttpContextMiddleware>();
  34. }
  35. }
  36. }