public static MPUser GetCurrentUser() { MPUser user = (MPUser)HttpContext.Current.Session["MPUser"]; if (user != null) { return(user); } string handle = HttpContext.Current.User.Identity.Name; if (handle != null) { // user has been authenticated before (marked remember me) // TODO: move this logic to the PostAuthenticateRequest event handler MPRSession session = MPRController.StartSession(); IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle); if (users.Count == 1) { user = users[0]; user.LastLogin = DateTime.Now; MPRController.Save <MPUser>(session, user); MPRController.EndSession(session, true); HttpContext.Current.Session["MPUser"] = user; return(user); } MPRController.EndSession(session, false); } throw new InvalidUserException("The session doesn't contain a valid user"); }
public void TestGetUserByHandle() { string handle = "admin"; MPRSession session = MPRController.StartSession(); IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle); Assert.That(users.Count, Is.EqualTo(1)); MPUser user = users[0]; System.Console.WriteLine("User {0}:\n{1}\n{2}\n{3} permissions", user.Handle, user.Name, user.EMail, user.Permissions.Count); Assert.That(user.Permissions.Count, Is.EqualTo(4)); MPRController.EndSession(session, true); }
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { e.Authenticated = false; // TODO: replace with real authentication if (Login1.Password != "password") { return; } // Add user to session string handle = Login1.UserName; MPRSession session = MPRController.StartSession(); IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle); if (users.Count != 1) // Either none or a serious error { MPRController.EndSession(session, false); return; } MPUser user = users[0]; user.LastLogin = DateTime.Now; MPRController.Save <MPUser>(session, user); HttpContext.Current.Session["MPUser"] = user; MPRController.EndSession(session, true); e.Authenticated = true; log.Info(String.Format("User {0} has logged in", user.Handle)); FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet); }