示例#1
0
        public async Task<IActionResult> Signup(SignupInputModel model)
        {
            if (ModelState.IsValid == false)
            {
                return Redirect(model.ReturnUrl);
            }

            var userExists = await _userManager.FindByEmailAsync(model.Email);

            if (userExists != null)
            {
                return Redirect(model.ReturnUrl);
            }

            var appUser = new ApplicationUser
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Email = model.Email,
                UserName = model.Email
            };

            var result = _userManager.CreateAsync(appUser, model.Password).Result;

            if (!result.Succeeded)
            {
                throw new Exception(result.Errors.First().Description);
            }

            result = _userManager.AddClaimsAsync(appUser, new Claim[]{
                        new Claim(JwtClaimTypes.Name, $"{appUser.FirstName} {appUser.LastName}"),
                        new Claim(JwtClaimTypes.GivenName, appUser.FirstName),
                        new Claim(JwtClaimTypes.FamilyName, appUser.LastName),
                        new Claim(JwtClaimTypes.Email, appUser.Email),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, string.Empty),
                        new Claim(JwtClaimTypes.Address, string.Empty)
                    }).Result;

            if (!result.Succeeded)
            {
                throw new Exception(result.Errors.First().Description);
            }
            else
            {
                Console.WriteLine($"User with email {appUser.Email} created");
            }

            var vm = BuildSignupViewModelAsync(model);
            return View(vm);
        }
示例#2
0
        public async Task <AuthToken> Signup([FromBody] SignupInputModel inputModel)
        {
            if (await dbContext.Players.AnyAsync(p => p.Name.ToLower() == inputModel.PlayerName.ToLower()))
            {
                return(null);
            }

            Player player = new Player()
            {
                Name         = inputModel.PlayerName,
                SecurityHash = utilities.HashText(inputModel.InitialPassword)
            };

            dbContext.Players.Add(player);

            await dbContext.SaveChangesAsync();

            return(new AuthToken
            {
                PlayerName = player.Name,
                PlayerId = player.Id,
                Signature = CreateSignature(player)
            });
        }
示例#3
0
        private async Task<SignupViewModel> BuildSignupViewModelAsync(SignupInputModel model)
        {
            var vm = await BuildSignupViewModelAsync(model.ReturnUrl);

            return vm;
        }