Exemple #1
0
        public async Task <KeyValuePair <string, string> > CreateAsync(CreateAccountInputModel input, ApplicationUser owner)
        {
            var user = new ApplicationUser
            {
                UserName    = input.UserName,
                Email       = input.Email,
                PhoneNumber = input.Phone,
                Parent      = owner,
            };

            var userPassword = Guid.NewGuid().ToString().Substring(0, 8);

            var result = await this.userManager.CreateAsync(user, userPassword);

            if (result.Succeeded)
            {
                await this.userManager.AddToRoleAsync(user, "Customer");

                var token = await this.userManager.GenerateEmailConfirmationTokenAsync(user);

                var customer = await this.customersRepository.GetByIdWithDeletedAsync(input.Id);

                customer.HasAccount = true;
                this.customersRepository.Update(customer);
                await this.customersRepository.SaveChangesAsync();

                return(new KeyValuePair <string, string>(token, userPassword));
            }
            else
            {
                throw new Exception(result.ToString());
            }
        }
Exemple #2
0
        public IActionResult Create(CreateAccountInputModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    HierarchicalUserAccount account = _userAccountService.CreateAccount(model.Username, PasswordGenerator.GeneratePasswordOfLength(16), model.Email);
                    _userAccountService.SetConfirmedEmail(account.ID, account.Email);
                    _userAccountService.ResetPassword(account.ID);
                    AddClaims(account.ID, model);

                    return(View("Success", model));
                }
                catch (ValidationException ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return(Create());
        }
Exemple #3
0
        private void AddClaims(Guid accountId, CreateAccountInputModel model)
        {
            model.Email = model.Email.ToLowerInvariant().Trim();

            UserClaimCollection claims = new UserClaimCollection
            {
                new UserClaim("given_name", model.GivenName),
                new UserClaim("family_name", model.FamilyName),
                new UserClaim("name", string.Join(" ",
                                                  new string[] { model.GivenName, model.FamilyName }
                                                  .Where(name => !string.IsNullOrWhiteSpace(name))
                                                  ))
            };

            if (model.IsAuthCentralAdmin)
            {
                claims.Add(new UserClaim("fsw:authcentral:admin", "true"));
            }

            if (!string.IsNullOrWhiteSpace(model.Organization))
            {
                claims.Add(new UserClaim("fsw:organization", model.Organization));
            }
            else if (model.Email.EndsWith("@foodservicewarehouse.com") || model.Email.EndsWith("@fsw.com"))
            {
                claims.Add(new UserClaim("fsw:organization", "FSW"));
            }
            else
            {
                string emailDomain = model.Email.Split('@')[1];
                claims.Add(new UserClaim("fsw:organization", emailDomain));
            }

            if (!string.IsNullOrWhiteSpace(model.Department))
            {
                claims.Add(new UserClaim("fsw:department", model.Department));
            }

            _userAccountService.AddClaims(accountId, claims);
        }