protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); bool runInitializer = Convert.ToBoolean(ConfigurationManager.AppSettings["RunDatabaseInitializer"]); if (runInitializer) { Database.SetInitializer <SweatyTShirtContext>(new SweatyTShirtInitializer()); using (SweatyTShirtContext sweatyTShirtContext = new SweatyTShirtContext()) { sweatyTShirtContext.Database.Initialize(true); } } if (!WebSecurity.Initialized) { WebSecurity.InitializeDatabaseConnection("SweatyTShirtContext", "UserProfile", "UserId", "UserName", autoCreateTables: true); } int sendEmailsInterval = Convert.ToInt32(ConfigurationManager.AppSettings["SendEmailsInterval"]); AddTask("SendEmails", sendEmailsInterval); }
//[ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl) { if (string.IsNullOrEmpty(model.Email)) { throw new ApplicationException("Missing required property model.Email"); } UserProfile userProfile = null; using (SweatyTShirtContext context = new SweatyTShirtContext()) { userProfile = context.UserProfiles.FirstOrDefault(o => !string.IsNullOrEmpty(o.Email) && o.Email.ToUpper() == model.Email.ToUpper()); } if (userProfile != null) { if (ModelState.IsValid && WebSecurity.Login(userProfile.UserName, AccountRepository.AllUsersPassword, persistCookie: model.RememberMe)) { return(RedirectToLocal(returnUrl)); } } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The email provided is not registered yet, please click the 'Register' link to register."); return(View(model)); }
//[ValidateAntiForgeryToken] 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 (SweatyTShirtContext context = new SweatyTShirtContext()) { UserProfile emailMatch = context.UserProfiles.FirstOrDefault(u => !string.IsNullOrEmpty(u.Email) && u.Email.ToLower() == model.Email.ToLower()); // Check if user already exists if (emailMatch == null) { //add the user locally. now the user can login using the login page or conintue to use their external login, //it is the user's choice. string userName = null; using (AccountRepository accountRepository = new AccountRepository(context)) { userName = accountRepository.CreateUser(model.FullName, model.Email); } OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, userName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); Session[FacebookRepository.IS_FB_AUTHENTICATED] = true; return(RedirectToLocal(returnUrl)); } else { if (emailMatch != null) { ModelState.AddModelError("Email", string.Format("A user with the email '{0}', full name '{1}' already exists, please click the 'Login' link and log in.", emailMatch.Email, emailMatch.FullName)); } } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return(View(model)); }
//[ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { if (string.IsNullOrEmpty(model.Email)) { throw new ApplicationException("Missing required property model.Email"); } UserProfile userProfile = null; using (SweatyTShirtContext context = new SweatyTShirtContext()) { userProfile = context.UserProfiles.FirstOrDefault(o => !string.IsNullOrEmpty(o.Email) && o.Email.ToUpper() == model.Email.ToUpper()); } if (userProfile == null) { // Attempt to register the user try { string userName = null; using (AccountRepository repository = new AccountRepository()) { userName = repository.CreateUser(model.FullName, model.Email); } WebSecurity.Login(userName, AccountRepository.AllUsersPassword); return(RedirectToAction("Index", "Home")); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } else { ModelState.AddModelError("", string.Format("The email address '{0}' is already registered, please click the 'Login' link to log in.", model.Email)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public SimpleMembershipInitializer() { Database.SetInitializer <SweatyTShirtContext>(null); try { using (var context = new SweatyTShirtContext()) { 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 AccountRepository(SweatyTShirtContext context) : base(context) { }
/// <summary> /// Base class will NOT create SweatyTShirtContext and will NOT /// call Dispose on sweatyTShirtContext when this object is disposed. /// </summary> /// <param name="sweatyTShirtContext"></param> public BaseRepository(SweatyTShirtContext sweatyTShirtContext) { _context = sweatyTShirtContext; _isLocalContext = false; }
/// <summary> /// Base class will create SweatyTShirtContext and will call dispose on it /// when this object is disposed. /// </summary> public BaseRepository() { _context = new SweatyTShirtContext(); _isLocalContext = true; }