public async Task <string> AddAsync(ContentSourceRegistrationViewModel model)
        {
            var dbContentSource = MapAdd().CreateMapper().Map <ContentSource>(model);

            try
            {
                await _repo.ContentSources.AddOrUpdateAndSaveAsync(dbContentSource);
            }catch (DbEntityValidationException ex) {
                foreach (var validationErrors in ex.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                               validationError.PropertyName,
                                               validationError.ErrorMessage);
                    }
                }
            }
            return(dbContentSource.Id);
        }
        public async Task <ActionResult> ContentSourceRegistration(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return(RedirectToAction("RegistrationError"));
            }
            var registration = await _repo.ContentSourceRegistrationRequests.GetByIdAsync(id);

            if (registration == null)
            {
                return(RedirectToAction("RegistrationError"));
            }
            var existingUser = await _repo.UserManager.FindByEmailAsync(registration.Email);

            var vm = new ContentSourceRegistrationViewModel()
            {
                Email            = registration.Email,
                IsAccountCreated = existingUser != null,
                FirstName        = existingUser?.FirstName,
                LastName         = existingUser?.LastName,
            };

            return(View(vm));
        }
        public async Task <ActionResult> ContentSourceRegistration(ContentSourceRegistrationViewModel model)
        {
            if (model.IsAccountCreated)
            {
                ModelState.Remove("Password");
                ModelState.Remove("ConfirmPassword");
                ModelState.Remove("FirstName");
                ModelState.Remove("LastName");
            }
            else
            {
                var passwordValid = await _repo.UserManager.PasswordValidator.ValidateAsync(model.Password);

                if (!passwordValid.Succeeded)
                {
                    foreach (var error in passwordValid.Errors)
                    {
                        ModelState.AddModelError("Password", error);
                    }
                }

                var validUser = await _repo.UserManager.UserValidator.ValidateAsync(new ApplicationUser()
                {
                    Id             = Guid.NewGuid().ToString(),
                    DateCreatedUtc = DateTime.UtcNow,
                    Email          = model.Email,
                    UserName       = model.Email,
                    EmailConfirmed = true,
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                });

                if (!validUser.Succeeded)
                {
                    foreach (var error in validUser.Errors)
                    {
                        ModelState.AddModelError("Email", error);
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var builder = new ContentSourceBuilder(_repo);

            if (model.IsAccountCreated)
            {
                var id = await builder.AddAsync(model);

                var user = await _repo.UserManager.FindByEmailAsync(model.Email);

                user.ContentSourceId = id;
                await _repo.UserManager.UpdateAsync(user);

                if (!await _repo.RoleManager.RoleExistsAsync("ContentSource"))
                {
                    await _repo.RoleManager.CreateAsync(new ApplicationRole("ContentSource") { DateCreatedUtc = DateTime.UtcNow });
                }
                //await _repo.UserManager.AddToRoleAsync(user.Id, "ContentSource");
                //await _repo.SignInManager.SignInAsync(user, false, false);

                var registrations = await _repo.ContentSourceRegistrationRequests.Where(m => m.Email.ToLower() == user.Email.ToLower()).ToListAsync();

                await _repo.ContentSourceRegistrationRequests.DeleteAndSaveAsync(registrations);

                return(RedirectToAction("Index", "Home"));
            }

            //create account
            var newUser = new ApplicationUser()
            {
                Id             = Guid.NewGuid().ToString(),
                DateCreatedUtc = DateTime.UtcNow,
                Email          = model.Email,
                UserName       = model.Email,
                EmailConfirmed = true,
                IsActive       = true,
                FirstName      = model.FirstName,
                LastName       = model.LastName,
            };

            var contentSourceId = await builder.AddAsync(model);

            newUser.ContentSourceId = contentSourceId;

            await _repo.UserManager.CreateAsync(newUser, model.Password);

            if (!await _repo.RoleManager.RoleExistsAsync("ContentSource"))
            {
                await _repo.RoleManager.CreateAsync(new ApplicationRole("ContentSource") { DateCreatedUtc = DateTime.UtcNow });
            }
            //await _repo.UserManager.AddToRoleAsync(newUser.Id, "ContentSource");
            //await _repo.SignInManager.SignInAsync(newUser, false, false);
            var otherRegistrations = await _repo.ContentSourceRegistrationRequests.Where(m => m.Email.ToLower() == newUser.Email.ToLower()).ToListAsync();

            await _repo.ContentSourceRegistrationRequests.DeleteAndSaveAsync(otherRegistrations);

            return(RedirectToAction("Index", "Home"));
        }