Ejemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var CheckEmail    = _dbhelpers.FindEmail(model.RegisterEmail);       //check if such mail already in db
                var CheckUserName = _dbhelpers.FindUserName(model.RegisterUsername); //check if such username already in db

                var user = new ApplicationUser                                       //create new user using identity
                {
                    UserName            = model.RegisterUsername,
                    Email               = model.RegisterEmail,
                    FirstName           = model.FirstName,
                    LastName            = model.LastName,
                    Country             = model.Country,
                    City                = model.City,
                    Gender              = model.Gender,
                    BirthDate           = model.BirthDate,
                    YourSelfDescription = model.YourSelfDescription,
                    JoinDate            = DateTime.Now,
                    LastLoginDate       = DateTime.Now,
                    EmailConfirmed      = true    //kapitowka
                };
                //check if there no such mail or username in already in use
                if (CheckEmail == null && CheckUserName == null)
                {
                    var result = await UserManager.CreateAsync(user, model.RegisterPassword);

                    if (result.Succeeded)
                    {
                        UserManager.AddToRole(user.Id, "Member"); //add new user to members group

                        // Send an email with this link (app_start identityconfig)
                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                                     new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        await UserManager.SendEmailAsync(user.Id,
                                                         "Confirm your account", "Please confirm your account by clicking <a href=\""
                                                         + callbackUrl + "\">here</a>");

                        //Message after registration
                        ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                                          + "before you can log in.";

                        return(View("Info"));
                    }
                    AddErrors(result);
                }
                else
                {
                    if (CheckEmail != null)
                    {
                        ModelState.AddModelError("", "Email is already registered.");
                    }
                    if (CheckUserName != null)
                    {
                        ModelState.AddModelError("", "Username " + model.RegisterUsername.ToLower() + " is already taken.");
                    }
                }
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }