Esempio n. 1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email }, true);
                    Restaurant restaurant=new Restaurant(){Name = model.RestaurantName,Username = model.UserName,Active = false,Email = model.Email};
                    restaurant.OpenTimes=new List<OpenTime>();
                    restaurant.ClosedDates=new List<CloseDate>();
                    foreach (Days day in (Days[])Enum.GetValues(typeof(Days)))
            {
                OpenTime openTime = new OpenTime() { Day = day };
                restaurant.OpenTimes.Add(openTime);
            }
                    using (EFContext db=new EFContext())
                    {
                        db.Restaurants.Add(restaurant);
                        db.SaveChanges();
                    }

                    //dynamic email = new Email("RegEmail");
                    //email.To = model.Email;
                    //email.UserName = model.UserName;
                    //email.ConfirmationToken = confirmationToken;
                    //email.Send();

                    var mail = new MailMessage("*****@*****.**", model.Email);
                    mail.IsBodyHtml = true;

                    var subject = "Aktivering av konto";
                    var body = "<h2>Hej " + model.UserName + "</h2>!<br/>";
                    body += "För att aktivera ditt konto och registrera en restaurang, vänligen klicka på följande länk:<br/>";
                    //Länk till aktivering, typ "Acivate", "User"

                    Uri uri = System.Web.HttpContext.Current.Request.Url;
                    String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;

                    var myLink = host + "/Account/RegisterConfirmation/" + confirmationToken;

                    mail.Subject = subject;
                    mail.Body = body + Environment.NewLine  + myLink;

                    SmtpClient _client = new SmtpClient();
                    _client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

                    _client.PickupDirectoryLocation = HttpRuntime.AppDomainAppPath + "EmailContent";

                    _client.Send(mail);

                    return RedirectToAction("RegisterStepTwo", "Account");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Esempio n. 2
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 (var db = new EFContext())
                {
                    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);
        }