Esempio n. 1
0
        private async Task <UserDto> AddUserWithRole(UserDto user, string roleName)
        {
            try
            {
                ApplicationUser applicationUser = new ApplicationUser
                {
                    Email        = user.Email,
                    UserName     = user.Email,
                    FullName     = user.UserName,
                    ContragentId = user.CtgId
                };

                IdentityResult result = await _userManager.CreateAsync(applicationUser, user.Password);

                if (!result.Succeeded)
                {
                    return(new UserDto
                    {
                        Success = false,
                        ErrorMessage = string.Join("\r\n ", result.Errors.Select(e => e.Description).ToArray())
                    });
                }
                await _userManager.AddToRoleAsync(applicationUser, roleName);

                // Send an email with this link
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(applicationUser);

                var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                             new { userId = applicationUser.Id, code = code }, protocol: HttpContext.Request.Scheme);

                IAccountConfirmationService accountConfirmationService = new AccountEmailConfirmationService();
                await accountConfirmationService.Confirm(applicationUser, callbackUrl);

                // Comment out following line to prevent a new user automatically logged on.
                // await _signInManager.SignInAsync(user, isPersistent: false);


                return(new UserDto
                {
                    Id = applicationUser.Id,
                    Email = applicationUser.Email,
                    Password = string.Empty,
                    UserName = applicationUser.UserName,
                    Name = user.Name
                });
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> RegisterContragent(RegisterContragentViewModel model, string returnUrl = null)
        {
            using (DB_A12601_bielkaContext context = new DB_A12601_bielkaContext())
            {
                var userToCompare = context.AspNetUsers.Where(u => u.UserName == model.Email && u.Deleted).FirstOrDefault();
                if (userToCompare != null)
                {
                    userToCompare.UserName           = userToCompare.Id + "_" + userToCompare.UserName;
                    userToCompare.NormalizedUserName = userToCompare.Id + "_" + userToCompare.NormalizedUserName;
                    userToCompare.Email           = userToCompare.Id + "_" + userToCompare.Email;
                    userToCompare.NormalizedEmail = userToCompare.Id + "_" + userToCompare.NormalizedEmail;
                    context.SaveChanges();
                }
            }

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email    = model.Email,
                    FullName = model.Name
                };

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

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, UsersController.CONTAGENT_ROLE_NAME);

                    // Send an email with this link
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account",
                                                 new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);

                    IAccountConfirmationService accountConfirmationService = new AccountEmailConfirmationService();
                    await accountConfirmationService.Confirm(user, callbackUrl);

                    // Comment out following line to prevent a new user automatically logged on.
                    // await _signInManager.SignInAsync(user, isPersistent: false);

                    _logger.LogInformation(3, "User created a new account with password.");

                    Contragent contragent = CreateContragent(model.CompanyName);

                    using (DB_A12601_bielkaContext context = new DB_A12601_bielkaContext())
                    {
                        var userToModify = context.AspNetUsers.Where(u => u.Id == user.Id).FirstOrDefault();

                        userToModify.ContragentId = contragent.CgtId;

                        context.SaveChanges();
                    }
                    return(View("SucceedRegister"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }