/// <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 <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); }
public IdentityServer4.EntityFramework.Entities.Client CreateClientEntity(ClientBindingDto dto) { return(new IdentityServer4.EntityFramework.Entities.Client { ClientName = dto.ClientName, Description = dto.Description, ClientUri = dto.ClientUri, LogoUri = dto.LogoUri, Created = DateTime.Today, Enabled = true, ClientId = Guid.NewGuid().ToString("N"), }); }
/// <summary> /// Builds a Client Entity. /// </summary> /// <param name="dto">ClientBindingDto</param> /// <returns></returns> public Client client(ClientBindingDto dto) { var FullEntity = new Client { //JB. Assign a new random ID. ClientId = Guid.NewGuid().ToString("N"), ClientName = dto.ClientName, Description = dto.Description, ClientUri = dto.ClientUri, LogoUri = dto.LogoUri, Enabled = true, }; return(FullEntity); }
//[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)); } }
//[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(Dto.Responses.ErrorResponse))] public async Task <IActionResult> AddClient(ClientBindingDto dto) { return(Ok(await _repo.AddClient(_factory.CreateClientEntity(dto), dto))); }