private static ActionConstraintContext CreateActionConstraintContext(HttpMethodActionConstraint constraint) { var context = new ActionConstraintContext(); var actionSelectorCandidate = new ActionSelectorCandidate(new ActionDescriptor(), new List<IActionConstraint> { constraint }); context.Candidates = new List<ActionSelectorCandidate> { actionSelectorCandidate }; context.CurrentCandidate = context.Candidates[0]; return context; }
public void HttpMethodActionConstraint_Accept_Preflight_CaseInsensitive(IEnumerable<string> httpMethods, string accessControlMethod) { // Arrange var constraint = new HttpMethodActionConstraint(httpMethods); var context = CreateActionConstraintContext(constraint); context.RouteContext = CreateRouteContext("oPtIoNs", accessControlMethod); // Act var result = constraint.Accept(context); // Assert Assert.True(result, "Request should have been accepted."); }
public void HttpMethodActionConstraint_Accept_CaseInsensitive(IEnumerable <string> httpMethods, string expectedMethod) { // Arrange var constraint = new HttpMethodActionConstraint(httpMethods); var context = CreateActionConstraintContext(constraint); context.RouteContext = CreateRouteContext(expectedMethod); // Act var result = constraint.Accept(context); // Assert Assert.True(result, "Request should have been accepted."); }
public void HttpMethodActionConstraint_IgnoresPreflightRequests(IEnumerable <string> httpMethods, string accessControlMethod) { // Arrange var constraint = new HttpMethodActionConstraint(httpMethods); var context = CreateActionConstraintContext(constraint); context.RouteContext = CreateRouteContext("oPtIoNs", accessControlMethod); // Act var result = constraint.Accept(context); // Assert Assert.False(result, "Request should have been rejected."); }
private static ActionConstraintContext CreateActionConstraintContext(HttpMethodActionConstraint constraint) { var context = new ActionConstraintContext(); var actionSelectorCandidate = new ActionSelectorCandidate(new ActionDescriptor(), new List <IActionConstraint> { constraint }); context.Candidates = new List <ActionSelectorCandidate> { actionSelectorCandidate }; context.CurrentCandidate = context.Candidates[0]; return(context); }
public void Apply(ActionModel action) { var refitAttrs = GetRefitAttributes(action.ActionMethod); foreach (var refitAttr in refitAttrs) { var httpMethodActionConstraint = new HttpMethodActionConstraint(new[] { refitAttr.Method.Method }); var attributeRouteModel = new AttributeRouteModel(new RouteAttribute($"{_routePrefix}{refitAttr.Path}")); if (action.Selectors.Count == 1 && action.Selectors.First().AttributeRouteModel == null) { var selector = action.Selectors.First(); if (!string.IsNullOrEmpty(attributeRouteModel.Template)) { selector.AttributeRouteModel = attributeRouteModel; } selector.ActionConstraints.Add(httpMethodActionConstraint); } else { var selectorModel = new SelectorModel { ActionConstraints = { httpMethodActionConstraint } }; if (!string.IsNullOrEmpty(attributeRouteModel.Template)) { selectorModel.AttributeRouteModel = attributeRouteModel; } action.Selectors.Add(selectorModel); } } }
public override void PreInitialize() { Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString( JPGZServiceConsts.ConnectionStringName ); //配置grpc Configuration.Modules.UseGrpcService(option => { option.GrpcBindAddress = _appConfiguration["Grpc:GrpcBindAddress"]; option.GrpcBindPort = int.Parse(_appConfiguration["Grpc:GrpcBindPort"]); }).AddRpcServiceAssembly(typeof(JPGZServiceApplicationModule).Assembly); //禁用redis缓存会自动使用内存缓存 if (bool.Parse(_appConfiguration["App:RedisCache:IsEnabled"])) { //使用redis作为缓存 Configuration.Caching.UseRedis(options => { options.ConnectionString = _appConfiguration["App:RedisCache:ConnectionString"]; options.DatabaseId = _appConfiguration.GetValue <int>("App:RedisCache:DatabaseId"); }); //配置redis的Cache过期时间 Configuration.Caching.Configure("mycache", cache => { //缓存滑动过期时间,时长应当根据数据的更新频率来设置 cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(10); }); } //其他缓存时间配置 Configuration.Caching.ConfigureAll(options => { options.Clear(); options.DefaultSlidingExpireTime = TimeSpan.FromMinutes(5); }); //使用配置管理器 Configuration.Settings.Providers.Add <ConfigSettingProvider>(); // Use database for language management //Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization(); Configuration.Modules.AbpAspNetCore() .CreateControllersForAppServices( typeof(JPGZServiceApplicationModule).GetAssembly() ). /* 自定义路由格式, * 默认为/api/services/app/Controller.ControllerName/action.ActionName/*/ ConfigureControllerModel(model => { foreach (var action in model.Actions) { var verb = ProxyScriptingHelper.GetConventionalVerbForMethodName(action.ActionName); var constraint = new HttpMethodActionConstraint(new List <string> { verb }); foreach (var selector in action.Selectors) { selector.ActionConstraints.Add(constraint); selector.AttributeRouteModel = new AttributeRouteModel( new RouteAttribute( $"api/{action.Controller.ControllerName}/{action.ActionName}" ) ); } } });; ConfigureTokenAuth(); }
public CorsHttpMethodActionConstraint(HttpMethodActionConstraint constraint) : base(constraint.HttpMethods) { }
public IEnumerable <RouteInformation> GetAllRouteInformations() { List <RouteInformation> ret = new List <RouteInformation>(); var routes = m_actionDescriptorCollectionProvider.ActionDescriptors.Items; foreach (ActionDescriptor _e in routes) { RouteInformation info = new RouteInformation(); // Area if (_e.RouteValues.ContainsKey("area")) { info.Area = _e.RouteValues["area"]; } // Path and Invocation of Razor Pages if (_e is PageActionDescriptor) { var e = _e as PageActionDescriptor; info.Path = e.ViewEnginePath; info.Invocation = e.RelativePath; } // Path of Route Attribute if (_e.AttributeRouteInfo != null) { var e = _e; info.Path = $"/{e.AttributeRouteInfo.Template}"; } // Path and Invocation of Controller/Action if (_e is ControllerActionDescriptor) { var e = _e as ControllerActionDescriptor; if (info.Path == "") { info.Path = $"/{e.ControllerName}/{e.ActionName}"; } info.Invocation = $"{e.ControllerName}Controller.{e.ActionName}"; } // Extract HTTP Verb if (_e.ActionConstraints != null && _e.ActionConstraints.Select(t => t.GetType()).Contains(typeof(HttpMethodActionConstraint))) { HttpMethodActionConstraint httpMethodAction = _e.ActionConstraints.FirstOrDefault(a => a.GetType() == typeof(HttpMethodActionConstraint)) as HttpMethodActionConstraint; if (httpMethodAction != null) { info.HttpMethod = string.Join(",", httpMethodAction.HttpMethods); } } // Special controller path if (info.Path == "/RouteAnalyzer_Main/ShowAllRoutes") { info.Path = RouteAnalyzerRouteBuilderExtensions.RouteAnalyzerUrlPath; } // Additional information of invocation info.Invocation += $" ({_e.DisplayName})"; // Generating List ret.Add(info); } // Result return(ret); }
public Schema GetSchema() { List <ActionDescriptor> ret = new List <ActionDescriptor>(); IReadOnlyList <Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor> routes = m_actionDescriptorCollectionProvider.ActionDescriptors.Items; foreach (Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor ad in routes) { ActionDescriptor info = new ActionDescriptor(); // Path and Invocation of Controller/Action if (ad is ControllerActionDescriptor) { ControllerActionDescriptor cad = ad as ControllerActionDescriptor; if (info.Path == "") { info.Path = $"/{cad.ControllerName}/{cad.ActionName}"; } info.Invocation = $"{cad.ControllerName}Controller.{cad.ActionName}"; } // Area if (ad.RouteValues.ContainsKey("area")) { info.Area = ad.RouteValues["area"]; } // Path and Invocation of Razor Pages if (ad is PageActionDescriptor) { PageActionDescriptor pad = ad as PageActionDescriptor; info.Path = pad.ViewEnginePath; info.Invocation = pad.RelativePath; } // Path of Route Attribute if (ad.AttributeRouteInfo != null) { info.Path = $"/{ad.AttributeRouteInfo.Template}"; } // Extract HTTP Verb if (ad.ActionConstraints != null && ad.ActionConstraints.Select(t => t.GetType()).Contains(typeof(HttpMethodActionConstraint))) { HttpMethodActionConstraint httpMethodAction = ad.ActionConstraints.FirstOrDefault(a => a.GetType() == typeof(HttpMethodActionConstraint)) as HttpMethodActionConstraint; if (httpMethodAction != null) { info.HttpMethod = string.Join(",", httpMethodAction.HttpMethods); } } // Extract parameters if (ad.Parameters != null) { info.Arguments = ad.Parameters.Select(_typeparser.ParseParameter).ToArray(); } // Return type if (ad is ControllerActionDescriptor) { ControllerActionDescriptor cad = ad as ControllerActionDescriptor; info.ReturnType = new Identifier { Kind = cad.MethodInfo.ReturnType.GetKind(), Type = _typeparser.ParseType(cad.MethodInfo.ReturnType).TypeName }; } // Additional information of invocation info.Invocation += $" ({ad.DisplayName})"; // Special controller path if (info.Path == "/MvcSchema/GetSchema") { continue; } // Generating List ret.Add(info); } return(new Schema { Actions = ret.ToArray(), Types = _typeparser.TypeDescriptors.ToArray() }); }
public IEnumerable <RouteInformation> GetAllRouteInformations() { List <RouteInformation> ret = new List <RouteInformation>(); var routes = m_actionDescriptorCollectionProvider.ActionDescriptors.Items; routes = routes.Where(w => w.FilterDescriptors.Any(f => f.Filter.GetType() == typeof(FirewallValidate) && ((FirewallValidate)f.Filter).Policies.Contains(Firewall.Policy.FirewallPolicyEnum .DisableRequestRule))) .ToList(); foreach (ActionDescriptor _e in routes) { RouteInformation info = new RouteInformation(); // Action if (_e.RouteValues.ContainsKey("Action")) { info.Action = _e.RouteValues["Action"]; } if (_e.RouteValues.ContainsKey("Controller")) { info.Controller = _e.RouteValues["Controller"]; } // Area if (_e.RouteValues.ContainsKey("area")) { info.Area = _e.RouteValues["area"]; } // Path and Invocation of Razor Pages if (_e is PageActionDescriptor) { var e = _e as PageActionDescriptor; info.Path = e.ViewEnginePath; info.Invocation = e.RelativePath; } // Path of Route Attribute if (_e.AttributeRouteInfo != null) { var e = _e; info.Path = $"/{e.AttributeRouteInfo.Template}"; } // Path and Invocation of Controller/Action if (_e is ControllerActionDescriptor) { var e = _e as ControllerActionDescriptor; if (info.Path == "") { info.Path = $"/{e.ControllerName}/{e.ActionName}"; } info.Invocation = $"{e.ControllerName}Controller.{e.ActionName}"; } // Extract HTTP Verb if (_e.ActionConstraints != null && _e.ActionConstraints.Select(t => t.GetType()).Contains(typeof(HttpMethodActionConstraint))) { HttpMethodActionConstraint httpMethodAction = _e.ActionConstraints.FirstOrDefault(a => a.GetType() == typeof(HttpMethodActionConstraint)) as HttpMethodActionConstraint; if (httpMethodAction != null) { info.HttpMethod = string.Join(",", httpMethodAction.HttpMethods); } } // Additional information of invocation info.Invocation += $" ({_e.DisplayName})"; // Generating List ret.Add(info); } // Result return(ret); }