Esempio n. 1
0
        private static User CreateUserFromRequest(ApiRegisterRequest request)
        {
            var user = new User {
                UserName    = request.UserName,
                Email       = request.Email,
                PhoneNumber = request.PhoneNumber
            };

            if (!string.IsNullOrWhiteSpace(request.FirstName))
            {
                user.Claims.Add(new IdentityUserClaim <string> {
                    ClaimType  = JwtClaimTypes.GivenName,
                    ClaimValue = request.FirstName ?? string.Empty,
                    UserId     = user.Id
                });
            }
            if (!string.IsNullOrWhiteSpace(request.LastName))
            {
                user.Claims.Add(new IdentityUserClaim <string> {
                    ClaimType  = JwtClaimTypes.FamilyName,
                    ClaimValue = request.LastName ?? string.Empty,
                    UserId     = user.Id
                });
            }
            user.Claims.Add(new IdentityUserClaim <string> {
                ClaimType  = BasicClaimTypes.ConsentCommencial,
                ClaimValue = request.HasAcceptedTerms ? bool.TrueString.ToLower() : bool.FalseString.ToLower(),
                UserId     = user.Id
            });
            user.Claims.Add(new IdentityUserClaim <string> {
                ClaimType  = BasicClaimTypes.ConsentTerms,
                ClaimValue = request.HasReadPrivacyPolicy ? bool.TrueString.ToLower() : bool.FalseString.ToLower(),
                UserId     = user.Id
            });
            user.Claims.Add(new IdentityUserClaim <string> {
                ClaimType  = BasicClaimTypes.ConsentTermsDate,
                ClaimValue = $"{DateTime.UtcNow:O}",
                UserId     = user.Id
            });
            user.Claims.Add(new IdentityUserClaim <string> {
                ClaimType  = BasicClaimTypes.ConsentCommencialDate,
                ClaimValue = $"{DateTime.UtcNow:O}",
                UserId     = user.Id
            });
            return(user);
        }
Esempio n. 2
0
        public async Task <IActionResult> Register([FromBody] ApiRegisterRequest request)
        {
            var user = CreateUserFromRequest(request);
            var requestClaimTypes = request.Claims.Select(x => x.Type);
            var claimTypes        = await _configurationDbContext.ClaimTypes.Where(x => requestClaimTypes.Contains(x.Name)).ToListAsync();

            var unknownClaimTypes = requestClaimTypes.Except(claimTypes.Select(x => x.Name));

            if (unknownClaimTypes.Any())
            {
                ModelState.AddModelError(string.Empty, $"The following claim types are not supported: '{string.Join(", ", unknownClaimTypes)}'.");
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            var canAddClaims = claimTypes.All(x => x.UserEditable) || User.IsSystemClient();

            if (!canAddClaims)
            {
                ModelState.AddModelError(nameof(claimTypes), $"The following claims are not editable: '{string.Join(", ", claimTypes.Where(x => !x.UserEditable).Select(x => x.Name))}'.");
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            foreach (var claim in request.Claims)
            {
                user.Claims.Add(new IdentityUserClaim <string> {
                    ClaimType  = claim.Type,
                    ClaimValue = claim.Value ?? string.Empty,
                    UserId     = user.Id
                });
            }
            var result = await _userManager.CreateAsync(user, request.Password);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            var createdUser = SingleUserInfo.FromUser(user);
            var token       = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            await _eventService.Publish(new UserRegisteredEvent(createdUser, token));

            return(NoContent());
        }
Esempio n. 3
0
        public bool Register(ApiRegisterRequest input)
        {
            var ret = Register(input.UserName, input.Password, input.EMail);

            if (ret)
            {
                if (input.Attributes != null && input.Attributes.Count > 0)
                {
                    Login(input.UserName, input.Password);
                    var attrList = input.Attributes.Select(p => new AttributeType()
                    {
                        Name = p.Key, Value = p.Value
                    }).ToList();

                    ret = UpdateAttributes(attrList);
                }
            }
            return(ret);
        }