ServiceStackAPIDocumentationinSwagger-UIAfterClosedDoor和元数据,但我想一直允许API访问(API有一些公共方法,有些需要基本身份验证)。所以我所做的是为API添加这个路由前缀:publicoverrideRouteAttribute[]GetRouteAttributes(TyperequestType){routes.Each(x=>x.Path="/API"+x.Path);返回路线;}和:ServiceRoutes=newDictionary{{typeof(AuthenticateService),new[]{"/api/auth","/api/auth/{provider}"}},}这也在网络配置中:是,当我转到/api/now时它工作正常,但是当我尝试调用某些方法时,我被重定向到我的登录路径。有没有办法像我开始的那样解决这个问题,或者有更好的方法来保护文档?在元数据页面上没有要求身份验证的明确选项,但您可以使用PreRequestFilter来保护对/metadata和/swagger-ui页面的访问:PreRequestFilters.Add((req,res)=>{if(req.PathInfo.StartsWith("/metadata")||req.PathInfo.StartsWith("/swagger-ui")){varsession=req.GetSession();if(!session.IsAuthenticated){res.StatusCode=(int)HttpStatusCode.Unauthorized;res.EndRequest();}}});如果您正在使用Swagger2.0/OpenAPI功能,您可以保护对/openapiJSON规范的访问,您可以在运行时动态添加[Authenticate]属性:publicAppHost(){typeof(OpenApiService).AddAttributes(newAuthenticateAttribute());如果您使用的是较旧的Swagger1.2插件,则可以通过以下方式保护对后端服务的访问:typeof(SwaggerResources).AddAttributes(newAuthenticateAttribute());这假定您使用的是ServiceStack身份验证而不是ASP.NET身份验证。以上就是C#学习教程:关门后Swagger-UI中ServiceStackAPI文档分享的全部内容。代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
