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.

IdentityServerUrlMiddleware.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Routing;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using IdentityServer4.Extensions;
  11. using Teknik.Configuration;
  12. using Teknik.Utilities;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.AspNetCore.Mvc.Internal;
  15. using Microsoft.AspNetCore.Mvc.Routing;
  16. namespace Teknik.Middleware
  17. {
  18. public class IdentityServerUrlMiddleware
  19. {
  20. private readonly RequestDelegate _next;
  21. private readonly IRouter _router;
  22. public IdentityServerUrlMiddleware(RequestDelegate next, IRouter router)
  23. {
  24. _next = next;
  25. _router = router;
  26. }
  27. public async Task Invoke(HttpContext httpContext, Config config)
  28. {
  29. RouteData routeData = new RouteData();
  30. routeData.Routers.Add(_router);
  31. var context = new ActionContext(httpContext, routeData, new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor());
  32. UrlHelper urlHelper = new UrlHelper(context);
  33. string baseUrl = urlHelper.SubRouteUrl("auth", "Auth.IdentityServer");
  34. string curSub = baseUrl.GetSubdomain();
  35. //if (!string.IsNullOrEmpty(curSub) && curSub != "dev")
  36. httpContext.SetIdentityServerOrigin(baseUrl);
  37. httpContext.SetIdentityServerBasePath(httpContext.Request.PathBase.Value.TrimEnd('/'));
  38. await _next(httpContext);
  39. }
  40. }
  41. // Extension method used to add the middleware to the HTTP request pipeline.
  42. public static class IdentityServerUrlMiddlewareExtensions
  43. {
  44. public static IApplicationBuilder UseIdentityServerUrl(this IApplicationBuilder builder, Config config)
  45. {
  46. var routes = new RouteBuilder(builder)
  47. {
  48. DefaultHandler = builder.ApplicationServices.GetRequiredService<MvcRouteHandler>(),
  49. };
  50. routes.BuildRoutes(config);
  51. return builder.UseMiddleware<IdentityServerUrlMiddleware>(routes.Build());
  52. }
  53. }
  54. }