public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { var config = app.ApplicationServices.GetRequiredService <AnyServiceConfig>(); if (config.UseErrorEndpointForExceptionHandling) { app.UseExceptionHandler("/__error"); } app.UseHsts(); app.UseHttpsRedirection(); app.UseAuthentication(); //we need to customize what happens when user is not authenticated in order to enable notifications var handler = OnMissingUserIdWorkContextMiddlewareHandlers.PermittedPathsOnMissingUserIdHandler(new[] { new PathString("/chathub"), new PathString("/notify") }); app.UseMiddleware <WorkContextMiddleware>(handler); app.UseAnyService(useWorkContextMiddleware: false); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapAnyService(); endpoints.MapHub <ChatHub>("/chatHub"); }); }
public async Task DefaultOnMissingUserIdHandler_Test() { var statusCode = 0; var hc = new Mock <HttpContext>(); hc.SetupSet(_ => _.Response.StatusCode = It.Is <int>(c => c == StatusCodes.Status401Unauthorized)).Callback <int>(c => statusCode = c); var l = new Mock <ILogger>(); var res = await OnMissingUserIdWorkContextMiddlewareHandlers.DefaultOnMissingUserIdHandler(hc.Object, null, l.Object); res.ShouldBeFalse(); statusCode.ShouldBe(StatusCodes.Status401Unauthorized); }
//[InlineData("/auth-required", false, 401)] public async Task PermittedPathsOnMissingUserIdHandler_Test(string path, bool isPermitted, int expStatusCode) { var statusCode = 0; var hc = new Mock <HttpContext>(); hc.SetupGet(_ => _.Request.Path).Returns(new PathString(path)); hc.SetupSet(_ => _.Response.StatusCode = It.IsAny <int>()).Callback <int>(c => statusCode = c); var l = new Mock <ILogger>(); var handler = OnMissingUserIdWorkContextMiddlewareHandlers.PermittedPathsOnMissingUserIdHandler(new[] { new PathString("/no-auth-required") }); var res = await handler(hc.Object, null, l.Object); res.ShouldBe(isPermitted); statusCode.ShouldBe(expStatusCode); }