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); } }
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 db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); 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); }
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 db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); //Add user info to member table (storage) if (Session["extradata"] != null) { IDictionary<string, string> extraData = (IDictionary<string, string>) Session["extradata"]; //Save profile pic int userId = WebSecurity.GetUserId(model.UserName); try { string picPath = extraData["picture"]; using (WebClient client2 = new WebClient()) { var dirUpload = Server.MapPath("./Uploads/" + userId + "/"); Directory.CreateDirectory(dirUpload); var path = Path.Combine(dirUpload, "temp.jpg"); client2.DownloadFile(picPath, path); ADB.UploadBlob("pf_" + userId, path); DirectoryInfo directory = new DirectoryInfo(dirUpload); foreach (FileInfo file2 in directory.GetFiles()) file2.Delete(); directory.Delete(true); } } catch (Exception e) { } //Get gender string gender = ""; if (extraData.ContainsKey("gender")) { gender = extraData["gender"]; if ("male".Equals(gender)) { gender = "M"; } else if ("female".Equals(gender)) { gender = "F"; } } string birthday = ""; if (extraData.ContainsKey("birthday")) { birthday = extraData["birthday"]; } string email = ""; if (extraData.ContainsKey("email")) { email = extraData["email"]; } string location = ""; if (extraData.ContainsKey("location")) { location = extraData["location"]; } string worklocation = ""; if (extraData.ContainsKey("work")) { worklocation = extraData["work"]; } string name = ""; if (extraData.ContainsKey("name")) { name = extraData["name"]; } ADM.AddMember(userId, model.UserName, "", name, "", gender, birthday, "", email, location, "", worklocation, ""); Session.Remove("extraData"); } return RedirectToLocal(returnUrl); /* if (Session["facebooktoken"]!= null) { var client = new Facebook.FacebookClient(Session["facebooktoken"].ToString()); dynamic response = client.Get("me", new { fields = "verified,id,name,birthday,gender,email,address,picture.height(320).width(320),location,work" }); int userId = WebSecurity.GetUserId(model.UserName); try { string picPath = response["picture"]["data"]["url"]; string localFilename = "temp.jpg"; using (WebClient client2 = new WebClient()) { var dirUpload = Server.MapPath("./Uploads/" + userId + "/"); Directory.CreateDirectory(dirUpload); var path = Path.Combine(dirUpload, localFilename); client2.DownloadFile(picPath, path); ADB.UploadBlob("pf_" + userId, path); DirectoryInfo directory = new DirectoryInfo(dirUpload); foreach (FileInfo file2 in directory.GetFiles()) file2.Delete(); directory.Delete(true); } } catch (KeyNotFoundException e) { } string gender = ""; try { gender = response["gender"]; if ("male".Equals(gender)) { gender = "M"; } else if ("female".Equals(gender)) { gender = "F"; } } catch (KeyNotFoundException e) { gender = ""; } string birthday = ""; try { birthday = response["birthday"]; } catch (KeyNotFoundException e) { birthday = ""; } string email = ""; try { email = response["email"]; } catch (KeyNotFoundException e) { email = ""; } string location = ""; try { location = response["location"]["name"]; } catch (KeyNotFoundException e) { location = ""; } string worklocation = ""; try { JsonArray v = (JsonArray)response["work"]; if (v != null && v.Count > 0) { try { var t = v.First(); var employer2 = (t as IDictionary<string, object>)["employer"]; var location2 = (t as IDictionary<string, object>)["location"]; worklocation = ((JsonObject)employer2)["name"].ToString() + ", " + ((JsonObject)location2)["name"].ToString(); } catch (Exception e) { worklocation = ""; } } else { worklocation = ""; } } catch (KeyNotFoundException e) { worklocation = ""; } ADM.AddMember(userId, model.UserName, "", response["name"], "", gender, birthday, "", email, location, "", worklocation, ""); } */ } 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); }