Пример #1
0
        public async Task <IActionResult> Register([FromBody] UserRegistrationDto model)
        {
            if (await _authRepo.AccountExists(model.UserName, model.Email))
            {
                ModelState.AddModelError("UserName", "The username or the email already exist");
            }

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

            var appUser = _mapper.Map <Account>(model);

            // if(string.IsNullOrEmpty(model.DisplayName)){
            //     member.DisplayName = appUser.UserName;
            // }
            // member.Identity = appUser;

            var user = await _authRepo.Register(appUser, model.UserType, model.Password);

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var request     = Url.ActionContext.HttpContext.Request;
            var callbackUrl = request.Scheme + "://" + request.Host.Value + "/account/confirm/" + user.Id + "/" + WebUtility.UrlEncode(code);

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

            if (await _authRepo.AddAppUser(user, model.UserType) == null)
            {
                var result = await _authRepo.DelteAccount(appUser);

                return(BadRequest("アカウントの作成に失敗しました"));
            }

            await _authRepo.AddRoles(appUser, model.UserType == "Business"?new string[] { "Business" } : new string[] { "Member" });

            await _emailSender.SendEmailAsync(model.Email, "アカウントの確認", BuildConfirmEmailContent(appUser.UserName, callbackUrl));

            return(CreatedAtRoute("GetUser", new { id = appUser.Id }, new { }));
        }
Пример #2
0
        public async Task <IActionResult> Register(AccountForRegisterDto accountForRegisterDto)
        {
            //Remove spaces in front and back and lower the string.
            accountForRegisterDto.Email = accountForRegisterDto.Email.ToLower().Trim();

            //Check if the user exists
            if (await _repo.AccountExists(accountForRegisterDto.Email))
            {
                return(BadRequest(new { error = "Email already exists" }));
            }

            //Make new Account object based on the DTO
            var accountToCreate = new Account
            {
                Email      = accountForRegisterDto.Email,
                FirstName  = accountForRegisterDto.FirstName,
                LastName   = accountForRegisterDto.LastName,
                MiddleName = accountForRegisterDto.MiddleName
            };



            //Create the account
            var createdAccount = await _repo.Register(accountToCreate, accountForRegisterDto.Password);

            //Send verification Email
            Guid verificationId = Guid.NewGuid();
            var  verification   = new Verification {
                Id = verificationId, Account = createdAccount
            };
            await _repo.CreateVerificationInstance(verification);

            EmailService.SendVerificationEmail(createdAccount.Email, verificationId.ToString());

            //Return 201 (created)
            return(StatusCode(201));
        }