Esempio n. 1
0
        public async Task <IActionResult> Register([FromBody] UserRegistrationDto model)
        {
            if (await _authRepo.EmailExists(model.Email))
            {
                var account = await _authRepo.GetAccountByEmail(model.Email);

                if (!account.EmailConfirmed)
                {
                    await _authRepo.DelteAccount(account);
                }
                else
                {
                    ModelState.AddModelError("Email", "そのEメールアドレスは既に使用されています");
                }
            }
            else if (await _authRepo.UserNameExists(model.UserName))
            {
                var account = await _authRepo.GetAccountByUserName(model.UserName);

                if (!account.EmailConfirmed)
                {
                    await _authRepo.DelteAccount(account);
                }
                else
                {
                    ModelState.AddModelError("UserName", "そのユーザーネームは既に使用されています");
                }
            }

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

            var newAccount = _mapper.Map <Account>(model);
            var user       = await _authRepo.Register(newAccount, 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?id=" + user.Id + "&code=" + 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(newAccount);

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

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

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

            return(CreatedAtRoute("GetUser", new { id = newAccount.Id }, new { }));
        }
Esempio n. 2
0
        public IHttpActionResult Add(UserModel user)
        {
            var resultUser = Manager.AddUser(user.ToUser());
            var AuthUser   = AuthRepository.AddAppUser(new AppUser {
                Id = resultUser.Id, PrivateKey = Guid.NewGuid()
            });

            return(Ok(new AuthUserResultModel(AuthUser)));
        }
Esempio n. 3
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 { }));
        }