public static UserInfo GetUserInfo(string userId, string userRole, string institutionId) { UserInfo result = new UserInfo { Role = userRole, InstitutionId = institutionId }; result.Token = CryptographicService.EncryptString(userId + "|" + userRole + "|" + institutionId); return(result); }
public async Task <bool> Register(User user) { var registeredUser = await this.repository.GetAsync <User>(true, x => x.Email == user.Email); if (registeredUser != null) { throw new Exception("Email is already registered."); } user.Password = CryptographicService.GetHashString(user.Password); await this.repository.AddAsync <User>(user); return(true); }
public async Task <UserInfo> LoginLikeClient(User user) { var client = await this.repository.GetAsync <User>(true, x => x.Email == user.Email && x.Password == CryptographicService.GetHashString(user.Password)); if (client == null) { throw new Exception("User not found"); } UserInfo userInfo = new UserInfo(); userInfo.Token = CryptographicService.EncryptString(client.Id + "|" + "user" + "|" + ""); userInfo.Role = "user"; userInfo.InstitutionId = "-1"; return(userInfo); }
public async Task <ChatTokenContainer> GetChatToken(ChatInfo сhatInfo) { var chat = await this.repository.GetAsync <Chat>(true, x => x.Id == сhatInfo.Id && x.InitiatorId == сhatInfo.InitiatorId && x.RecipientId == сhatInfo.RecipientId && x.InstitutionId == сhatInfo.InstitutionId); if (chat == null) { throw new Exception("Chat not found"); } ChatTokenContainer chatTokenContainer = new ChatTokenContainer(); chatTokenContainer.ChatToken = CryptographicService.EncryptString(chat.Id + "|" + chat.InitiatorId + "|" + chat.RecipientId + "|" + chat.InstitutionId); chatTokenContainer.ChatId = chat.Id; return(chatTokenContainer); }
public async Task <UserInfo> LoginLikeEmployee(UserInfo userInfo) { int clientId = AuthenticationService.GetUserId(userInfo.Token); var client = await this.repository.GetAsync <User>(true, x => x.Id == clientId); if (client == null) { throw new Exception("User not found"); } string role = userInfo.Role; string InstitutionId = userInfo.InstitutionId.ToString(); UserInfo newUserInfo = new UserInfo(); newUserInfo.Token = CryptographicService.EncryptString(clientId + "|" + role + "|" + InstitutionId); newUserInfo.Role = role; newUserInfo.InstitutionId = InstitutionId; return(newUserInfo); }
public int GetInstitutionIdFromToken(string token) { return(Convert.ToInt32(CryptographicService.DecryptString(token).Split("|")[3])); }
public int GetRecipientIdFromToken(string token) { return(Convert.ToInt32(CryptographicService.DecryptString(token).Split("|")[2])); }
public int GetInitiatorIdFromToken(string token) { return(Convert.ToInt32(CryptographicService.DecryptString(token).Split("|")[1])); }
public static string GetUserRole(string token) { return(CryptographicService.DecryptString(token).Split("|")[1]); }
public static int GetUserId(string token) { return(Convert.ToInt32(CryptographicService.DecryptString(token).Split("|")[0])); }