Exemplo n.º 1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);

            if (!context.ModelState.IsValid)
            {
                string msg = "";
                var    methodIgnoreAttribute = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(false).OfType <ModelStateIgnoreAttribute>().FirstOrDefault();
                foreach (var item in context.ModelState.Where(x => x.Value.ValidationState == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid))
                {
                    if (methodIgnoreAttribute != null && methodIgnoreAttribute.Names.Contains(item.Key))
                    {
                        context.ModelState.Remove(item.Key);
                        continue;
                    }
                    msg += "\n" + item.Key;
                    foreach (var err in item.Value.Errors)
                    {
                        msg += "\n" + err.ErrorMessage;
                        if (err.Exception != null)
                        {
                            msg += "exception: " + err.Exception.Message;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(msg))
                {
                    context.Result = new BadRequestObjectResult(ServiceResult <string> .Instance.ErrorResult(ServiceResultCode.InvalidModel, "Invalid Model" + msg));
                    return;
                }
            }


            MainTokenData = AdminHelper.GetDataFromToken(GetTokenFromHeader());
        }
Exemplo n.º 2
0
        public ServiceResult <AdminLoginResponseModel> Login(string email, string password)
        {
            var response = ServiceResult <AdminLoginResponseModel> .Instance.ErrorResult(ServiceResultCodeAdmin.UserInvalidCredential);

            // TODO: uncomment on production
            //var userResult = GetBy(x => x.FirmId == firmId && x.DivisionId == divisionId && x.Email == email);
            var userResult = GetBy(x => x.Email == email);

            if (!userResult.Success || userResult.Value == null || userResult.Value?.Id == 0)
            {
                return(response);
            }

            if (userResult.Value.Status != Status.Active)
            {
                return(response);
            }

            if (userResult.Value.Pass != password)
            {
                return(response);
            }

            var tokenData = new AdminTokenData
            {
                UserId  = userResult.Value.Id,
                IsSuper = userResult.Value.IsSuper
            };

            string token = AuthHelper.EncryptTicket(userResult.Value.Email, AdminHelper.SecretKey, 60 * 24 * 30, tokenData);

            return(response.SuccessResult(new AdminLoginResponseModel
            {
                Token = token,
                Name = userResult.Value.Name,
                UserId = userResult.Value.Id,
                Theme = userResult.Value.Theme,
                IsSuper = userResult.Value.IsSuper,
            }));
        }