Esempio n. 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);
        }
Esempio n. 2
0
        public async Task <object> 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();

            Object response;
            int    clientId = 0;

            try
            {
                using (_ctx)
                {
                    //JB. Create the client.
                    _ctx.Clients.Add(client);
                    _ctx.SaveChanges();
                    clientId = client.Id;
                    //JB. Add now the Secret
                    await _ctx.ClientSecrets.AddAsync(_factory.CreateClientSecret(clientId, NewSecret));

                    //JB. Add Client Grant Type
                    await _ctx.ClientGrantTypes.AddAsync(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 _ctx.ClientScopes.AddAsync(new ClientScope { ClientId = clientId, Scope = scopev });
                    //}
                    ////JB. Add claims. Info about this Client
                    //foreach (var c in dto.Claims)
                    //{
                    //    await _ctx.ClientClaims.AddAsync(new ClientClaim { ClientId = clientId, Type = c["Type"], Value = c["Value"] });
                    //}
                    _ctx.SaveChanges();
                };

                response = new ClientResponseDto
                {
                    ClientName    = client.ClientName,
                    Client_Id     = client.ClientId,
                    Secret        = NewSecret,
                    AllowedScopes = dto.AllowedScopes,
                    Claims        = dto.Claims
                };
            }
            catch (Exception ex)
            {
                response = new ClientErrorResponseDto {
                    Error = HttpStatusCode.InternalServerError.ToString(), Message = ex.Message
                };
            }

            return(response);
        }
Esempio n. 3
0
 //[Authorize]
 public async Task <IActionResult> AddClient(ClientBindingDto dto)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try
     {
         if (await _repo.ClientExist(dto.ClientName))
         {
             ClientErrorResponseDto response = new ClientErrorResponseDto {
                 Error = "Conflict", Message = "A Client with name '" + dto.ClientName + "' already exist"
             };
             return(Conflict(response));
         }
         return(Ok(await _repo.AddClient(_factory.CreateClientEntity(dto), dto)));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }