public async Task <IActionResult> Create([FromForm] AccountCreateRequest request)
        {
            if (request == null || string.IsNullOrWhiteSpace(request.UserName) || string.IsNullOrWhiteSpace(request.Password) || string.IsNullOrWhiteSpace(request.DisplayName))
            {
                return(BadRequest());
            }

            var user = await _userManager.FindByNameAsync(request.UserName);

            if (user == null)
            {
                user = new WingTipUser
                {
                    MigrationStatus = (int)MigrationStatus.New,
                    UserName        = request.UserName
                };

                var displayNameClaim = new IdentityUserClaim <string>
                {
                    ClaimType  = DisplayNameClaimType,
                    ClaimValue = request.DisplayName
                };

                user.Claims.Add(displayNameClaim);

                if (!string.IsNullOrEmpty(request.PlayerTag))
                {
                    var playerTagClaim = new IdentityUserClaim <string>
                    {
                        ClaimType  = PlayerTagClaimType,
                        ClaimValue = request.PlayerTag
                    };

                    user.Claims.Add(playerTagClaim);
                }

                // If the user does not exist, then creates the user in the SQL identity store with the Azure Active Directory-managed
                // password.
                await _userManager.CreateAsync(user, request.Password);
            }
            else
            {
                // If the user does exist, then update the password for the user in the SQL identity store to the Azure Active Directory-managed
                // password.
                await _userManager.SetPasswordAsync(user, request.Password);

                user.MigrationStatus = (int)MigrationStatus.MigratedWithPassword;
                await _userManager.UpdateAsync(user);
            }

            return(Ok(user));
        }
コード例 #2
0
        public async Task <IActionResult> Create(CreateViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

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

            var user = new WingTipUser
            {
                MigrationStatus = (int)MigrationStatus.NotMigrated,
                UserName        = viewModel.UserName
            };

            var displayNameClaim = new IdentityUserClaim <string>
            {
                ClaimType  = WingTipClaimTypes.DisplayNameClaimType,
                ClaimValue = viewModel.DisplayName
            };

            user.Claims.Add(displayNameClaim);

            if (!string.IsNullOrEmpty(viewModel.PlayerTag))
            {
                var playerTagClaim = new IdentityUserClaim <string>
                {
                    ClaimType  = WingTipClaimTypes.PlayerTagClaimType,
                    ClaimValue = viewModel.PlayerTag
                };

                user.Claims.Add(playerTagClaim);
            }

            var createResult = await _userManager.CreateAsync(user, viewModel.Password);

            if (!createResult.Succeeded)
            {
                throw new InvalidOperationException(createResult.Errors.ToString());
            }

            return(RedirectToAction("Index"));
        }