Exemplo n.º 1
0
        public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new User { UserName = model.UserName, Email = model.Email };
                var result = await _userManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                    await _signInManager.SignInAsync(user, isPersistent: false);
                    //_logger.LogInformation(3, "User created a new account with password.");
                    return RedirectToLocal(returnUrl);
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
 public override void Add(User entity)
 {
     using (var scope = new TransactionScope(TransactionScopeOption.Suppress)) {
         base.Add(entity);
         scope.Complete();
     }
 }
Exemplo n.º 3
0
        public async Task Setup(string userName, string email, string password, Action<string, string> reportError)
        {
            var userManager = _serviceProvider.GetRequiredService<UserManager<User>>();
            var options = _serviceProvider.GetRequiredService<IOptions<IdentityOptions>>().Value;
            var T = _serviceProvider.GetRequiredService<IStringLocalizer<SetupEventHandler>>();

            var superUser = new User
            {
                UserName = userName,
                Email = email,
                RoleNames = { "Administrator" }
            };

            var result = await userManager.CreateAsync(superUser, password);

            if (!result.Succeeded)
            {
                foreach(var error in result.Errors)
                {
                    switch (error.Code)
                    {
                        // Password
                        case "PasswordRequiresDigit":
                            reportError("Password", T["Passwords must have at least one digit ('0'-'9')."]);
                            break;
                        case "PasswordRequiresLower":
                            reportError("Password", T["Passwords must have at least one lowercase ('a'-'z')."]);
                            break;
                        case "PasswordRequiresUpper":
                            reportError("Password", T["Passwords must have at least one uppercase('A'-'Z')."]);
                            break;
                        case "PasswordRequiresNonAlphanumeric":
                            reportError("Password", T["Passwords must have at least one non letter or digit character."]);
                            break;
                        case "PasswordTooShort":
                            reportError("Password", T["Passwords must be at least {0} characters.", options.Password.RequiredLength]);
                            break;

                        // Password confirmation
                        case "PasswordMismatch":
                            reportError("PasswordConfirmation", T["Incorrect password."]);
                            break;

                        // User name
                        case "InvalidUserName":
                            reportError("AdminUserName", T["User name '{0}' is invalid, can only contain letters or digits.", superUser]);
                            break;

                        // Email
                        case "InvalidEmail":
                            reportError("AdminEmail", T["Email '{0}' is invalid.", email]);
                            break;
                    }

                }
            }

            return;
        }
Exemplo n.º 4
0
        public Task Setup(string userName, string email, string password)
        {
            var userManager = _serviceProvider.GetRequiredService<UserManager<User>>();
            var superUser = new User
            {
                UserName = userName,
                Email = email,
                RoleNames = { "Administrator" }
            };

            return userManager.CreateAsync(superUser, password);
        }
        public LiveoUserHandler(IRepository<LiveoUserRecord> repository, 
            ILiveoUserService liveoUserService,
            IOrchardServices orchardServices,
            ILiveoPersonaService personaService,
            ILiveoPersonaUserService liveoPersonaUserService) {
            Filters.Add(StorageFilter.For(repository));
            _liveoUserService = liveoUserService;
            _orchardServices = orchardServices;
            _personaService = personaService;
            _liveoPersonaUserService = liveoPersonaUserService;

            OnCreated<UserPart>((context, part) => {
                                    var liveoUser = part.As<LiveoUserPart>();
                if (liveoUser != null) {
                    // ensure created user...
                    var user = _liveoUserService.GetById(liveoUser.UserId);
                    if (user != null) {
                        liveoUser.User = user;
                    }
                    if (user == null && !string.IsNullOrEmpty(part.Email)) {
                        user = _liveoUserService.GetUserByEmail(part.Email);

                        if (user == null) {
                            user = new User();
                            user.UserName = part.Email;
                            user.CreateDate = DateTime.UtcNow;
                            user.LastUpdateDate = user.CreateDate;
                            user.Email = user.UserName;
                            user.Password = "******";
                            user.PasswordFormat = 0;
                            user.LoweredEmail = user.Email;

                            _liveoUserService.Add(user);
                        }

                        liveoUser.UserId = user.Id;
                        liveoUser.User = user;

                        var rootPersona = _personaService.GetPersonaByName("User");
                        var userPersona = new UserPersona();
                        userPersona.PersonaId = rootPersona.Id;
                        userPersona.UserId = user.Id;
                        _personaService.Save(rootPersona);

                        _orchardServices.ContentManager.Publish(part.ContentItem);

                        _orchardServices.ContentManager.Publish(liveoUser.ContentItem);
                    }
                }
                                });

            OnLoaded<UserPart>((context, part) => {
                                   var liveoUser = part.As<LiveoUserPart>();
                if (liveoUser != null) {
                    // ensure created user when module might have been disabled, etc...
                    // ensure created user...
                    var user = _liveoUserService.GetById(liveoUser.UserId);
                    if (user != null) {
                        liveoUser.User = user;
                    }
                    if (user == null && !string.IsNullOrEmpty(part.Email))
                    {
                        user = new User();
                        user.UserName = part.Email;
                        user.CreateDate = DateTime.UtcNow;
                        user.LastUpdateDate = user.CreateDate;
                        user.Email = user.UserName;
                        user.Password = "******";
                        user.PasswordFormat = 0;
                        user.LoweredEmail = user.Email;

                        _liveoUserService.Add(user);

                        liveoUser.UserId = user.Id;

                        var rootPersona = _personaService.GetPersonaByName("User");
                        var userPersona = new UserPersona();
                        userPersona.PersonaId = rootPersona.Id;
                        userPersona.UserId = user.Id;
                        _personaService.Save(rootPersona);

                        _orchardServices.ContentManager.Publish(liveoUser.ContentItem);
                    }
                }
                               });
        }
Exemplo n.º 6
0
        public async Task<IActionResult> Create(CreateUserViewModel model)
        {
            CleanViewModel(model);

            if (ModelState.IsValid)
            {
                if (await _userManager.FindByNameAsync(model.UserName) != null)
                {
                    ModelState.AddModelError(string.Empty, T["The user name is already used."]);
                }

                if (await _userManager.FindByEmailAsync(model.Email) != null)
                {
                    ModelState.AddModelError(string.Empty, T["The email is already used."]);
                }
            }

            if (ModelState.IsValid)
            {
                var roleNames = model.Roles.Where(x => x.IsSelected).Select(x => x.Role).ToList();
                var user = new User { UserName = model.UserName, Email = model.Email, RoleNames = roleNames };
                var result = await _userManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    _notifier.Success(TH["User created successfully"]);
                    return RedirectToAction(nameof(Index));
                }

                _session.Cancel();

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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