using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Owin; using Microsoft.Owin.Mapping; using Owin; using Custom.Owin.Authentication.Infrastructure; namespace Custom.Owin.Authentication.Extensions { public static class IAppBuilderExtensions { public static IAppBuilder MapEx(this IAppBuilder app, string pathMatch, Action configuration) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (pathMatch == null) { throw new ArgumentNullException(nameof(pathMatch)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (!string.IsNullOrEmpty(pathMatch) && pathMatch.EndsWith("/", StringComparison.Ordinal)) { throw new ArgumentException("Path must not end with a '/'", nameof(pathMatch)); } return app.MapEx(new PathString(pathMatch), configuration); } public static IAppBuilder MapEx(this IAppBuilder app, PathString pathMatch, Action configuration) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (pathMatch.HasValue && pathMatch.Value.EndsWith("/", StringComparison.Ordinal)) { throw new ArgumentException("Path must not end with a '/'", nameof(pathMatch)); } MapOptions mapOptions = new MapOptions { PathMatch = pathMatch }; IAppBuilder result = app.Use(new object[] { mapOptions }); IAppBuilder appBuilder = app.New(); configuration(appBuilder); mapOptions.Branch = (Func, Task>)appBuilder.Build(typeof(Func, Task>)); return result; } } }