public async Task <IActionResult> EmailConfirmed(EmailConfirmViewModel model)
        {
            var user = await _dbContext
                       .Users
                       .Include(t => t.Emails)
                       .SingleOrDefaultAsync(t => t.Id == model.UserId);

            if (user == null)
            {
                return(NotFound());
            }
            var mailObject = await _dbContext
                             .UserEmails
                             .SingleOrDefaultAsync(t => t.ValidateToken == model.Code);

            if (mailObject == null || mailObject.OwnerId != user.Id)
            {
                return(NotFound());
            }
            if (!mailObject.Validated)
            {
                _logger.LogWarning($"The email object with address: {mailObject.EmailAddress} was already validated but the user was still trying to validate it!");
            }
            mailObject.Validated     = true;
            mailObject.ValidateToken = string.Empty;
            await _dbContext.SaveChangesAsync();

            return(View());
        }
示例#2
0
        private async Task SendConfirmEmail(IdentityUser user)
        {
            if (user == null)
            {
                throw new NullReferenceException("User not exist.");
            }

            var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            // TODO: Change url to Request.Headers.Referrer + "#/confirm" - need use LocalStorage instead Session Storage

            var url = Url.Link("Default", new
            {
                Controller = "Home",
                Action     = "ConfirmEmail",
                userId     = user.Id,
                code       = code,
            });

            EmailConfirmViewModel confirm = new EmailConfirmViewModel()
            {
                UserName = user.UserName,
                Url      = url
            };


            string body = Razor.RenderViewToString("Email", "ConfirmEmail", confirm);

            await UserManager.SendEmailAsync(user.Id, "Confirm your account", body);
        }
        public IActionResult EmailConfirm(int userId, string code)
        {
            ViewBag.MsgInfo = "";
            var model = new EmailConfirmViewModel()
            {
                Id   = userId,
                Code = code,
            };

            return(View());
        }
        public async Task <IActionResult> ConfirmEmail([FromQuery] EmailConfirmViewModel model)
        {
            var employer = await _userManager.FindByIdAsync(model.UserId);

            var confirm = await _userManager.ConfirmEmailAsync(employer, Uri.UnescapeDataString(model.Token));

            if (confirm.Succeeded)
            {
                return(RedirectToAction("Index", "/"));
            }

            return(Unauthorized());
        }
        public async Task <IActionResult> EmailCode(EmailConfirmViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var result = await _userManager.ConfirmEmailAsync(user, model.Code);

            if (result.Succeeded)
            {
                return(RedirectToAction("MySecurity"));
            }
            ModelState.AddModelError(string.Empty, $"{_locService.GetLocalizedHtmlString("genericError")}");
            return(View(model));
        }
示例#6
0
        public IActionResult CheckEmailConfirm(EmailConfirmViewModel confirmEmail)
        {
            if (ConfirmEmail.ValidationToken != confirmEmail.EnteredNumber)
            {
                return(RedirectToAction("ThanksForReservation", "Passengers"));
            }
            else
            {
                this.context.Reservations.First(r => r.Id == ConfirmEmail.ReservationIdGoing).IsConfirmed = true;
                this.context.SaveChanges();

                if (ConfirmEmail.ReservationIdReturning != 0)
                {
                    this.context.Reservations.First(r => r.Id == ConfirmEmail.ReservationIdReturning).IsConfirmed = true;
                    this.context.SaveChanges();
                }
                ConfirmEmail.EmailConfirmed = true;
            }
            return(RedirectToAction("ThanksForReservation", "Passengers"));
        }
示例#7
0
        public async Task <IActionResult> ConfirmEmailAsync(EmailConfirmViewModel ec)
        {
            var services = new ServiceCollection();

            services.AddLogging();
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(Startup.ConnectionString));

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            using (var serviceProvider = services.BuildServiceProvider())
            {
                using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
                {
                    var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                    var user    = await userMgr.FindByIdAsync(ec.UserId);

                    await userMgr.ConfirmEmailAsync(user, ec.code);
                }
            }
            return(View());
        }