コード例 #1
0
        public async Task <IActionResult> Register(RegisterInputModel model, string button)
        {
            if (button == "register")
            {
                //todo fix this

/*                if (model.Password.Equals(model.ConfirmPassword))
 *              {
 *                  Console.WriteLine(model.Password + " : " + model.ConfirmPassword);
 *                  return BadRequest("Passwords did not match");
 *              }*/

                var user = new AuctorUser
                {
                    Email = model.Email, UserName = model.Username, PhoneNumber = model.PhoneNumber, SecurityStamp = new Random().Next().ToString()
                };

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

                if (!res.Succeeded)
                {
                    foreach (var identityError in res.Errors)
                    {
                        Console.WriteLine(identityError.Code + " : " + identityError.Description);
                    }
                }
            }
            if (model.ReturnUrl != null)
            {
                return(Redirect(model.ReturnUrl));
            }

            return(Redirect("/"));
        }
コード例 #2
0
        private async Task <AuctorUser> AutoProvisionUserAsync(string provider, string providerUserId, IEnumerable <Claim> claims)
        {
            // create a list of claims that we want to transfer into our store
            var filtered = new List <Claim>();

            // user's display name
            var name = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Name)?.Value ??
                       claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;

            if (name != null)
            {
                filtered.Add(new Claim(JwtClaimTypes.Name, name));
            }
            else
            {
                var first = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.GivenName)?.Value ??
                            claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value;
                var last = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.FamilyName)?.Value ??
                           claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value;
                if (first != null && last != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, first + " " + last));
                }
                else if (first != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, first));
                }
                else if (last != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, last));
                }
            }

            // email
            var email = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Email)?.Value ??
                        claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;

            if (email != null)
            {
                filtered.Add(new Claim(JwtClaimTypes.Email, email));
            }

            var user = new AuctorUser
            {
                UserName = Guid.NewGuid().ToString(),
            };
            var identityResult = await _userManager.CreateAsync(user);

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

            if (filtered.Any())
            {
                identityResult = await _userManager.AddClaimsAsync(user, filtered);

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

            identityResult = await _userManager.AddLoginAsync(user, new UserLoginInfo(provider, providerUserId, provider));

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

            return(user);
        }