Ejemplo n.º 1
0
        public static MidFunc UseHangfireDashboard(
            [NotNull] DashboardOptions options,
            [NotNull] JobStorage storage,
            [NotNull] RouteCollection routes,
            [CanBeNull] IOwinDashboardAntiforgery antiforgery)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }

            return
                (next =>
                 async env =>
            {
                var owinContext = new OwinContext(env);
                var context = new OwinDashboardContext(storage, options, env);

                if (!options.IgnoreAntiforgeryToken && antiforgery != null)
                {
                    context.AntiforgeryHeader = antiforgery.HeaderName;
                    context.AntiforgeryToken = antiforgery.GetToken(env);
                }

#pragma warning disable 618
                if (options.AuthorizationFilters != null)
                {
                    if (options.AuthorizationFilters.Any(filter => !filter.Authorize(owinContext.Environment)))
#pragma warning restore 618
                    {
                        owinContext.Response.StatusCode = GetUnauthorizedStatusCode(owinContext);
                        return;
                    }
                }
                else
                {
                    // ReSharper disable once LoopCanBeConvertedToQuery
                    foreach (var filter in options.Authorization)
                    {
                        if (!filter.Authorize(context))
                        {
                            owinContext.Response.StatusCode = GetUnauthorizedStatusCode(owinContext);
                            return;
                        }
                    }

                    foreach (var filter in options.AsyncAuthorization)
                    {
                        if (!await filter.AuthorizeAsync(context))
                        {
                            owinContext.Response.StatusCode = GetUnauthorizedStatusCode(owinContext);
                            return;
                        }
                    }
                }

                if (!options.IgnoreAntiforgeryToken && antiforgery != null && !antiforgery.ValidateRequest(env))
                {
                    owinContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                    return;
                }

                var findResult = routes.FindDispatcher(owinContext.Request.Path.Value);

                if (findResult == null)
                {
                    await next(env);

                    return;
                }

                context.UriMatch = findResult.Item2;

                await findResult.Item1.Dispatch(context);
            });
        }
        public static MidFunc UseHangfireDashboard(
            [NotNull] DashboardOptions options,
            [NotNull] JobStorage storage,
            [NotNull] RouteCollection routes,
            [CanBeNull] IOwinDashboardAntiforgery antiforgery)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }

            return
                (next =>
                 env =>
            {
                var owinContext = new OwinContext(env);
                var context = new OwinDashboardContext(storage, options, env);

                if (!options.IgnoreAntiforgeryToken && antiforgery != null)
                {
                    context.AntiforgeryHeader = antiforgery.HeaderName;
                    context.AntiforgeryToken = antiforgery.GetToken(env);
                }

#pragma warning disable 618
                if (options.AuthorizationFilters != null)
                {
                    if (options.AuthorizationFilters.Any(filter => !filter.Authorize(owinContext.Environment)))
#pragma warning restore 618
                    {
                        return Unauthorized(owinContext);
                    }
                }
                else
                {
                    if (options.Authorization.Any(filter => !filter.Authorize(context)))
                    {
                        return Unauthorized(owinContext);
                    }
                }

                if (!options.IgnoreAntiforgeryToken && antiforgery != null && !antiforgery.ValidateRequest(env))
                {
                    return Unauthorized(owinContext);
                }

                var findResult = routes.FindDispatcher(owinContext.Request.Path.Value);

                if (findResult == null)
                {
                    return next(env);
                }

                context.UriMatch = findResult.Item2;

                return findResult.Item1.Dispatch(context);
            });
        }