コード例 #1
0
        public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
        {
            var  httpContext     = _httpContextAccessor.HttpContext;
            bool IsAuthenticated = authService.IsAuthorized(httpContext);

            if (!IsAuthenticated)
            {
                foreach (var pathItem in swaggerDoc.Paths.Values)
                {
                    pathItem.Get    = null;
                    pathItem.Delete = null;
                    pathItem.Patch  = null;
                    pathItem.Post   = null;
                    pathItem.Put    = null;
                }
            }
        }
コード例 #2
0
        public async Task Invoke(HttpContext context)
        {
            if (_authService.IsAuthorized(context))
            {
                await next.Invoke(context);

                return;
            }

            // Return authentication type (causes browser to show login dialog)
            context.Response.Headers["WWW-Authenticate"] = "Basic";

            // Add realm if it is not null
            if (!string.IsNullOrWhiteSpace(realm))
            {
                context.Response.Headers["WWW-Authenticate"] += $" realm=\"{realm}\"";
            }

            // Return unauthorized
            context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        }