Пример #1
0
        /// <summary>
        /// JB. Asyncronously create a new Client. Add Secret and Scope and return a response witht he info needed for Client Integration.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public async Task <ClientResponseDto> AddClient(Client client, ClientBindingDto dto)
        {
            //JB. Build a newly randomdized Secret, this is what is passed to the client and it is not hashed yet. It will be hashed at persisting time.
            string NewSecret = await RandomStringGenerator.GeneratedString();

            ClientResponseDto response = null;
            int clientId = 0;

            try
            {
                await Task.Run(async() =>
                {
                    using (/*var*/ ctx /*= new ResourceConfigDbContext()*/)
                    {
                        ctx.Clients.Add(client);
                        ctx.SaveChanges();
                        clientId = client.Id;
                    };
                    //JB. Add now Secret
                    _secretsRepo.AddClientSecret(_factory.CreateClientSecret(clientId, NewSecret));

                    //JB. Add Client Grant Type
                    await _grantRepo.AddGrantType(new ClientGrantType {
                        ClientId = clientId, GrantType = IdentityServer4.Models.GrantType.ClientCredentials
                    });

                    //JB. Add Scopes this client is allowed in the system.
                    foreach (var scopev in dto.AllowedScopes)
                    {
                        await _scopeRepo.CreateClientScope(new ClientScope {
                            ClientId = clientId, Scope = scopev
                        });
                    }

                    response = new ClientResponseDto
                    {
                        ClientName    = client.ClientName,
                        Client_Id     = client.ClientId,
                        Secret        = NewSecret,
                        AllowedScopes = dto.AllowedScopes,
                        Claims        = dto.Claims
                    };
                    //JB. Add claims. Info about this Client
                    foreach (var c in dto.Claims)
                    {
                        await _claimRepo.AddClaim(new ClientClaim {
                            ClientId = clientId, Type = c["Type"], Value = c["Value"]
                        });
                    }
                });
            }
            catch (Exception ex)
            {
                ClientErrorResponseDto errorResponse = new ClientErrorResponseDto {
                    Error = "Not Found. " + ex.Message
                };
            }

            return(response);
        }
        public async Task <IActionResult> AddClientScope(ClientScopeBindingDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(Ok(await _repo.CreateClientScope(_factory.BuildClientScope(dto))));
        }