public ActionResult LogonTwitter() { InstantiateTwitterInMemoryTokenManager(); var client = new TwitterClient(_tokenManager, Url.Action("TwitterCallback")); client.StartAuthentication(); return null; }
public ActionResult TwitterCallback() { InstantiateTwitterInMemoryTokenManager(); var client = new TwitterClient(_tokenManager, Url.Action("TwitterCallback")); if (client.FinishAuthentication()) { // Boom we are in, get the stuff we need //client.UserName; //client.AccessToken; //client.SecretToken; using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { var doCommit = true; // See if the user has already logged in to this site using open Id var user = MembershipService.GetUserByTwitterId(client.AccessToken); var fakeEmail = string.Format("{0}@twitter.com", client.UserName); if (user == null) { // First time logging in, so need to register them as new user // password is irrelavant as they'll login using FB Id so generate random one user = new MembershipUser { // Bit shit, but twitter won't give you an email. So we do this and // Set notifications to false. Email = fakeEmail, Password = StringUtils.RandomString(8), TwitterAccessToken = client.AccessToken, IsExternalAccount = true, DisableEmailNotifications = true, UserName = _bannedWordService.SanitiseBannedWords(client.UserName), Twitter = string.Format("http://twitter.com/{0}", _bannedWordService.SanitiseBannedWords(client.UserName)) }; doCommit = ProcessSocialLogonUser(user, doCommit); } else { // Do an update to make sure we have the most recent details user.Email = fakeEmail; user.MiscAccessToken = client.AccessToken; TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Members.NowLoggedIn"), MessageType = GenericMessages.success }; // Log the user in FormsAuthentication.SetAuthCookie(user.UserName, true); } if (doCommit) { try { unitOfWork.Commit(); // Only send the email if the admin is not manually authorising emails or it's pointless // CAN'T SENT TO TWITTER USERS AS WE DON'T HAVE AN EMAIL! :( // https://dev.twitter.com/discussions/1737 //SendEmailConfirmationEmail(user); return RedirectToAction("Index", "Home"); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); FormsAuthentication.SignOut(); TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.GenericMessage"), MessageType = GenericMessages.error }; } } } } // Only add this if one hasn't been added already if (TempData[AppConstants.MessageViewBagName] == null) { // Either cancelled or there was an error TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.GenericMessage"), MessageType = GenericMessages.error }; } return RedirectToAction("LogOn", "Members"); }