public async Task <Result> Login([FromBody] AdminUserLoginRequest request) { var user = await _adminUserRepository.Query().FirstOrDefaultAsync(e => e.Name == request.Name); if (user == null) { return(Result.Fail(ResultCodes.UserNotExists)); } if (!user.Pwd.Equals(request.Pwd.ToMD5Base64())) { return(Result.Fail(ResultCodes.PasswordError)); } List <Claim> claims = new List <Claim>(); claims.Add(new Claim("id", user.Id.ToString())); claims.Add(new Claim(ClaimTypes.Role, user.Role.ToString())); var token = _tokenService.JwtToken(claims); var userRes = _mapper.Map <AdminUserResponse>(user); return(Result.Ok(new AdminLoginResponse(token, userRes))); }
public async Task <IActionResult> GetAdminLoginAuthentication([FromHeader(Name = "Grid-General-Token")] string Token, [FromBody] AdminUserLoginRequest userdetails) { try { if ((string.IsNullOrEmpty(userdetails.Email)) || (string.IsNullOrEmpty(userdetails.Password))) { LogInfo.Warning(StatusMessages.MissingRequiredFields); return(Ok(new OperationResponse { HasSucceeded = false, Message = StatusMessages.MissingRequiredFields, IsDomainValidationErrors = true })); } TokenValidationHelper tokenValidationHelper = new TokenValidationHelper(); if (!tokenValidationHelper.ValidateGenericToken(Token, _iconfiguration)) { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.TokenAuthFailed), IsDomainValidationErrors = true })); } AdminUsersDataAccess _AdminUsersDataAccess = new AdminUsersDataAccess(_iconfiguration); DatabaseResponse response = await _AdminUsersDataAccess.GetLoginAuthentication(userdetails); if (response.ResponseCode == ((int)DbReturnValue.EmailNotExists)) { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.EmailNotExists), IsDomainValidationErrors = true })); } else if (response.ResponseCode == ((int)DbReturnValue.PasswordIncorrect)) { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.PasswordIncorrect), IsDomainValidationErrors = true })); } else if (response.ResponseCode == ((int)DbReturnValue.AccountDeactivated)) { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.AccountDeactivated), IsDomainValidationErrors = true })); } else if (response.ResponseCode == ((int)DbReturnValue.AuthSuccess)) { //Authentication success var adminuser = new AdminUsers(); adminuser = (AdminUsers)response.Results; var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes("stratagile grid adminuser signin jwt hashing secret"); DatabaseResponse configResponse = ConfigHelper.GetValueByKey(ConfigKeys.CustomerTokenExpiryInDays.ToString(), _iconfiguration); int expiryDay = 0; if (configResponse.ResponseCode == (int)DbReturnValue.RecordExists) { expiryDay = int.Parse(configResponse.Results.ToString()); } var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, adminuser.AdminUserID.ToString()) }), Expires = DateTime.Now.AddDays(expiryDay), // need to check with business needs SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); DatabaseResponse tokenResponse = new DatabaseResponse(); tokenResponse = await _AdminUsersDataAccess.LogAdminUserToken(adminuser.AdminUserID, tokenString); // return basic user info (without password) and token to store client side return(Ok(new OperationResponse { HasSucceeded = true, Message = EnumExtensions.GetDescription(DbReturnValue.AuthSuccess), ReturnedObject = new LoggedInPrinciple { AdminUser = adminuser, IsAuthenticated = true, Token = tokenString } } )); } else { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.ReasonUnknown), IsDomainValidationErrors = true })); } } catch (Exception ex) { LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); return(Ok(new OperationResponse { HasSucceeded = false, Message = StatusMessages.ServerError, IsDomainValidationErrors = false })); } }
public async Task <DatabaseResponse> GetLoginAuthentication(AdminUserLoginRequest users) { try { SqlParameter[] parameters = { new SqlParameter("@Email", SqlDbType.VarChar), new SqlParameter("@Password", SqlDbType.VarChar) }; parameters[0].Value = users.Email; parameters[1].Value = new Sha2().Hash(users.Password); _DataHelper = new DataAccessHelper("Admin_AuthenticateAdminUser", parameters, _configuration); DataSet ds = new DataSet(); int result = await _DataHelper.RunAsync(ds); AdminUsers adminuser = new AdminUsers(); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0) { adminuser = (from model in ds.Tables[0].AsEnumerable() select new AdminUsers() { AdminUserID = model.Field <int>("AdminUserID"), Email = model.Field <string>("Email"), Password = model.Field <string>("Password"), Name = model.Field <string>("Name"), Role = model.Field <string>("Role"), }).FirstOrDefault(); List <Permission> permissionList = new List <Permission>(); if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0) { permissionList = (from model in ds.Tables[1].AsEnumerable() select new Permission() { RolePermission = model.Field <string>("Permission"), }).ToList(); } adminuser.Permissions = permissionList.Select(item => item.RolePermission).ToList(); } return(new DatabaseResponse { ResponseCode = result, Results = adminuser }); } catch (Exception ex) { LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); throw ex; } finally { _DataHelper.Dispose(); } }