Esempio n. 1
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {

                        // Insert name into the profile table
                       UserProfile profile = db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
                       //need to check to see if it is facebook or twitter
                        if (provider == "twitter")
                        {

                            if (Session["AccessToken"] != null && Session["AccessTokenSecret"] != null) //used to distinugish between facebook and twitter regisration
                            {
                                //"9kCMAgidv1NzN8TfMVgZw", "RimlGsenvejdoRlw0NSazYzXJBO6olF2IBMJcw11Uc"
                                //creating new tweetsharp service
                                TwitterService service = new TwitterService(Settings.Settings.TwitterConsumerKey, Settings.Settings.TwitterConsumerSecret, Session["AccessToken"].ToString(), Session["AccessTokenSecret"].ToString());
                                TwitterUser me = service.VerifyCredentials();
                                ContextDb _db = new ContextDb();
                                Models.User temp = new Models.User { UserId = profile.UserId, Email = model.Email, Image = me.ProfileImageUrl, Name =me.Name, RemainingProposals = 1, RemainingVotes = 3 };
                                temp = _db.User.Add(temp);
                                _db.SaveChanges();
                                CheckChanceState(temp.UserId);
                                DatabaseCallsApi _api = new DatabaseCallsApi();
                                _api.AddOrUpdateService(temp.UserId, "twitter", Session["AccessToken"].ToString(), Session["AccessTokenSecret"].ToString());
                                Session.Remove("AccessToken");
                                Session.Remove("AccessTokenSecret");
                            }

                        }
                        else
                        {
                            //setting new facebook service
                            FacebookAPI facebook = new FacebookAPI(FacebookToken);
                            dynamic facebookData = facebook.GetUsersData();
                            if (facebookData != null)
                            {

                                ContextDb _db = new ContextDb();

                                Models.User temp = new Models.User { UserId = profile.UserId, Email = facebookData.email.ToString(), Image = facebookData.picture["data"]["url"].ToString(), Name = facebookData.name.ToString(), RemainingProposals = 1, RemainingVotes = 3 };
                                temp = _db.User.Add(temp);
                                _db.SaveChanges();
                                CheckChanceState(temp.UserId);
                                DatabaseCallsApi _api = new DatabaseCallsApi();
                                _api.AddOrUpdateService(temp.UserId, Provider, FacebookToken);

                            }
                        }

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Esempio n. 2
0
        private User CreateUserIfNotExists(long id, dynamic me, string token, DateTime expireDate)
        {
            using (DataContext db = new DataContext())
            {
                lock (db) // Trying to see if this fixes race problems on initial user creation.
                {
                    var dbUser = db.Users.SingleOrDefault(u => u.FacebookId == id);

                    if (dbUser == null)
                    {
                        _log.Info("Creating User: "******" - " + me.name);

                        dbUser = new Models.User();
                        dbUser.Created = DateTime.UtcNow;
                        dbUser.FacebookId = id;
                        dbUser.Name = me.name;
                        dbUser.Email = me.email;
                        dbUser.Link = me.link;
                        dbUser.Token = token;
                        dbUser.TokenExpire = expireDate;
                        dbUser.ShareActivity = true; // We share activity inside InTheBoks by default.
                        dbUser.ShareFacebook = false; // We don't share to Facebook by default.

                        db.Users.Add(dbUser);
                        db.SaveChanges();
                    }
                    else
                    {
                        _log.Info("Updating User: "******" - " + me.name);

                        dbUser.Name = me.name;
                        dbUser.Email = me.email;
                        dbUser.Link = me.link;
                        dbUser.Token = token;
                        dbUser.TokenExpire = expireDate;

                        db.SaveChanges();
                    }

                    return dbUser;
                }
            }
        }