Exemplo n.º 1
0
        public async Task <IActionResult> ConfirmEmail(string userId, string token)
        {
            if (userId == null || token == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            ApplicationUser user = await userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with ID {userId} is not found.";
                return(View("NotFound"));
            }

            IdentityResult result = await userManager.ConfirmEmailAsync(user, token);

            if (result.Succeeded)
            {
                return(View());
            }

            var errorModel = new ExceptionDetailsViewModel()
            {
                Title = $"Email addreess is not confirmed."
            };

            return(View("Error", errorModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.Email,
                    Email    = model.Email,
                    City     = model.City
                };

                IdentityResult result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    if (signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                    {
                        return(RedirectToAction("listusers", "administration"));
                    }

                    var errorModel = new ExceptionDetailsViewModel()
                    {
                        Title   = "Registration successful",
                        Message = "Please confirm your email address before logging in."
                    };

                    return(View("Error", errorModel));
                }

                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> DeleteRole(string id)
        {
            IdentityRole role = await roleManager.FindByIdAsync(id);

            if (role == null)
            {
                return(RoleNotFound(id));
            }

            try
            {
                IdentityResult result = await roleManager.DeleteAsync(role);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ListRoles"));
                }

                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }

                return(View("ListRoles"));
            }
            catch (DbUpdateException)
            {
                var model = new ExceptionDetailsViewModel()
                {
                    Title   = $"{role.Name} role is currrently in use",
                    Message = $"First delete all the users from {role.Name} role and then try to delete it."
                };

                return(View("Error", model));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl, string remoteError)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            var model = new LoginViewModel()
            {
                ReturnUrl = returnUrl,
                ExternalLoginProviders = await signInManager.GetExternalAuthenticationSchemesAsync()
            };

            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, remoteError);
                return(View("Login", model));
            }

            var info = await signInManager.GetExternalLoginInfoAsync();

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

            string          email = info.Principal.FindFirstValue(ClaimTypes.Email);
            ApplicationUser user  = null;

            if (email != null)
            {
                user = await userManager.FindByEmailAsync(email);

                if (user != null && !user.EmailConfirmed)
                {
                    ModelState.AddModelError(string.Empty, "Email address is not confirmed yet.");
                    return(View("Login", model));
                }
            }

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

            if (result.Succeeded)
            {
                return(LocalRedirect(returnUrl));
            }

            if (email != null)
            {
                if (user == null)
                {
                    user = new ApplicationUser()
                    {
                        UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
                        Email    = info.Principal.FindFirstValue(ClaimTypes.Email)
                    };

                    await userManager.CreateAsync(user);

                    string emailConfirmationToken = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    string emailConfirmationLink = Url.Action(
                        "ConfirmEmail",
                        "Account",
                        new { UserId = user.Id, Token = emailConfirmationToken },
                        Request.Scheme
                        );

                    logger.LogInformation(emailConfirmationLink);

                    return(View("Error", new ExceptionDetailsViewModel()
                    {
                        Title = "Registration successful",
                        Message = "Please confirm your email address before loggin in."
                    }));
                }

                await userManager.AddLoginAsync(user, info);

                await signInManager.SignInAsync(user, isPersistent : false);

                return(LocalRedirect(returnUrl));
            }

            var errorModel = new ExceptionDetailsViewModel()
            {
                Title   = $"Email address is not received from {info.LoginProvider}",
                Message = "Sorry for that. Please contact support team."
            };

            return(View("Error", errorModel));
        }