Exemplo n.º 1
0
        public async Task <ActionResult> ValidateLogin(LoginInfo loginInfo)
        {
            string             msg          = string.Empty;
            WebResponseContent responseData = new WebResponseContent();

            try
            {
                responseData = loginInfo.ValidationEntity();
                if (!responseData.Status)
                {
                    return(Json(responseData));
                }
                responseData.Status = false;
                var PINCode = TempData[VerificationCode];
                if (PINCode == null ||
                    loginInfo.VerificationCode.ToLower() != PINCode.ToString().ToLower())
                {
                    return(Json(responseData.Set(ResponseType.PINError, false)));
                }

                Sys_User user = await Task.Run(() => sys_UserRepository.Find(x => x.UserName == loginInfo.UserName).FirstOrDefault());

                if (user == null || loginInfo.PassWord.Trim() != (user.UserPwd ?? "").DecryptDES(AppSetting.Secret.User))
                {
                    responseData.Set(ResponseType.LoginError);
                    return(Json(responseData));
                }

                ClaimsIdentity claimIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                claimIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.User_Id.ToString()));
                claimIdentity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
                claimIdentity.AddClaim(new Claim(ClaimTypes.Surname, user.UserTrueName));
                claimIdentity.AddClaim(new Claim(ClaimTypes.Role, user.Role_Id.ToString()));
                claimIdentity.AddClaim(new Claim("RoleName", user.RoleName ?? ""));
                await HttpContext.SignInAsync(
                    new ClaimsPrincipal(claimIdentity),
                    new AuthenticationProperties()
                {
                    ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(60)
                });

                TempData[VerificationCode] = string.Empty;
                loginInfo.PassWord         = "";
                responseData.Set(ResponseType.LoginSuccess, true);
            }
            catch (Exception ex)
            {
                msg = ex.Message + ex.StackTrace;
                responseData.Set(ResponseType.ServerError);
                return(Json(responseData));
            }
            finally
            {
                Logger.Info(LoggerType.Login, loginInfo.Serialize(), responseData.Message, msg);
            }
            return(Json(responseData));
        }