public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus); if (createStatus == MembershipCreateStatus.Success) { FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Index(RegisterModel model) { var user = Membership.GetUser(User.Identity.Name); user.Email = model.UserName; Membership.UpdateUser(user); var profile = UserProfile.GetUserProfile(User.Identity.Name); profile.FirstName = model.FirstName; profile.LastName = model.LastName; profile.Save(); return View(model); }
public ActionResult OpenIdLogOn(string returnUrl) { var openid = new OpenIdRelyingParty(); var response = openid.GetResponse(); if (response == null) // Initial operation { try { var req = openid.CreateRequest("https://www.google.com/accounts/o8/id"); var fields = new ClaimsRequest(); var fetch = new FetchRequest(); fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); fetch.Attributes.AddRequired(WellKnownAttributes.Name.First); fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last); fields.Email = DemandLevel.Require; req.AddExtension(fetch); return req.RedirectingResponse.AsActionResult(); } catch (ProtocolException ex) { // display error by showing original LogOn view ModelState.AddModelError("", "Unable to authenticate: " + ex.Message); return View("Login"); } } else // OpenId redirection callback { // Step 2: OpenID Provider sending assertion response switch (response.Status) { case AuthenticationStatus.Authenticated: string identifier = response.ClaimedIdentifier; // var fetch = response.GetExtension<ClaimsResponse>(); // var email = fetch.Email; var fetch = response.GetExtension<FetchResponse>(); var email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); // var users = Membership.FindUsersByEmail(email); var user = Membership.GetUser(email); // OpenId lookup fails - Id doesn't exist for login - login first) if (user != null) { FormsAuthentication.SetAuthCookie(user.UserName, false); var profile = UserProfile.GetUserProfile(user.UserName); if (profile.FirstName == null || profile.FirstName == string.Empty) { profile.FirstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First); profile.LastName = fetch.GetAttributeValue(WellKnownAttributes.Name.Last); profile.Save(); } } else { // User not found create a new user const string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@£$%&"; var passwordChars = new char[8]; var rand = new Random(); var newUser = new RegisterModel { Email = email, UserName = email }; newUser.FirstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First); newUser.LastName = fetch.GetAttributeValue(WellKnownAttributes.Name.Last); for (var i = 0; i < 8; i++) { passwordChars[i] = allowedChars[rand.Next(0, allowedChars.Length)]; } newUser.Password = new string(passwordChars); newUser.ConfirmPassword = newUser.Password; //Create the user and sign in MembershipCreateStatus createStatus; Membership.CreateUser(newUser.UserName, newUser.Password, newUser.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { FormsAuthentication.SetAuthCookie(newUser.UserName, false /* createPersistentCookie */); var profile = UserProfile.GetUserProfile(newUser.UserName); profile.FirstName = newUser.FirstName; profile.LastName = newUser.LastName; profile.Save(); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); return View("Register"); } } if (!string.IsNullOrEmpty(returnUrl)) return Redirect(returnUrl); return RedirectToAction("Index", "Home"); case AuthenticationStatus.Canceled: ModelState.AddModelError("", "Authentication cancelled at google"); return View("Login"); case AuthenticationStatus.Failed: ModelState.AddModelError("", "Authentication FAILED"); return View("Login"); } } return new EmptyResult(); }
public ActionResult Index() { var user = Membership.GetUser(User.Identity.Name); var profile = UserProfile.GetUserProfile(User.Identity.Name); var reg = new RegisterModel(); reg.UserName = user.UserName; reg.Email = user.Email; reg.FirstName = profile.FirstName; reg.LastName = profile.LastName; return View(reg); }