public ActionResult LogOn() { var openid = new OpenIdRelyingParty(); IAuthenticationResponse response = openid.GetResponse(); if (response != null) { var fetchResponse = response.GetExtension<FetchResponse>(); switch (response.Status) { case AuthenticationStatus.Authenticated: var claimedIdentifier = response.ClaimedIdentifier; User user = DocumentSession.Query<User>("UserByClaimedIdentifier").SingleOrDefault(u => u.ClaimedIdentifier.Equals(claimedIdentifier)); if (user == null) { user = new User { FullName = fetchResponse.GetAttributeValue(WellKnownAttributes.Name.FullName), Email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email), ClaimedIdentifier = claimedIdentifier }; DocumentSession.Store(user); } LoginUser(user); if (string.IsNullOrEmpty(user.FullName)) { return RedirectToAction("Details", "User"); } return RedirectToAction("Index", "Home"); case AuthenticationStatus.Canceled: ModelState.AddModelError("loginIdentifier", "Login was cancelled at the provider"); break; case AuthenticationStatus.Failed: ModelState.AddModelError("loginIdentifier", "Login failed using the provided OpenID identifier"); break; } } return View(); }
public void Should_be_able_to_find_a_user_by_claimed_identifier() { DocumentStore.ExecuteIndex(new UserByClaimedIdentifier()); using (IDocumentSession session = DocumentStore.OpenSession()) { var user = new User { ClaimedIdentifier = "*****@*****.**" }; session.Store(user); session.SaveChanges(); } using (IDocumentSession session = DocumentStore.OpenSession()) { User user = session.Query<User>("UserByClaimedIdentifier") .SingleOrDefault(u => u.ClaimedIdentifier.Equals("*****@*****.**")); user.Should().NotBeNull(); Assert.Equal("*****@*****.**", user.ClaimedIdentifier); } }
private void LoginUser(User user) { string userData = user.Serialize(); var authenticationTicket = new FormsAuthenticationTicket( 1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); string ticket = FormsAuthentication.Encrypt(authenticationTicket); var formsCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticket); Response.Cookies.Add(formsCookie); }