Exemple #1
0
        public async Task <IActionResult> Register(User model, string totalPrice)
        {
            var user = new User {
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                UserName     = model.Email,
                Email        = model.Email,
                City         = model.City,
                StreetAdress = model.StreetAdress,
                PostNumber   = model.PostNumber,
                CreatedAt    = DateTime.Now,
                PhoneNumber  = model.PhoneNumber,
                Currency     = model.Currency,
                Country      = model.Country
            };

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


            if (result.Succeeded && totalPrice != null)
            {
                await SignInMgr.SignInAsync(user, isPersistent : false);

                return(RedirectToAction("SelectPaymentAndDeliveryOption", "OrderConfirmation", new { totalPrice }));
            }
            if (result.Succeeded)
            {
                await SignInMgr.SignInAsync(user, isPersistent : false);

                return(RedirectToAction("Index", "Products"));
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError("", error.Description);
            }

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> GoogleLoginCallback(string remoteError = null)
        {
            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Invalid Login Attempt: {remoteError}");
                return(RedirectToAction("Login"));
            }

            var info = await SignInMgr.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ModelState.AddModelError(string.Empty, "Error loading external login information.");
                return(RedirectToAction("Login"));
            }

            var googleEmail = info.Principal.FindFirstValue(ClaimTypes.Email);

            if (String.IsNullOrEmpty(googleEmail))
            {
                ModelState.AddModelError(string.Empty, "Could not find an email associated with your google account. Make sure your Google account has a registered email address.");
                return(RedirectToAction("Login"));
            }

            var matchingUser = Context.Users.Where(u => u.Email == googleEmail).FirstOrDefault();

            if (matchingUser != null && !Context.UserLogins.Where(u => u.UserId == matchingUser.Id).Any())
            {
                ModelState.AddModelError(string.Empty, "An account with that email address already exists.");
                return(RedirectToAction("Login"));
            }

            var result = await SignInMgr.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                return(RedirectToAction("Index", "Products"));
            }

            var user = new User
            {
                Email         = googleEmail,
                UserName      = googleEmail,
                FirstName     = info.Principal.FindFirstValue(ClaimTypes.GivenName),
                LastName      = info.Principal.FindFirstValue(ClaimTypes.Surname),
                StreetAdress  = info.Principal.FindFirstValue(ClaimTypes.StreetAddress),
                PostNumber    = info.Principal.FindFirstValue(ClaimTypes.PostalCode),
                City          = info.Principal.FindFirstValue(ClaimTypes.Locality),
                Country       = info.Principal.FindFirstValue(ClaimTypes.Country),
                Currency      = "SEK",
                PhoneNumber   = info.Principal.FindFirstValue(ClaimTypes.MobilePhone),
                Password      = null,
                SecurityStamp = Guid.NewGuid().ToString()
            };
            await UserMgr.CreateAsync(user);

            await UserMgr.AddLoginAsync(user, info);

            await SignInMgr.SignInAsync(user, false);

            return(RedirectToAction("Index", "Products"));
        }