Пример #1
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            string inviter = string.Empty;

            if ((_InviteOnly) && (!_superAdminService.IsSuperAdminEmail(model.Email)))
            {
                if (string.IsNullOrEmpty(model.Code))
                {
                    //ModelState.AddModelError("Code", "Please provide an Invite Code.");
                    return(BadRequest(Errors.AddErrorToModelState("", "", ModelState)));
                }

                inviter = await _inviteService.IsInvited(model.Email, model.Code);

                if (string.IsNullOrEmpty(inviter))
                {
                    //ModelState.AddModelError("Code", "Invalid Invite Code.");
                    return(BadRequest(Errors.AddErrorToModelState("", "", ModelState)));
                }
            }

            if (ModelState.IsValid)
            {
                var user = new AppUser {
                    UserName = model.Email, Email = model.Email, Inviter = inviter
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await this.SendConfirmationEmail(user);

                    // await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    //return RedirectToLocal(returnUrl);
                    return(new OkObjectResult(""));
                }
                return(BadRequest(Errors.AddErrorToModelState("", "", ModelState)));
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(Errors.AddErrorToModelState("", "", ModelState)));
        }
Пример #2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model, string returnUrl = null)
        {
            model.Username = model.Username.ToLowerInvariant();
            model.Email    = model.Email.ToLowerInvariant();

            ViewData["ReturnUrl"] = returnUrl;

            if (await this.UserExists(model.Username))
            {
                return(BadRequest(Errors.AddErrorToModelState("Username", "Username already exists.", ModelState)));
            }

            if (model.Username.Length < 5)
            {
                return(BadRequest(Errors.AddErrorToModelState("Username", "Username must be more than 5 characters.", ModelState)));
            }

            if (model.Username.Length > 20)
            {
                return(BadRequest(Errors.AddErrorToModelState("Username", "Username must be less than 20 characters.", ModelState)));
            }

            if (await this.EmailExists(model.Email))
            {
                return(BadRequest(Errors.AddErrorToModelState("Email", "Email already exists.", ModelState)));
            }

            string tenantRegEx = _configuration["TenantRegEx"];

            if (!string.IsNullOrEmpty(tenantRegEx))
            {
                Regex regex = new Regex(tenantRegEx);
                var   match = regex.Match(model.Email);
                if (!match.Success)
                {
                    return(BadRequest(Errors.AddErrorToModelState("Email", "Email is not allowed.", ModelState)));
                }
            }

            string inviter = string.Empty;

            if ((_InviteOnly) && (!_claimsService.IsSuperAdmin(model.Username)))
            {
                if (string.IsNullOrEmpty(model.Code))
                {
                    return(BadRequest(Errors.AddErrorToModelState("Code", "Please provide an Invite Code.", ModelState)));
                }

                inviter = await _inviteService.IsInvited(model.Email, model.Code);

                if (string.IsNullOrEmpty(inviter))
                {
                    return(BadRequest(Errors.AddErrorToModelState("Code", "Invalid Invite Code.", ModelState)));
                }
            }

            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.Username))
                {
                    model.Username = model.Email;
                }

                var user = new AppUser {
                    UserName = model.Username, Email = model.Email, Inviter = inviter
                };
                user.Id = Guid.NewGuid().ToString();

                if (_configuration["DomainAsTenant"] == "y")
                {
                    user.Tenant = this.GetUserDomain(user.Email);
                }

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

                if (result.Succeeded)
                {
                    var emailResult = await this.SendConfirmationEmail(user, false);

                    // await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    //return RedirectToLocal(returnUrl);
                    return(emailResult);
                }
                return(BadRequest(Errors.AddErrorToModelState("", "", ModelState)));
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(Errors.AddErrorToModelState("", "", ModelState)));
        }