public async Task <bool> SaveAsync(BackgroundTask task)
        {
            var document = new BackgroundTaskDocument(task);

            if (task.Id == 0)
            {
                await _repository.CreateAsync(document);

                task.Id = document.TaskId;
                return(true);
            }

            var existing = await _repository.RetrieveSingleOrDefaultAsync(x => x.TaskId == task.Id);

            if (existing == null)
            {
                await _repository.CreateAsync(document);

                _logger.Trace(() => "Creating new task with ID {Id} and handler '{Handler}'", document.Id,
                              document.Handler);
                return(true);
            }

            document.Id = existing.Id;
            _logger.Trace(() => "Updating existing task with ID {Id} and handler '{Handler}'", document.Id,
                          document.Handler);
            await _repository.UpdateAsync(existing.Id, document);

            return(true);
        }
Ejemplo n.º 2
0
Archivo: Add.cs Proyecto: qiqi545/HQ
        private static void AddHttps(this IServiceCollection services, ISafeLogger logger, SecurityOptions options)
        {
            if (options.Https.Enabled)
            {
                logger?.Trace(() => "HTTPS enabled.");

                services.AddHttpsRedirection(o =>
                {
                    o.HttpsPort          = null;
                    o.RedirectStatusCode = options.Https.Hsts.Enabled ? 307 : 301;
                });

                if (options.Https.Hsts.Enabled)
                {
                    logger?.Trace(() => "HSTS enabled.");

                    services.AddHsts(o =>
                    {
                        o.MaxAge            = options.Https.Hsts.HstsMaxAge;
                        o.IncludeSubDomains = options.Https.Hsts.IncludeSubdomains;
                        o.Preload           = options.Https.Hsts.Preload;
                    });
                }
            }
        }
Ejemplo n.º 3
0
Archivo: Add.cs Proyecto: qiqi545/HQ
 private static void AddSuperUser(this IServiceCollection services, ISafeLogger logger, SuperUserOptions options)
 {
     if (options.Enabled)
     {
         logger?.Trace(() => "SuperUser enabled.");
         services.AddDefaultAuthorization(Constants.Security.Policies.SuperUserOnly, ClaimValues.SuperUser);
     }
 }
Ejemplo n.º 4
0
        public IActionResult VerifyToken()
        {
            if (User.Identity == null)
            {
                _logger?.Trace(() => "User is unauthorized");

                return(Unauthorized());
            }

            if (User.Identity.IsAuthenticated)
            {
                _logger?.Trace(() => "{User} verified token", User.Identity.Name);

                return(Ok(new { Data = User.ProjectClaims() }));
            }

            return(Unauthorized());
        }
Ejemplo n.º 5
0
Archivo: Add.cs Proyecto: qiqi545/HQ
        private static void AddAuthentication(this IServiceCollection services, ISafeLogger logger,
                                              SecurityOptions security,
                                              SuperUserOptions superUser)
        {
            var tokens  = security.Tokens;
            var cookies = security.Cookies;
            var claims  = security.Claims;

            if (tokens.Enabled || cookies.Enabled || superUser.Enabled)
            {
                if (!tokens.Enabled && !cookies.Enabled && superUser.Enabled)
                {
                    logger?.Trace(() => "Authentication enabled for super user only.");
                }
                else
                {
                    logger?.Trace(() => "Authentication enabled.");
                }

                services.AddAuthentication(security, superUser, tokens, cookies, claims);
            }

            if (tokens.Enabled || superUser.Enabled)
            {
                services.AddAuthorization(x =>
                {
                    TryAddDefaultPolicy(services, logger, x, tokens.Scheme);
                });
            }

            if (cookies.Enabled)
            {
                services.AddAuthorization(x =>
                {
                    TryAddDefaultPolicy(services, logger, x, cookies.Scheme);
                });
            }
        }
Ejemplo n.º 6
0
Archivo: Add.cs Proyecto: qiqi545/HQ
        private static void AddCors(this IServiceCollection services, ISafeLogger logger, CorsOptions cors)
        {
            if (!cors.Enabled)
            {
                return;
            }

            logger?.Trace(() => "CORS enabled.");

            services.AddRouting(o => { });

            services.AddCors(o =>
            {
                o.AddPolicy(Constants.Security.Policies.CorsPolicy, builder =>
                {
                    builder
                    .WithOrigins(cors.Origins ?? new[] { "*" })
                    .WithMethods(cors.Methods ?? new[] { "*" })
                    .WithHeaders(cors.Headers ?? new[] { "*" })
                    .WithExposedHeaders(cors.ExposedHeaders ?? new string[0]);

                    if (cors.AllowCredentials && cors.Origins?.Length > 0 && cors.Origins[0] != "*")
                    {
                        builder.AllowCredentials();
                    }
                    else
                    {
                        builder.DisallowCredentials();
                    }

                    if (cors.AllowOriginWildcards)
                    {
                        builder.SetIsOriginAllowedToAllowWildcardSubdomains();
                    }

                    if (cors.PreflightMaxAgeSeconds.HasValue)
                    {
                        builder.SetPreflightMaxAge(TimeSpan.FromSeconds(cors.PreflightMaxAgeSeconds.Value));
                    }
                });
            });
        }