private void CreateTherapistFromFileContent(CreateTherapistDto createTherapistDto)
        {
            _logger.Debug("Begin");

            if (!ModelState.IsValid)
            {
                throw new Exception(CreateLogString(ModelState));
            }

            var user = new NdUser()
            {
                Email       = createTherapistDto.Email,
                FirstName   = createTherapistDto.FirstName,
                Gender      = createTherapistDto.Gender,
                Institute   = createTherapistDto.Institute,
                LastName    = createTherapistDto.LastName,
                PhoneNumber = createTherapistDto.PhoneNumber,
                Title       = createTherapistDto.Title,
                WebPage     = createTherapistDto.WebPage
            };
            var password = Membership.GeneratePassword(8, 1);

            IdentityResult addUserResult = _userManager.Create(user, password);

            if (!addUserResult.Succeeded)
            {
                throw new Exception(string.Join(Environment.NewLine, addUserResult.Errors));
            }

            IdentityResult addUserToRoleResult = _userManager.AddToRole(user.Id, Role.Therapist.ToString());

            if (!addUserToRoleResult.Succeeded)
            {
                throw new Exception(string.Join(Environment.NewLine, addUserToRoleResult.Errors));
            }

            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Path.Combine("~/Results", user.Id)));

            string code        = _userManager.GenerateEmailConfirmationToken(user.Id);
            var    callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

            _userManager.SendEmail(user.Id, "Confirm your account", NdEmailTemplateProvider.CreateConfirmEmailWithPassword(callbackUrl.ToString(), password));
        }
        public async Task <IHttpActionResult> CreateTherapist(CreateTherapistDto createTherapistDto)
        {
            _logger.Debug("Begin");

            if (!ModelState.IsValid)
            {
                _logger.Error(CreateLogString(ModelState));
                return(BadRequest(ModelState));
            }

            var user = new NdUser()
            {
                Email       = createTherapistDto.Email,
                FirstName   = createTherapistDto.FirstName,
                Gender      = createTherapistDto.Gender,
                Institute   = createTherapistDto.Institute,
                LastName    = createTherapistDto.LastName,
                PhoneNumber = createTherapistDto.PhoneNumber,
                Title       = createTherapistDto.Title,
                WebPage     = createTherapistDto.WebPage
            };
            var password = Membership.GeneratePassword(8, 1);

            IdentityResult addUserResult = await _userManager.CreateAsync(user, password);

            if (!addUserResult.Succeeded)
            {
                _logger.Error(string.Format("Create therapist with e-mail [{0}] failed", createTherapistDto.Email),
                              new Exception(string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            IdentityResult addUserToRoleResult = await _userManager.AddToRoleAsync(user.Id, Role.Therapist.ToString());

            if (!addUserToRoleResult.Succeeded)
            {
                _logger.Error(string.Format("Add therapist with e-mail [{0}] to role [Therapist] failed", createTherapistDto.Email),
                              new Exception(string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            try
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Path.Combine("~/Results", user.Id)));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Create folder for therapist with e-mail [{0}] failed", createTherapistDto.Email), ex);
                return(InternalServerError(ex));
            }

            try
            {
                string code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);

                var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
                await _userManager.SendEmailAsync(user.Id, "Confirm your account", NdEmailTemplateProvider.CreateConfirmEmailWithPassword(callbackUrl.ToString(), password));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error sending ConfirmEmailWithPassword email for therapist with e-mail [{0}]", createTherapistDto.Email), ex);
                return(InternalServerError(ex));
            }

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            return(Created(locationHeader, user.CreateCreateUserReturnDto(_userManager, Request)));
        }