/// <summary> /// Gets the security question for the given User Id or Token. /// Only one has to be provided /// </summary> /// <param name="userId">The Id of the user</param> /// <param name="token">The password reset token send to the user using <see cref="M:ForgottenPassword"/></param> /// <returns>The Security Question for the user</returns> public string GetSecurityQuestion(string userId = null, string token = null) { User u = null; try { if (!string.IsNullOrWhiteSpace(userId)) { u = this.FindById(userId); } else if (!string.IsNullOrWhiteSpace(token)) { string id = MachineKeyEncryption.Decrypt(token); id = id.Substring(0, id.IndexOf("|")); u = this.FindById(id); } if (u == null) { throw this.manager.MessageHandler.GetError(ErrorCodes.USER_UNKNOWN); } Logger.Audit(new Audit(Actions.GET_SECURITY_QUESTION, AuditEventType.READ, u)); return(u != null ? u.SecurityQuestion : null); } catch (Exception ex) { Logger.Audit(new Audit(Actions.GET_SECURITY_QUESTION, AuditEventType.READ, typeof(User), "Id", userId, false, ex.Message)); throw ex; } }
/// <summary> /// Decrypts the string encrypted with <see cref="M:EncryptAnonymous"/> and puts the found data in the userId, questionniareName and formatName references /// </summary> /// <param name="anonymous">The string to decrypt</param> /// <param name="patientId">The found id of the user</param> /// <param name="questionnaireName">The found name of the questionniare</param> /// <param name="formatName">The found format name</param> /// <param name="episodeId">The episode ID to fill if it is there</param> private void DecryptAnonymous(string anonymous, ref string patientId, ref string questionnaireName, ref string formatName, ref int?episodeId) { XElement element = XElement.Parse(MachineKeyEncryption.Decrypt(anonymous)); foreach (XNode c in element.Nodes()) { XElement e = (XElement)c; switch (e.Name.LocalName) { case "p": patientId = e.Value; break; case "q": questionnaireName = e.Value; break; case "f": formatName = e.Value; break; case "e": episodeId = int.Parse(e.Value); break; } } }
/// <summary> /// Decrypts the given string into a SessionData object /// </summary> /// <param name="encryptedString">The encrypted string to decrypt</param> /// <returns>The SessionData instance</returns> public static SessionData Decrypt(string encryptedString) { string decrypted = MachineKeyEncryption.Decrypt(encryptedString); XmlSerializer xmlSerializer = new XmlSerializer(typeof(SessionData)); StringReader reader = new StringReader(decrypted); return(xmlSerializer.Deserialize(reader) as SessionData); }
/// <summary> /// Initializes a new instance of the <see cref="WcfUserSessionSecurity"/> class /// Continues with a existing session by the user (Or no session if the Session Id if empty) /// </summary> /// <param name="header">The Request header belonging to this session</param> internal protected WcfUserSessionSecurity(RequestHeader header) { this.sessionId = header.SessionId; string userId = MachineKeyEncryption.Decrypt(this.sessionId); try { this.InternalUser = WcfUserSessionSecurity.UserManager.Find(userId /*ticket.UserData*/); this.SessionData = WcfUserSessionSecurity.SessionStore.GetSessionData(this.sessionId); this.SessionData.LastRequestHeader = header; WcfUserSessionSecurity.SessionStore.UpdateSessionData(this.sessionId, this.SessionData); } catch (Exception) { this.InternalUser = null; this.SessionData = new SessionData(); this.SessionData.LastRequestHeader = header; } }
/// <summary> /// Loads all the session details from the persistant store file and fills the <see cref="WcfUserClient.sessionDetails"/> /// </summary> private void LoadSessionDetails() { lock (ClientSessionStore.savingLock) { if (this.sessionDetails != null) { return; } this.sessionDetails = new ConcurrentDictionary <string, ClientSessionData>(); // Open the file containing the data that you want to deserialize. FileInfo file = new FileInfo(this.persistantStoreLocation); if (!file.Exists || file.Length == 0) { this.securedData = new Dictionary <string, List <SecuredClientsessionData> >(); return; } FileStream fs = file.OpenRead(); // new FileStream(WcfUserClientSession.PersistantStoreLocation, FileMode.Open); try { BinaryReader binReader = new BinaryReader(fs); string xmlData = MachineKeyEncryption.Decrypt(binReader.ReadString()); this.securedData = this.Deserialize <Dictionary <string, List <SecuredClientsessionData> > >(xmlData); } catch (SerializationException e) { Console.WriteLine("Failed to deserialize. Reason: " + e.Message); } finally { fs.Close(); } } }
/// <summary> /// Retrieves the Client Session Data for the given session ID /// </summary> /// <param name="sessionId">The Id of the session to retrieve the data for</param> /// <returns>The Client Session Data</returns> public Model.ClientSessionData GetSessionData(string sessionId) { if (this.SessionDetails.ContainsKey(sessionId)) { return(this.sessionDetails[sessionId]); } else if (this.securedData.ContainsKey(sessionId.Substring(0, ClientSessionStore.SessionIdSubstringLength))) { List <SecuredClientsessionData> d = this.securedData[sessionId.Substring(0, ClientSessionStore.SessionIdSubstringLength)]; foreach (SecuredClientsessionData scsd in d) { try { // SecuredClientsessionData scsd = this.securedData[sessionId.Substring(0, ClientSessionStore.SessionIdSubstringLength)]; string data = scsd.SecuredData; UserSessionConfiguration config = this.Deserialize <UserSessionConfiguration>(MachineKeyEncryption.Decrypt(data, sessionId)); ClientSessionData csd = new ClientSessionData() { LastTimeUpdated = scsd.LastUpdated, UserSessionConfiguration = config }; this.SessionDetails.AddOrUpdate(sessionId, csd, (k, v) => { return(csd); }); d.Remove(scsd); return(this.sessionDetails[sessionId]); } catch (Exception) { // Obviously it wasn't this one } } } return(null); }