Ejemplo n.º 1
0
        public async Task Invoke(HttpContext context)
        {
            if (!context.Request.Path.StartsWithSegments(_options.PathMatch,
                                                         out var matchedPath, out var remainingPath))
            {
                await _next(context);

                return;
            }

            // Update the path
            var path     = context.Request.Path;
            var pathBase = context.Request.PathBase;

            context.Request.PathBase = pathBase.Add(matchedPath);
            context.Request.Path     = remainingPath;

            try
            {
                var dashboardContext = new CapDashboardContext(_storage, _options, context);
                var findResult       = _routes.FindDispatcher(context.Request.Path.Value);

                if (findResult == null)
                {
                    await _next.Invoke(context);

                    return;
                }

                foreach (var authorizationFilter in _options.Authorization)
                {
                    var authenticateResult = await authorizationFilter.AuthorizeAsync(dashboardContext);

                    if (authenticateResult)
                    {
                        continue;
                    }

                    var isAuthenticated = context.User?.Identity?.IsAuthenticated;

                    context.Response.StatusCode = isAuthenticated == true
                        ? (int)HttpStatusCode.Forbidden
                        : (int)HttpStatusCode.Unauthorized;

                    return;
                }

                dashboardContext.UriMatch = findResult.Item2;

                await findResult.Item1.Dispatch(dashboardContext);
            }
            finally
            {
                context.Request.PathBase = pathBase;
                context.Request.Path     = path;
            }
        }
Ejemplo n.º 2
0
        public Task Invoke(HttpContext context)
        {
            if (!context.Request.Path.StartsWithSegments(_options.PathMatch,
                                                         out var matchedPath, out var remainingPath))
            {
                return(_next(context));
            }

            // Update the path
            var path     = context.Request.Path;
            var pathBase = context.Request.PathBase;

            context.Request.PathBase = pathBase.Add(matchedPath);
            context.Request.Path     = remainingPath;

            try
            {
                var dashboardContext = new CapDashboardContext(_storage, _options, context);
                var findResult       = _routes.FindDispatcher(context.Request.Path.Value);

                if (findResult == null)
                {
                    return(_next.Invoke(context));
                }

                if (_options.Authorization.Any(filter => !filter.Authorize(dashboardContext)))
                {
                    var isAuthenticated = context.User?.Identity?.IsAuthenticated;

                    context.Response.StatusCode = isAuthenticated == true
                        ? (int)HttpStatusCode.Forbidden
                        : (int)HttpStatusCode.Unauthorized;

                    return(Task.CompletedTask);
                }

                dashboardContext.UriMatch = findResult.Item2;

                return(findResult.Item1.Dispatch(dashboardContext));
            }
            finally
            {
                context.Request.PathBase = pathBase;
                context.Request.Path     = path;
            }
        }
Ejemplo n.º 3
0
        public async Task Invoke(HttpContext context)
        {
            if (!context.Request.Path.StartsWithSegments(_options.PathMatch,
                                                         out var matchedPath, out var remainingPath))
            {
                await _next(context);

                return;
            }

            var userLanguages = context.Request.Headers["Accept-Language"].ToString();

            Strings.Culture = userLanguages.Contains("zh-") ? new CultureInfo("zh-CN") : new CultureInfo("en-US");

            // Update the path
            var path     = context.Request.Path;
            var pathBase = context.Request.PathBase;

            context.Request.PathBase = pathBase.Add(matchedPath);
            context.Request.Path     = remainingPath;

            try
            {
                var dashboardContext = new CapDashboardContext(_storage, _options, context);
                var findResult       = _routes.FindDispatcher(context.Request.Path.Value);

                if (findResult == null)
                {
                    await _next.Invoke(context);

                    return;
                }

                foreach (var authorizationFilter in _options.Authorization)
                {
                    var authenticateResult = await authorizationFilter.AuthorizeAsync(dashboardContext);

                    if (authenticateResult)
                    {
                        continue;
                    }

                    var isAuthenticated = context.User?.Identity?.IsAuthenticated;

                    if (_options.UseChallengeOnAuth)
                    {
                        await context.ChallengeAsync(_options.DefaultChallengeScheme);

                        return;
                    }

                    context.Response.StatusCode = isAuthenticated == true
                        ? (int)HttpStatusCode.Forbidden
                        : (int)HttpStatusCode.Unauthorized;

                    return;
                }

                dashboardContext.UriMatch = findResult.Item2;

                await findResult.Item1.Dispatch(dashboardContext);
            }
            finally
            {
                context.Request.PathBase = pathBase;
                context.Request.Path     = path;
            }
        }