public IActionResult Login([FromBody] LoginData data) { var hasil = new OutputData() { IsSucceed = true }; var datas = (from c in db.GetAllData <UserProfile>() where c.Username == data.UserName select c).ToList(); if (datas != null && datas.Count() > 0) { if (datas[0].Password == data.Password) { hasil.Data = UserToken.GenerateToken(datas[0].Username); } else { hasil.IsSucceed = false; hasil.ErrorMessage = "wrong password"; } } else { hasil.IsSucceed = false; hasil.ErrorMessage = "user not found."; } return(Ok(hasil)); }
public UserInfo PostValidateUser(Login login) { ctx = new GASEntities(); String Email; if (login.User_Login.All(char.IsDigit)) { Email = (from u in ctx.Users where u.UserMobile == login.User_Login select u.UserEmail.ToString()).First(); } else { Email = login.User_Login; } try { String enPassword = UserToken.GetHashedPassword(login.User_Password); //Utility.EncryptPassword(Email.ToLower(), login.User_Password); var userList = (from u in ctx.Users where u.UserLogin.ToLower() == Email.ToLower() && u.Password == enPassword select new UserInfo { UserId = u.UserID, UserLogin = u.UserLogin, UserName = u.UserName, UserEmail = u.UserEmail, UserMobile = u.UserMobile, UserRole = u.Role, OrgId = (int)u.OrganizationID, OrgName = u.OrgName, AccountType = u.SolutionType } ).First(); string ip = Utility.GetIP(Request); string userAgent = Utility.GetUserAgent(Request); long tick = DateTime.UtcNow.Ticks; userList.UserToken = UserToken.GenerateToken(login.User_Login, login.User_Password, ip, userAgent, tick); return(userList); } catch (Exception ex) { Utility.log(DateTime.Now.ToShortDateString() + ": PostValidateUser :"******"Error"; return(user); } }
/// <summary> /// Refreshes the user token of a user, effectively forcing them to login again. /// </summary> /// <param name="username">The user to refresh the token of</param> internal bool RefreshToken(string username) { int userID = GetID(username); if (userID == 0) { return(false); } DateTime expiry = UserToken.GetExpiration(); string token = UserToken.GenerateToken(); storeUserToken(userID, expiry, token); return(true); }
/// <summary> /// Attemts to register a new user or authenticate an existing user with the given credentials /// </summary> /// <param name="credentials">User credentials to authenticate</param> /// <param name="register">True if registering a new user, false otherwise</param> /// <returns>UserToken if successful, null otherwise</returns> internal UserToken Authenticate(Credentials credentials, bool register) { int userID; if (register) { // Register a new user if (createUser(credentials.username, credentials.GenerateHash())) { userID = GetID(credentials.username); // User was created } else { return(null); // User was not created } } else { userID = GetID(credentials.username); if (userID == 0) { return(null); } string hash = getUserHash(userID); if (hash != null && !credentials.Verify(hash)) { return(null); // Credentials were invalid } } // Generate a new authentication token. DateTime expiry = UserToken.GetExpiration(); string token = UserToken.GenerateToken(); storeUserToken(userID, expiry, token); return(new UserToken(credentials.username, expiry, token)); }