Пример #1
0
        public async Task FindRoutes(string method, string path, string aResult)
        {
            HorseMvc mvc = new HorseMvc();

            mvc.Init();
            mvc.CreateRoutes(Assembly.GetExecutingAssembly());
            mvc.Use();

            HttpRequest request = new HttpRequest();

            request.Method = method;
            request.Path   = path;

            HttpResponse response = new HttpResponse();

            RouteMatch match = mvc.RouteFinder.Find(mvc.Routes, request);

            Assert.NotNull(match);

            HorseController controller = mvc.ControllerFactory.CreateInstance(mvc,
                                                                              match.Route.ControllerType,
                                                                              request,
                                                                              response,
                                                                              mvc.ServiceProvider.CreateScope());

            MvcConnectionHandler handler = new MvcConnectionHandler(mvc, null);

            var parameters            = (await handler.FillParameters(request, match)).Select(x => x.Value).ToArray();
            Task <IActionResult> task = (Task <IActionResult>)match.Route.ActionType.Invoke(controller, parameters);

            IActionResult result = task.Result;
            string        url    = Encoding.UTF8.GetString(((MemoryStream)result.Stream).ToArray());

            url.Should().Be(aResult);
        }
Пример #2
0
        public void Run()
        {
            HorseMvc mvc = new HorseMvc();

            HomeController cont = new HomeController();

            Assert.NotNull(cont);

            mvc.Init();
            Assembly asm = Assembly.GetExecutingAssembly();

            mvc.CreateRoutes(asm);
            mvc.Use();

            HorseServer server = new HorseServer(ServerOptions.CreateDefault());

            server.UseMvc(mvc, HttpOptions.CreateDefault());
            server.Start(47442);
            System.Threading.Thread.Sleep(1000);

            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = client.GetAsync("http://127.0.0.1:47442/home/get").Result;

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Пример #3
0
        static void Main(string[] args)
        {
            HorseMvc mvc = new HorseMvc();
            HorseServer
                server = new HorseServer();

            mvc.Init();
            server.UseMvc(mvc, HttpOptions.CreateDefault());
            server.Run();
        }
Пример #4
0
        static void Main(string[] args)
        {
            HorseMvc mvc = new HorseMvc();

            mvc.IsDevelopment = false;
            mvc.Init(services =>
            {
                services.AddScoped <IScopedService, ScopedService>();
                services.AddTransient <IFirstService, FirstService>();
                services.AddTransient <ISecondService, SecondService>();

                services.AddJwt(mvc, options =>
                {
                    options.Key              = "Very_very_secret_key";
                    options.Issuer           = "localhost";
                    options.Audience         = "localhost";
                    options.Lifetime         = TimeSpan.FromHours(1);
                    options.ValidateAudience = false;
                    options.ValidateIssuer   = false;
                    options.ValidateLifetime = true;
                });

                mvc.Policies.Add(Policy.RequireRole("Admin", "Admin"));
                mvc.Policies.Add(Policy.RequireClaims("IT", "Database", "Cloud", "Source"));
                mvc.Policies.Add(Policy.Custom("Custom", (d, c) => true));

                mvc.StatusCodeResults.Add(HttpStatusCode.Unauthorized, new JsonResult(new { Message = "Access denied" }));

                mvc.ErrorHandler = new MvcErrorHandler();
            });

            CorsMiddleware cors = new CorsMiddleware();

            cors.AllowAll();

            mvc.Use(app =>
            {
                app.UseActionRoute("/test-action", request => new StringResult("Hello, Test Action!"));

                app.UseMiddleware(cors);
                app.UseMiddleware <TMid>();
                app.UseFiles("/download", "/home/mehmet/files");
            });

            HorseServer server = new HorseServer();
            var         opt    = HttpOptions.CreateDefault();

            opt.HttpConnectionTimeMax = 0;
            server.UseMvc(mvc, opt);
            server.Run(4410);
        }
Пример #5
0
        /// <summary>
        /// Adds JWT Implementation to Horse MVC
        /// </summary>
        public static HorseMvc AddJwt(this IServiceCollection services, HorseMvc horse, Action <JwtOptions> options)
        {
            JwtOptions jwtOptions = new JwtOptions();

            options(jwtOptions);

            jwtOptions.SigningKey          = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.Key));
            horse.ClaimsPrincipalValidator = new JwtClaimsPrincipalValidator(jwtOptions);

            services.AddSingleton(jwtOptions);
            services.AddScoped <IJwtProvider, JwtProvider>();

            return(horse);
        }
Пример #6
0
        static void Main(string[] args)
        {
            HorseServer server = new HorseServer();

            HorseMvc mvc = new HorseMvc();

            mvc.Init(services => { });

            mvc.Use(app =>
            {
                IServiceProvider provider = app.GetProvider();
            });

            server.UseMvc(mvc);
            server.Run(5000);
        }
        /// <summary>
        /// Checks Policy property.
        /// If it's not empty and validation fails, return false.
        /// Otherwise returns true.
        /// </summary>
        private bool CheckPolicy(HorseMvc mvc, ActionDescriptor descriptor, FilterContext context)
        {
            if (string.IsNullOrEmpty(Policy))
            {
                return(true);
            }

            Policy policy = mvc.Policies.Get(Policy);

            if (policy != null)
            {
                if (!policy.Validate(descriptor, context))
                {
                    context.Result = StatusCodeResult.Unauthorized();
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Verifies authority of action execution.
        /// If authorization fails, context.Result will be set to 403 or 401
        /// </summary>
        public void VerifyAuthority(HorseMvc mvc, ActionDescriptor descriptor, FilterContext context)
        {
            if (context.User == null)
            {
                context.Result = StatusCodeResult.Unauthorized();
                return;
            }

            if (!CheckPolicy(mvc, descriptor, context))
            {
                return;
            }

            if (!CheckRoles(descriptor, context))
            {
                return;
            }

            CheckClaims(descriptor, context);
        }
Пример #9
0
 public MvcAppBuilder(HorseMvc mvc)
 {
     Mvc = mvc;
 }