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

12345678910111213141516171819202122232425262728293031323334353637383940
  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.IdentityServer.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)
  20. {
  21. // Generate the NONCE used for this request
  22. string nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(StringHelper.RandomString(24)));
  23. httpContext.Items[Constants.NONCE_KEY] = nonce;
  24. await _next(httpContext);
  25. }
  26. }
  27. // Extension method used to add the middleware to the HTTP request pipeline.
  28. public static class SetupHttpContextMiddlewareExtensions
  29. {
  30. public static IApplicationBuilder UseHttpContextSetup(this IApplicationBuilder builder)
  31. {
  32. return builder.UseMiddleware<SetupHttpContextMiddleware>();
  33. }
  34. }
  35. }