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 LostPassword(LostPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipUser user;
                var artistUser = new Artist();
                using (var context = new UsersContext())
                {
                    var userProfile = context.UserProfiles.Where(u => u.UserName == model.Username).FirstOrDefault();

                    if (userProfile != null && !string.IsNullOrEmpty(userProfile.UserName))
                    {
                        user = Membership.GetUser(userProfile.UserName);

                        try
                        {
                            // get local user for their email
                            var trmwebservice = new WebService.WCFWebServiceJson();
                            artistUser = trmwebservice.GetArtist(userProfile.UserId);
                        }
                        catch
                        {
                            ModelState.AddModelError("", "No artist found by that user name.");

                            return View(model);
                        }
                    }
                    else
                    {
                        user = null;
                    }
                }
                if (user != null && artistUser != null)
                {
                    try
                    {
                        // Generate password token that will be used in the email link to authenticate user
                        var token = WebSecurity.GeneratePasswordResetToken(user.UserName);
                        // Generate the html link sent via emailModelState.AddModelError("", "There was an issue sending email: " + e.Message);
                        StringBuilder resetLink = new StringBuilder();
                        resetLink.Append(Url.Action("ResetPassword", "Account", new { rt = token }, "http"));
                        resetLink.AppendLine(Environment.NewLine);
                        resetLink.AppendLine("If the link does not work, please copy and paste it in your browser.");
                        resetLink.AppendLine(Environment.NewLine);
                        resetLink.AppendLine("The team at PlayLift Ltd.");

                        // Email stuff
                        string subject = "PlayLift - Reset your password for " + artistUser.ArtistName;
                        string body = "Reset password link: " + resetLink;
                        string from = "*****@*****.**";

                        MailMessage message = new MailMessage(from, artistUser.Email);
                        message.Subject = subject;
                        message.Body = body;
                        SmtpClient client = new SmtpClient("auth.smtp.1and1.co.uk");
                        client.Credentials = new NetworkCredential("*****@*****.**", "trm_info");

                        // Attempt to send the email
                        try
                        {
                            client.Send(message);
                        }
                        catch (Exception e)
                        {
                            ModelState.AddModelError("", "There was an issue sending email: " + e.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", "We cannot reset your password because: " + ex.Message + " If you have registered with a social network, please reset your password with the provider.");
                    }

                }
                else // Email not found
                {
                    /* Note: You may not want to provide the following information
                    * since it gives an intruder information as to whether a
                    * certain email address is registered with this website or not.
                    * If you're really concerned about privacy, you may want to
                    * forward to the same "Success" page regardless whether an
                    * user was found or not. This is only for illustration purposes.
                    */
                    ModelState.AddModelError("", "No user found by that user name.");

                    return View(model);
                }
            }

            /* You may want to send the user to a "Success" page upon the successful
            * sending of the reset email link. Right now, if we are 100% successful
            * nothing happens on the page. :P
            */
            return RedirectToAction("ResetLinkSent");
        }
        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 (!model.TermsAndConditions)
            {
                ModelState.AddModelError("TermsAndConditions", "You must agree to the terms and conditions to register.");
            }

            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();

                        var artist = new Artist
                        {
                            UserName = model.UserName,
                            UserType = DomainModel.Entities.User.UserTypeList.Artist,
                            TermsAndConditionsAccepted = model.TermsAndConditions,
                        };

                        var trmwebservice = new WebService.WCFWebServiceJson();
                        if (trmwebservice.RegisterArtistSocial(artist, provider, providerUserId))
                        {
                            OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
                            return RedirectToAction("RegisterSuccess", "Account");
                        }
                        else
                        {
                            ModelState.AddModelError("ArtistRegistrationError", "There was an issue registering you. If the problemt persists, please contact us at [email protected]");
                        }
                    }
                    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);
        }