public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
예제 #2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    CheckChanceState();
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    string MongoDirectory = "";
                    if (model.Image != null)
                    {
                        var filename = Path.GetFileName((model.Image.FileName));
                        var path = Path.Combine(Server.MapPath("~/Images"), filename);
                        MongoDirectory = String.Format("{0}{1}", @"\Images\", filename);
            //C:\Users\mohsen\Desktop\BOTF\BOTF\Content\images\no-icon.jpg
                        model.Image.SaveAs(path); //save image in images
                    }
                    else
                    {
                        MongoDirectory = Url.Content("~/Content/images/no-icon.jpg"); //if not image is chosen use a default one
                    }

                    ContextDb _db = new ContextDb();
                    UsersContext db = new UsersContext();
                    //set up a new user
                    int Id = db.UserProfiles.FirstOrDefault(c=>c.UserName == model.UserName).UserId;
                    _db.User.Add(new User { UserId = Id, Email = model.Email, Name = model.Name, RemainingProposals = 1, RemainingVotes = 3, Image = MongoDirectory });
                    _db.SaveChanges();
                    CheckChanceState(Id);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
예제 #3
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);
        }