/// <summary> /// GetGrantTypesFromViewModel /// </summary> /// <param name="model"></param> /// <returns></returns> private static ICollection <string> GetGrantTypesFromViewModel(ApiClientCreateViewModel model) { var result = new Collection <string>(); if (model.AuthorizationCodeGrantType) { result.Add(GrantType.AuthorizationCode); } if (model.ImplicitGrantType) { result.Add(GrantType.Implicit); } if (model.ClientCredentialsGrantType) { result.Add(GrantType.ClientCredentials); } if (model.HybridGrantType) { result.Add(GrantType.Hybrid); } if (model.ResourceOwnerPasswordGrantType) { result.Add(GrantType.ResourceOwnerPassword); } return(result); }
public IActionResult Create() { var model = new ApiClientCreateViewModel { AvailableApiScopes = GetAvailableApiScopes() }; return(View(model)); }
public async Task <IActionResult> Create(ApiClientCreateViewModel model) { if (!ModelState.IsValid) { model.AvailableApiScopes = GetAvailableApiScopes(); return(View()); } try { var cl = new Client { ClientName = model.ClientName, ClientId = model.ClientId, AllowedGrantTypes = GetGrantTypesFromViewModel(model), ClientUri = model.ClientUri, AllowOfflineAccess = true, ClientSecrets = new List <Secret> { new Secret("very_secret".Sha256()) } }; var parsed = cl.ToEntity(); parsed.Id = await _configurationDbContext.Clients.MaxAsync(x => x.Id) + 1; foreach (var item in parsed.AllowedGrantTypes) { item.Id = new Random().Next(1, 9999); } _configurationDbContext.Clients.Add(parsed); await _configurationDbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateException ex) { if (ex.InnerException is SqlException exception) { switch (exception.Number) { case 2601: ModelState.AddModelError(string.Empty, "The API Client Already Exists"); //_logger.LogError(exception, "The API Client already exists"); break; default: ModelState.AddModelError(string.Empty, "An unknown error occured"); //_logger.LogError(exception, "Unknown sql error"); break; } } else { //_logger.LogError(ex, "A db error occured"); ModelState.AddModelError(string.Empty, ex.Message); } } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); } model.AvailableApiScopes = GetAvailableApiScopes(); return(View(model)); }