Esempio n. 1
0
        public async Task <IActionResult> SendConfirmationLink([FromRoute] string userId)
        {
            var user = await userManager.FindByIdAsync(userId);

            var emailCode = await userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.Action("confirmaccount", "Account", values: new { userId = userId, code = emailCode }, Request.Scheme);
            var email       = user.Email;

            await accountHelper.SendConfirmationLink(callbackUrl, userId, email);

            return(Ok());
        }
Esempio n. 2
0
        public async Task <IActionResult> Register([FromForm] RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Error"));
            }

            if (string.IsNullOrEmpty(model.TenantId) ||
                string.IsNullOrEmpty(model.RedirectUrl))
            {
                return(View("Error"));
            }

            var user = new User
            {
                FirstName = model.FirstName,
                LastName  = model.LastName,
                Email     = model.Email,
                UserName  = $"{model.TenantId}_{model.Email}",
                TenantId  = new Guid(model.TenantId)
            };

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

            if (result.Errors.Any(x => x.Code == "DuplicateUserName"))
            {
                ModelState.AddModelError(string.Empty, "Email is already taken.");
                return(View(model));
            }

            if (result.Succeeded)
            {
                //if a new user is created, generate purpose token for email confirmation action
                string code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                var callbackUrl = Url.Action("EmailConfirmation", "Account", values: new { email = user.Email, code = code, redirectUrl = model.RedirectUrl }, Request.Scheme);
                //send the confirmation email to the user
                await accountHelper.SendConfirmationLink(callbackUrl, user.Id.ToString(), user.Email);

                //return and login the user
                await signInManager.SignInAsync(user, isPersistent : false);

                return(Redirect(model.RedirectUrl));
            }
            else
            {
                //if user is not created, show the possible errors
                ModelState.AddModelError(string.Empty, "Register Failed.");
                return(View(model));
            }
        }
Esempio n. 3
0
        public async Task <Guid> Handle(AddUserCommand request, CancellationToken cancellationToken)
        {
            var newUser = new User
            {
                FirstName = request.FirstName,
                LastName  = request.LastName,
                IsEnabled = request.IsEnabled,
                TenantId  = identityUser.TenantId,
                UserName  = $"{request.Email}_{identityUser.TenantId}",
                Email     = request.Email
            };

            var result = await userManager.CreateAsync(newUser);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException(string.Join(",", result.Errors.Select(x => x.Description)));
            }

            var roleAssignment = await userManager.AddToRolesAsync(newUser, request.Roles.Select(x => $"{x.RoleName.Replace(" ", "")}_{identityUser.TenantId}"));

            var emailCode = await userManager.GenerateEmailConfirmationTokenAsync(newUser);

            var tenant = await tenantService.GetTenantById(identityUser.TenantId.ToString());

            var productUrl = tenant.TenantProducts.FirstOrDefault().TenantProductUrl;

            var callbackUrl = string.Empty;

            if (productUrl.Contains("localhost"))
            {
                var baseUrl = "https://localhost:5001";
                callbackUrl = $"{baseUrl}/identity-b2b/Account/ConfirmAccount?userId={HttpUtility.UrlEncode(newUser.Id.ToString())}&code={HttpUtility.UrlEncode(emailCode)}";
            }
            else
            {
                if (productUrl.EndsWith("/"))
                {
                    productUrl = productUrl.Remove(productUrl.Length - 1);
                }

                var baseUrl = productUrl.Substring(0, productUrl.LastIndexOf("/"));
                callbackUrl = $"{baseUrl}/identity-b2b/Account/ConfirmAccount?userId={HttpUtility.UrlEncode(newUser.Id.ToString())}&code={HttpUtility.UrlEncode(emailCode)}";
            }

            await accountHelper.SendConfirmationLink(callbackUrl, newUser.Id.ToString(), newUser.Email);

            return(newUser.Id);
        }
Esempio n. 4
0
        public async Task <UserViewModel> Handle(AddUserCommand request, CancellationToken cancellationToken)
        {
            // Check if the user already exists for this tenant.
            if (await context.AspNetUsers.AsNoTracking().AnyAsync(x =>
                                                                  x.Email == request.Email && x.TenantId == request.TenantId))
            {
                throw new InvalidOperationException("User already exists.");
            }

            var newUser = new User
            {
                FirstName   = request.FirstName,
                LastName    = request.LastName,
                PhoneNumber = request.PhoneNumber,
                TenantId    = request.TenantId,
                UserName    = $"{request.Email}_{request.TenantId}",
                Email       = request.Email
            };

            var result = await userManager.CreateAsync(newUser);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException(string.Join(",", result.Errors.Select(x => x.Description)));
            }
            var emailCode = await userManager.GenerateEmailConfirmationTokenAsync(newUser);

            var passwordCode = await userManager.GeneratePasswordResetTokenAsync(newUser);

            var tenant = await tenantService.GetTenantById(request.TenantId.ToString());

            var productUrl = tenant.TenantProducts.FirstOrDefault().TenantProductUrl;

            var callbackUrl = string.Empty;

            if (productUrl.Contains("localhost"))
            {
                var baseUrl = "https://localhost:5002";
                callbackUrl = $"{baseUrl}/identity-b2b/Account/ConfirmAccount?userId={HttpUtility.UrlEncode(newUser.Id.ToString())}&code={HttpUtility.UrlEncode(emailCode)}";
            }
            else
            {
                if (productUrl.EndsWith("/"))
                {
                    productUrl = productUrl.Remove(productUrl.Length - 1);
                }

                var baseUrl = productUrl.Substring(0, productUrl.LastIndexOf("/"));
                callbackUrl = $"{baseUrl}/identity-b2c/Account/ConfirmAccount?userId={HttpUtility.UrlEncode(newUser.Id.ToString())}&code={HttpUtility.UrlEncode(emailCode)}";
            }


            if (request.SendConfirmationEmail)
            {
                await accountHelper.SendConfirmationLink(callbackUrl, newUser.Id.ToString(), newUser.Email);
            }

            return(new UserViewModel
            {
                Id = newUser.Id,
                FirstName = newUser.FirstName,
                LastName = newUser.LastName,
                TenantId = newUser.TenantId,
                IsActive = newUser.IsActive,
                EmailCode = emailCode,
                PasswordCode = passwordCode
            });
        }