Example #1
0
        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
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Example #2
0
 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
Example #3
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (model.PhoneNumber.Substring(0, 3) == "049")
                model.CountryCode = "386";
            else
                model.CountryCode = "377";

            if (ModelState.IsValid)
            {
                Random generator = new Random();
                String r = generator.Next(0, 1000000).ToString("D6");

                var user = new ApplicationUser() { 
                    UserName = model.UserName, 
                    Email = model.email,
                    PhoneNumber = String.Format("+{0}{1}", model.CountryCode, model.PhoneNumber),
                    address = model.address, 
                    active = true,
                    PasswordHash = passwordHasher.HashPassword(model.Password),
                    CountryCode = model.CountryCode,
                    Name = "",
                    EmailConfirmed = false,
                    PhoneNumberConfirmed = false,
                    TwoFactorEnabled = false,
                    LockoutEnabled = false,
                    AccessFailedCount = 0,
                    AuthyUserId = r
                };
                try
                {
                    var result = await UserManager.CreateAsync(user);

                    if (result.Succeeded)
                    {
                        await UserManager.RequestPhoneNumberConfirmationTokenAsync(user.Id);

                        return RedirectToAction("VerifyRegistrationCode", new { message = ApplicationMessages.VerificationCodeSent });
                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}",
                                                    validationError.PropertyName,
                                                    validationError.ErrorMessage);
                        }
                    }
                }
                    
                

                //var result = await UserManager.CreateAsync(user, model.Password);

                return RedirectToAction("VerifyRegistrationCode", new { message = ApplicationMessages.VerificationCodeSent });

                //try
                //{

                //    string AccountSid = "ACe57456e19af8e8ed260e7e4823bf7c59";
                //    string AuthToken = "e9b900ff25402260d3b67dfb2641c30b";
                //    var twilio = new TwilioRestClient(AccountSid, AuthToken);
                //    var message = twilio.SendMessage(
                //      "+12565768040", "+38649642226",
                //      "Kodi yt eshte 123456"
                //    );

                //    Console.WriteLine(message.Sid);
                //}
                //catch { }
                

                //if (result.Succeeded)
                //{
                //    await SignInAsync(user, isPersistent: false);
                //return RedirectToAction("Index", "Home");
                //}
                //else
                //{
                //    AddErrors(result);
                //}
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }