public async Task <JsonResult> CreateAgent(AgentCredentialsViewModel model)
        {
            return(await RunWithResult(async() => {
                ValidateAgentCredentials(model);

                var userManager = HttpContext.GetOwinContext().GetUserManager <BkUserManager>();
                var roleManager = HttpContext.GetOwinContext().GetUserManager <BkRoleManager>();

                var existingUser = await userManager.FindByNameAsync(model.Code);
                if (existingUser != null)
                {
                    throw new PublicException($"Пользователь с именем {model.Code} уже существует");
                }

                if (!roleManager.RoleExists(UserRoleNames.Agent))
                {
                    var roleCreationResult = await roleManager.CreateAsync(new BkRole(UserRoleNames.Agent));
                    if (!roleCreationResult.Succeeded)
                    {
                        throw new PublicException(string.Join(". ", roleCreationResult.Errors));
                    }
                }

                var user = new BkUser {
                    UserName = model.Code
                };
                var userCreationResult = await userManager.CreateAsync(user, model.Password);
                if (!userCreationResult.Succeeded)
                {
                    throw new PublicException(string.Join(". ", userCreationResult.Errors));
                }

                var addToRoleResult = await userManager.AddToRoleAsync(user.Id, UserRoleNames.Agent);
                if (!addToRoleResult.Succeeded)
                {
                    throw new PublicException(string.Join(". ", addToRoleResult.Errors));
                }

                var agentId = await _agentRepository.CreateAgentAync(model.Code, model.Password);

                return new Agent {
                    Id = agentId,
                    Code = model.Code,
                    Password = model.Password
                };
            }, "При создании агента произошла ошибка"));
        }