The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GrantsController.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using IdentityServer4.Services;
  2. using IdentityServer4.Stores;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Teknik.IdentityServer.Security;
  9. using Teknik.IdentityServer.ViewModels;
  10. using Teknik.Logging;
  11. using Microsoft.Extensions.Logging;
  12. using Teknik.Configuration;
  13. namespace Teknik.IdentityServer.Controllers
  14. {
  15. /// <summary>
  16. /// This sample controller allows a user to revoke grants given to clients
  17. /// </summary>
  18. [Authorize(AuthenticationSchemes = "Identity.Application")]
  19. public class GrantsController : DefaultController
  20. {
  21. private readonly IIdentityServerInteractionService _interaction;
  22. private readonly IClientStore _clients;
  23. private readonly IResourceStore _resources;
  24. public GrantsController(
  25. ILogger<Logger> logger,
  26. Config config,
  27. IIdentityServerInteractionService interaction,
  28. IClientStore clients,
  29. IResourceStore resources) : base(logger, config)
  30. {
  31. _interaction = interaction;
  32. _clients = clients;
  33. _resources = resources;
  34. }
  35. /// <summary>
  36. /// Show list of grants
  37. /// </summary>
  38. [HttpGet]
  39. public async Task<IActionResult> Index()
  40. {
  41. ViewBag.Title = "Granted Applications";
  42. return View("Index", await BuildViewModelAsync());
  43. }
  44. /// <summary>
  45. /// Handle postback to revoke a client
  46. /// </summary>
  47. [HttpPost]
  48. [ValidateAntiForgeryToken]
  49. public async Task<IActionResult> Revoke(string clientId)
  50. {
  51. await _interaction.RevokeUserConsentAsync(clientId);
  52. return RedirectToAction("Index");
  53. }
  54. private async Task<GrantsViewModel> BuildViewModelAsync()
  55. {
  56. var grants = await _interaction.GetAllUserConsentsAsync();
  57. var list = new List<GrantViewModel>();
  58. foreach(var grant in grants)
  59. {
  60. var client = await _clients.FindClientByIdAsync(grant.ClientId);
  61. if (client != null)
  62. {
  63. var resources = await _resources.FindResourcesByScopeAsync(grant.Scopes);
  64. var item = new GrantViewModel()
  65. {
  66. ClientId = client.ClientId,
  67. ClientName = client.ClientName ?? client.ClientId,
  68. ClientLogoUrl = client.LogoUri,
  69. ClientUrl = client.ClientUri,
  70. Created = grant.CreationTime,
  71. Expires = grant.Expiration,
  72. IdentityGrantNames = resources.IdentityResources.Select(x => x.DisplayName ?? x.Name).ToArray(),
  73. ApiGrantNames = resources.ApiResources.Select(x => x.DisplayName ?? x.Name).ToArray()
  74. };
  75. list.Add(item);
  76. }
  77. }
  78. return new GrantsViewModel
  79. {
  80. Grants = list
  81. };
  82. }
  83. }
  84. }