/// <summary> /// Creates streaming user. /// </summary> /// <param name="userName">User name string.</param> /// <param name="password">Password string.</param> /// <returns></returns> public bool CreateUser(string userName, string password) { var userNameValidation = new Regex("[a-z,A-Z,0-9,_!@#$%^&*]{4,32}", RegexOptions.Compiled | RegexOptions.Singleline); if (string.IsNullOrEmpty(userName) || !userNameValidation.IsMatch(userName, 0) || string.IsNullOrEmpty(password) || password.Length < 6) { throw new InvalidOperationException(@"Invalid username, allowed characters are 'a-z,A-Z,0-9,_!@#$%^&*' and length between 4 and 32 !"); } if (string.IsNullOrEmpty(password) || password.Length < 6) { throw new InvalidOperationException(@"Invalid password, minimum 6 characters !"); } var user = (from su in _dataContext.Get <StreamingUser>() where su.UserName == userName select su).FirstOrDefault(); if (user != null) { return(false); } user = new StreamingUser(); user.UserName = userName; user.PasswordHash = CryptographyHelper.ComputeHashSHA1($"{userName}_+_{password}{user.PasswordSalt}"); _dataContext.Add(user); _dataContext.SaveChanges(); return(true); }
/// <summary> /// Starts or continues previous media straiming session. /// </summary> /// <param name="userId">User identifier</param> /// <param name="ip">Ip address</param> /// <returns>Streaming session info object, containing playing media and milliseconds playing at.</returns> public SessionInfo StartSession(Guid userId, string ip) { var session = (from ss in _dataContext.Get <StreamingSession>() where ss.UserId == userId select ss).FirstOrDefault(); if (session == null) { session = new StreamingSession { DateStarted = DateTime.UtcNow, IsActive = true, StateInfoJson = string.Empty, UserId = userId, UserIp = ip }; _dataContext.Add(session); _dataContext.SaveChanges(); } else if (!session.IsActive) { session.IsActive = true; session.DateStarted = DateTime.UtcNow; session.DateFinished = null; _dataContext.Update(session); _dataContext.SaveChanges(); } return(new SessionInfo { SessionKey = session.Id.ToString(), PlayingMediaId = session.PlayingMediaId?.ToString() ?? string.Empty, PlayingAtMSec = (session.PlayingMediaId.HasValue ? session.PlayingAtMSec : 0) }); }