コード例 #1
0
        /// <summary>
        /// Called when [authorization].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="messageId">The message identifier.</param>
        /// <param name="filters">The filters.</param>
        /// <returns>Task&lt;System.Boolean&gt;.</returns>
        public async Task <bool> OnAuthorization(HttpContext context, HttpServerMessageSender sender, string messageId,
                                                 IEnumerable <IAuthorizationFilter> filters)
        {
            foreach (var filter in filters)
            {
                var path         = HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString()));
                var serviceRoute = await _serviceRouteProvider.GetRouteByPathRegex(path);

                if (serviceRoute == null)
                {
                    serviceRoute = await _serviceRouteProvider.GetLocalRouteByPathRegex(path);
                }
                context.Items.Add("route", serviceRoute);
                var filterContext = new AuthorizationFilterContext
                {
                    Path    = path,
                    Context = context,
                    Route   = serviceRoute
                };
                await filter.OnAuthorization(filterContext);

                if (filterContext.Result != null)
                {
                    await sender.SendAndFlushAsync(new TransportMessage(messageId, filterContext.Result));

                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
 private void AppResolve(IApplicationBuilder app)
 {
     RestContext.GetContext().Initialize(app.ApplicationServices);
     app.ApplicationServices.GetRequiredService <ObjectAccessor <IApplicationBuilder> >().Value = app;
     app.UseStaticFiles();
     app.UseRouting();
     _moduleProvider.Configure(new ApplicationInitializationContext(app.ApplicationServices));
     app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); });
     app.Run(async context =>
     {
         var messageId = Guid.NewGuid().ToString("N");
         var sender    = new HttpServerMessageSender(_serializer, context, _diagnosticListener);
         try
         {
             var filters   = app.ApplicationServices.GetServices <IAuthorizationFilter>();
             var isSuccess = await OnAuthorization(context, sender, messageId, filters);
             if (isSuccess)
             {
                 var actionFilters = app.ApplicationServices.GetServices <IActionFilter>();
                 await OnReceived(sender, messageId, context, actionFilters);
             }
         }
         catch (Exception ex)
         {
             var filters = app.ApplicationServices.GetServices <IExceptionFilter>();
             WirteDiagnosticError(messageId, ex);
             await OnException(context, sender, messageId, ex, filters);
         }
     });
 }
コード例 #3
0
        /// <summary>
        /// Called when [exception].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="messageId">The message identifier.</param>
        /// <param name="exception">The exception.</param>
        /// <param name="filters">The filters.</param>
        /// <returns>Task&lt;System.Boolean&gt;.</returns>
        public async Task <bool> OnException(HttpContext context, HttpServerMessageSender sender, string messageId,
                                             Exception exception, IEnumerable <IExceptionFilter> filters)
        {
            foreach (var filter in filters)
            {
                var path          = HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString()));
                var filterContext = new ExceptionContext
                {
                    RoutePath = path,
                    Context   = context,
                    Exception = exception
                };
                await filter.OnException(filterContext);

                if (filterContext.Result != null)
                {
                    await sender.SendAndFlushAsync(new TransportMessage(messageId, filterContext.Result));

                    return(false);
                }
            }

            return(true);
        }