public OperationResult<Guid> Login([FromUri]string userName, [FromUri]string password) { return Work(context => { var personInfo = context.PersonsDatabase.Search(userName); if (personInfo == null) throw new Exception("User \"" + userName + "\" not found."); if (personInfo.PasswordHash != password.GetPasswordHash()) throw new Exception("Wrong password."); var session = new Session { Id = Guid.NewGuid(), PersonId = personInfo.PersonId, BeginTime = DateTime.Now }; context.PersonsDatabase.AddSession(session); return session.Id; }); }
public void PersonsDatabaseTest() { Work(folder => { IPersonsDatabase database = new FileDatabase(folder); Assert.IsNull(database.Search("testUser")); var person1 = new PersonAuthInfo { PersonId = Guid.NewGuid(), Name = "Ivanov", PasswordHash = "пароль".GetPasswordHash() }; database.Add(person1); var person2 = database.Search("Ivanov"); Assert.AreEqual(person1.PersonId, person2.PersonId); Assert.AreEqual(person1.PasswordHash, person2.PasswordHash); var session1 = new Session { Id = Guid.NewGuid(), PersonId = person1.PersonId, BeginTime = DateTime.Now }; database.AddSession(session1); var session2 = database.GetSession(session1.Id); Assert.AreEqual(session1.Id, session2.Id); Assert.AreEqual(session1.PersonId, session2.PersonId); ((Session)session2).EndTime = DateTime.Now.AddHours(1); database.UpdateSession(session2); var session3 = database.GetSession(session1.Id); Assert.AreEqual(session2.EndTime, session3.EndTime); }); }