public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model,
                                                                   string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                ExternalLoginInfo info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new User {
                    UserName = model.UserName
                };
                IdentityResult result = await UserManager.CreateAsync(user);


                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, false);

                        int code = new Random().Next(0, 10000);

                        //user.Phones.Add(new UserPhone()
                        //{
                        //    ID = Guid.NewGuid(),
                        //    Confirm = false,
                        //    ConfirmCode = code,
                        //    DateAdded = DateTime.Now,
                        //    Phone = new Phone()
                        //    {
                        //        ID = Guid.NewGuid(),
                        //        Number = model.Phone,
                        //    }
                        //});

                        _context.SaveChanges();

                        var assistent = new SmsAssistent();
                        await assistent.SendMessageAsync(model.Phone, string.Format("Ваш проверочный код {0}", code));

                        return(RedirectToAction("Confirm", "Account"));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
        public ActionResult Create(string id)
        {
            string number = id.Replace("-", "").Replace(" ", "");

            if (number.StartsWith("80"))
            {
                var regex = new Regex(Regex.Escape("80"));
                number = regex.Replace(number, "+375", 1);
            }
            if (number.StartsWith("+") == false)
            {
                number = "+" + number;
            }


            Phone phone     = _db.Phones.SingleOrDefault(x => x.Number == number);
            int   code      = new Random().Next(0, 10000);
            var   assistent = new SmsAssistent();

            if (phone == null)
            {
                Guid uid = Guid.NewGuid();

                _db.Phones.Add(new Phone
                {
                    ID        = Guid.NewGuid(),
                    Number    = number,
                    UserPhone =
                        new UserPhone
                    {
                        ID          = Guid.NewGuid(),
                        Confirm     = false,
                        ConfirmCode = code,
                        DateAdded   = DateTime.Now,
                        User        = new UserInformation
                        {
                            ID = uid
                        }
                    }
                });

                _db.SaveChanges();

                assistent.SendMessage(number, string.Format("Ваш проверочный код {0}", code));

                return(RedirectToAction("Edit", "Register", new { id = uid }));
            }

            Guid idd  = phone.UserPhone.User.ID;
            int  next = new Random().Next(0, 10000);

            phone.UserPhone.ConfirmCode = next;

            _db.SaveChanges();
            assistent.SendMessage(id, string.Format("Ваш проверочный код {0}", next));

            if (phone.UserPhone.Confirm)
            {
                return(RedirectToAction("Reset", "Register", new { id = idd }));
            }

            return(RedirectToAction("Edit", "Register", new { id = idd }));
        }