protected void Application_Start() { Database.SetInitializer<GadgetEntities>(new SampleData()); GadgetEntities context = new GadgetEntities(); context.Database.Initialize(true); if (!WebSecurity.Initialized) WebSecurity.InitializeDatabaseConnection("GadgetEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true); AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); }
// GET: /Checkout/AddressAndPayment public ActionResult AddressAndPayment() { var username = User.Identity.Name; var context = new GadgetEntities(); var id = WebSecurity.CurrentUserId; var user = context.UserProfile.SingleOrDefault(u => u.UserId == id); var order = new OrderModel(); order.LastName = user.LastName; order.FirstName = user.FirstName; order.Email = user.EmailAddress; order.City = user.City; order.Country = user.Country; order.Address = user.Address; return View(order); }
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 (GadgetEntities db = new GadgetEntities()) { UserProfile user = db.UserProfile.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfile.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); }