示例#1
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var response = new MiddlewareResponse {
                Status = new APIResponseStatus {
                    IsSuccessful = false, Message = new APIResponseMessage()
                }
            };

            using (var scope = context.HttpContext.RequestServices.CreateScope())
            {
                try
                {
                    IServiceProvider   scopedServices = scope.ServiceProvider;
                    RedisCacheSettings redisSettings  = scopedServices.GetRequiredService <RedisCacheSettings>();
                    if (!redisSettings.Enabled)
                    {
                        await next();

                        return;
                    }

                    DataContext           _dataContext         = scopedServices.GetRequiredService <DataContext>();
                    IResponseCacheService responseCacheService = scopedServices.GetRequiredService <IResponseCacheService>();
                    var cacheKey = Cache.GenerateCacheKeyFromRequest(context.HttpContext.Request);

                    if (context.HttpContext.Request.Method != "GET")
                    {
                        await responseCacheService.ResetCacheAsync(cacheKey);
                    }
                    var cachedResponse = await responseCacheService.GetCacheResponseAsync(cacheKey);

                    if (!string.IsNullOrEmpty(cachedResponse))
                    {
                        var contentResult = new ContentResult
                        {
                            Content     = cachedResponse,
                            ContentType = "application/json",
                            StatusCode  = 200
                        };
                        context.Result = contentResult;
                        return;
                    }

                    var executedContext = await next();

                    if (executedContext.Result is OkObjectResult okObjectResult)
                    {
                        await responseCacheService.CatcheResponseAsync(cacheKey, okObjectResult, TimeSpan.FromSeconds(1000));

                        context.HttpContext.Response.StatusCode = 200;
                        context.Result = new OkObjectResult(okObjectResult);
                        return;
                    }
                    await next();
                }
                catch (Exception ex)
                {
                    context.HttpContext.Response.StatusCode  = 500;
                    response.Status.IsSuccessful             = false;
                    response.Status.Message.FriendlyMessage  = ex.Message;
                    response.Status.Message.TechnicalMessage = ex.ToString();
                    context.Result = new InternalServerErrorObjectResult(response);
                    return;
                }
            }
        }
        public async Task <Responder> Handle(AuthSettupCreate request, CancellationToken cancellationToken)
        {
            var response = new Responder {
                Status = new APIResponseStatus {
                    IsSuccessful = true, Message = new APIResponseMessage()
                }
            };

            try
            {
                var domain = _security.ScrewIdentifierGrid.FirstOrDefault(t => t.Media == request.Media && request.Module == t.Module);
                var other  = _security.ScrewIdentifierGrid.FirstOrDefault(t => t.Media != request.Media && request.Module == t.Module);

                domain.ActiveOnMobileApp                = request.ActiveOnMobileApp;
                domain.ActiveOnWebApp                   = request.ActiveOnWebApp;
                domain.ActiveDirectory                  = request.ActiveDirectory;
                domain.EnableLoginFailedLockout         = request.EnableLoginFailedLockout;
                domain.InActiveSessionTimeout           = TimeSpan.FromMinutes(request.InActiveSessionTimeout);
                domain.NumberOfFailedLoginBeforeLockout = request.NumberOfFailedLoginBeforeLockout;
                domain.PasswordUpdateCycle              = request.PasswordUpdateCycle;
                domain.SecuritySettingActiveOnMobileApp = request.SecuritySettingActiveOnMobileApp;
                domain.SecuritySettingsActiveOnWebApp   = request.SecuritySettingsActiveOnWebApp;
                domain.ShouldAthenticate                = request.ShoulAthenticate;
                domain.UseActiveDirectory               = request.UseActiveDirectory;
                domain.ShouldRetryAfterLockoutEnabled   = request.ShouldRetryAfterLockoutEnabled;
                domain.RetryTimeInMinutes               = TimeSpan.FromMinutes(request.RetryTimeInMinutes);
                domain.EnableRetryOnMobileApp           = request.EnableRetryOnMobileApp;
                domain.EnableRetryOnWebApp              = request.EnableRetryOnWebApp;
                domain.EnableLoadBalance                = request.EnableLoadBalance;
                domain.LoadBalanceInHours               = request.LoadBalanceInHours;

                _security.Entry(domain).CurrentValues.SetValues(domain);

                other.ActiveDirectory                  = request.ActiveDirectory;
                other.EnableLoginFailedLockout         = request.EnableLoginFailedLockout;
                other.InActiveSessionTimeout           = TimeSpan.FromMinutes(request.InActiveSessionTimeout);
                other.NumberOfFailedLoginBeforeLockout = request.NumberOfFailedLoginBeforeLockout;
                other.PasswordUpdateCycle              = request.PasswordUpdateCycle;
                other.SecuritySettingActiveOnMobileApp = request.SecuritySettingActiveOnMobileApp;
                other.SecuritySettingsActiveOnWebApp   = request.SecuritySettingsActiveOnWebApp;
                other.UseActiveDirectory               = request.UseActiveDirectory;
                other.ShouldRetryAfterLockoutEnabled   = request.ShouldRetryAfterLockoutEnabled;
                other.RetryTimeInMinutes               = TimeSpan.FromMinutes(request.RetryTimeInMinutes);
                other.EnableRetryOnMobileApp           = request.EnableRetryOnMobileApp;
                other.EnableRetryOnWebApp              = request.EnableRetryOnWebApp;
                other.EnableLoadBalance                = request.EnableLoadBalance;
                other.LoadBalanceInHours               = request.LoadBalanceInHours;

                _security.Entry(other).CurrentValues.SetValues(other);

                using (var trans = await _security.Database.BeginTransactionAsync())
                {
                    try
                    {
                        if (domain.Module == (int)Modules.CENTRAL)
                        {
                            var allUser = await _security.Users.ToListAsync();

                            if (allUser.Count() > 0)
                            {
                                foreach (var user in allUser)
                                {
                                    user.NextPasswordChangeDate = DateTime.UtcNow.AddDays(domain.PasswordUpdateCycle);
                                }
                            }
                        }

                        await _security.SaveChangesAsync();

                        await trans.CommitAsync();
                    }
                    catch (Exception e)
                    {
                        await trans.RollbackAsync();

                        response.Status.Message.FriendlyMessage = e?.Message;
                        response.Status.IsSuccessful            = false;
                        return(response);
                    }
                    finally { await trans.DisposeAsync(); }
                }

                var authsetting = await _security.ScrewIdentifierGrid.ToListAsync();

                await _cacheService.ResetCacheAsync(CacheKeys.AuthSettings);

                await _cacheService.CatcheResponseAsync(CacheKeys.AuthSettings, authsetting, TimeSpan.FromSeconds(3600));

                response.ResponderId         = domain.ScrewIdentifierGridId;
                response.Status.IsSuccessful = true;
                return(response);
            }
            catch (Exception ex)
            {
                return(response);
            }
        }