public async Task<SessionInformation> getCurrentSession() { var request = System.Web.HttpContext.Current.Request; var response = System.Web.HttpContext.Current.Response; if(request.Cookies["session"] == null) { //No session cookie var thisSessionInformation = new SessionInformation(); thisSessionInformation.SessionID = Guid.NewGuid().ToString(); response.AppendCookie(new HttpCookie("session", thisSessionInformation.SessionID)); //TODO: Create new user UserInformation info = new UserInformation(); await db.GetCollection<UserInformation>("users").InsertOneAsync(info); thisSessionInformation.User = info.Id; await db.GetCollection<SessionInformation>("sessions").InsertOneAsync(thisSessionInformation); return thisSessionInformation; } return (await (await db.GetCollection<SessionInformation>("sessions").FindAsync(Builders<SessionInformation>.Filter.Eq(m => m.SessionID, request.Cookies["session"].Value))).ToListAsync()).First(); }
public async Task CreateUser(RegistrationScreen screen) { using (RandomNumberGenerator mrand = RandomNumberGenerator.Create()) { byte[] salt = new byte[32]; mrand.GetBytes(salt); using (Rfc2898DeriveBytes mderive = new Rfc2898DeriveBytes(screen.Password,salt)) { byte[] hash = mderive.GetBytes(32); UserInformation info = new UserInformation() { FirstName = screen.FirstName, LastName = screen.LastName, IpAddress = HttpContext.Current.Request.UserHostName, LastAccessTime = DateTime.Now }; info.Password = hash; info.Salt = salt; await db.GetCollection<UserInformation>("users").InsertOneAsync(info); } } }