Exemple #1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName  = model.Name,
                    Email     = model.Email,
                    CountryId = model.countryId,
                    StateId   = model.stateId,
                    CityId    = model.cityId
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var    confirmationLInk = Url.Action("ConfirmEmail", "Account", new { area = "Comman", userId = user.Id, token = token }, Request.Scheme);
                    string Ref           = "NewAccountConfirmation";
                    int    isSendSuccess = CommanFunction.SendConfirmationLink(confirmationLInk, model.Name, model.Email, Ref);
                    if (isSendSuccess == 1)
                    {
                        if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                        {
                            return(RedirectToAction("Dashboard", "Dashboard", new { Area = "User" }));
                        }
                        ViewBag.Success = "Registration successful";
                        ViewBag.Message = "Before you can Login, please confirm your email, by clicking on the confirmation link we have emailed you";
                        return(View("RegistrationSuccessful"));
                    }
                }
                // If there are any errors, add them to the ModelState object
                // which will be displayed by the validation summary tag helper
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, item.Description);
                }
                var countryList = _accountRepository.CounteryList().ToList();
                if (countryList != null)
                {
                    foreach (var country in countryList)
                    {
                        model.Countries.Add(new SelectListItem {
                            Text = country.CountryName, Value = country.CountryId.ToString()
                        });
                    }
                }
            }
            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Find the user by email
                var user = await _userManager.FindByEmailAsync(model.Email);

                // If the user is found AND Email is confirmed
                if (user != null && await _userManager.IsEmailConfirmedAsync(user))
                {
                    // Generate the reset password token
                    var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                    // Build the password reset link
                    var    passwordResetLink = Url.Action("ResetPassword", "Account", new { area = "Comman", email = model.Email, token = token }, Request.Scheme);
                    string Ref           = "PasswordResetLink";
                    int    isSendSuccess = CommanFunction.SendConfirmationLink(passwordResetLink, user.UserName, model.Email, Ref);
                    if (isSendSuccess == 1)
                    {
                        return(View("ForgotPasswordConfirmation"));
                    }
                    //Before you can Login, please reset your password, by clicking on the reset password link we have emailed you on your email-{model.Email} id.
                    // Log the password reset link
                    _logger.Log(LogLevel.Warning, passwordResetLink);

                    // Send the user to Forgot Password Confirmation view
                    return(View("ForgotPasswordConfirmation"));
                }

                // To avoid account enumeration and brute force attacks, don't
                // reveal that the user does not exist or is not confirmed
                return(View("ForgotPasswordConfirmation"));
            }

            return(View(model));
        }
Exemple #3
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            LoginViewModel loginViewModel = new LoginViewModel
            {
                ReturnUrl      = returnUrl,
                ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

            //Check any remote error from google
            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                return(View("Login", loginViewModel));
            }
            //Get External login info
            var info = await _signInManager.GetExternalLoginInfoAsync();

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

            // Get the email claim from external login provider (Google, Facebook etc)
            var             email = info.Principal.FindFirstValue(ClaimTypes.Email);
            ApplicationUser user  = null;

            if (email != null)
            {
                // Find the user
                user = await _userManager.FindByEmailAsync(email);

                // If email is not confirmed, display login view with validation error
                if (user != null && !user.EmailConfirmed)
                {
                    ModelState.AddModelError(string.Empty, "Email not confirmed yet");
                    return(View("Login", loginViewModel));
                }
            }
            //Signin with external registerd login provider
            var signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (signInResult.Succeeded)
            {
                return(LocalRedirect(returnUrl));
            }
            else
            {
                if (email != null)
                {
                    if (user == null)
                    {
                        user = new ApplicationUser
                        {
                            UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
                            Email    = info.Principal.FindFirstValue(ClaimTypes.Email)
                        };
                        //Create new in AspNetUsers Table
                        var result = await _userManager.CreateAsync(user);

                        if (result.Succeeded)
                        {
                            //Insert external loging provider user in AspNetUserLogins Table
                            await _userManager.AddLoginAsync(user, info);

                            //Get user details by email id
                            var getUserByEmail = await _userManager.FindByEmailAsync(info.Principal.FindFirstValue(ClaimTypes.Email));

                            //Generate Email Confirmation Token
                            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                            var    confirmationLInk = Url.Action("ConfirmEmail", "Account", new { area = "Comman", userId = getUserByEmail.Id, token = token }, Request.Scheme);
                            string Ref           = "NewAccountConfirmation";
                            int    isSendSuccess = CommanFunction.SendConfirmationLink(confirmationLInk, getUserByEmail.UserName, getUserByEmail.Email, Ref);
                            if (isSendSuccess == 1)
                            {
                                if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                                {
                                    return(RedirectToAction("Dashboard", "Dashboard", new { Area = "User" }));
                                }
                                ViewBag.Success = "Registration successful";
                                ViewBag.Message = "Before you can Login, please confirm your email, by clicking on the confirmation link we have emailed you";
                                return(View("RegistrationSuccessful"));
                            }
                        }
                    }
                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    //return LocalRedirect(returnUrl);
                }
                ViewBag.ErrorTitle   = $"Email claim not received from: {info.LoginProvider}";
                ViewBag.ErrorMessage = "Please contact support on [email protected]";
                return(View("Error"));
            }
        }