IdentityServer4揭秘---Consent(同意页面)

授权同意页面与登录一样首先要分析页面的需要什么模型元素后建立相关的模型类

界面的话就 记住选择  、按钮、RuturnUrl、以及选择的资源Scope

  1. /// <summary>
  2. /// 主要绑定Consent界面上的一些模型
  3. /// </summary>
  4. public class ConsentViewModel
  5. {
  6. public string ReturnUrl { get; set; }
  7. public bool RememberConsent { get; set; }
  8. public string Button { get; set; }
  9. public IEnumerable<string> ScopesConsented { get; set; }
  10. }

ConsentViewModel

这里可以注意到还有Idr4的对应数据 比如客户端的一些信息,如名称、Logo、客户端的授权Scope等等、这里根据需要可以多写一些

  1. /// <summary>
  2. /// 主要绑定Idr4中关于Consent界面交互的实体字段
  3. /// </summary>
  4. public class Idr4ConsentViewModel : ConsentViewModel
  5. {
  6. public string ClientName { get; set; }
  7. public string ClientUrl { get; set; }
  8.  
  9. public string ClientLogoUrl { get; set; }
  10.  
  11. public bool AllowRememberConsent { get; set; }
  12.  
  13. public IEnumerable<Idr4ScopeViewModel> IdentityScopes { get; set; }
  14. public IEnumerable<Idr4ScopeViewModel> ResouceScopes { get; set; }
  15. }

Idr4ConsentViewModel

这里同样需要构建Idr4Consent页面展示模型

  1. private async Task<Idr4ConsentViewModel> CreateIdr4ConsentViewModelAsync(string ReturnUrl)
  2. {
  3. var request = await _identityServerInteractionService.GetAuthorizationContextAsync(ReturnUrl);
  4. if (request != null)
  5. {
  6. //通过客户端id获取客户端信息
  7. var clientModel = await _clientStore.FindEnabledClientByIdAsync(request.ClientId);
  8. if (clientModel != null)
  9. {
  10. //获取资源Scope信息 这里包括了两种 一种是IdentityResource 和ApiResource
  11.  
  12. var resources = await _resourceStore.FindEnabledResourcesByScopeAsync(request.ScopesRequested);
  13. //获取所有的权限
  14.  
  15. // var resources = await _resourceStore.FindEnabledResourcesByScopeAsync(clientModel.AllowedScopes);
  16.  
  17. if (resources != null && (resources.ApiResources.Any() || resources.IdentityResources.Any()))
  18. {
  19. //构造界面需要的模型
  20.  
  21. var vm = new Idr4ConsentViewModel();
  22.  
  23. //界面初始化时候
  24. vm.RememberConsent = true; //默认true
  25. vm.ScopesConsented = Enumerable.Empty<string>();
  26. vm.ReturnUrl = ReturnUrl;
  27. //构建关于Client的信息
  28. vm.ClientName = clientModel.ClientName;
  29. vm.ClientUrl = clientModel.ClientUri;
  30. vm.ClientLogoUrl = clientModel.LogoUri;
  31. vm.AllowRememberConsent = clientModel.AllowRememberConsent;
  32. vm.IdentityScopes = resources.IdentityResources.Select(x => new Idr4ScopeViewModel
  33. {
  34. Name = x.Name,
  35. DisplayName = x.DisplayName,
  36. Description = x.Description,
  37. Emphasize = x.Emphasize,
  38. Required = x.Required,
  39. Checked = vm.ScopesConsented.Contains(x.Name) || x.Required
  40. }).ToArray();
  41. vm.ResouceScopes = resources.ApiResources.SelectMany(x => x.Scopes).Select(k => new Idr4ScopeViewModel
  42. {
  43. Name = k.Name,
  44. DisplayName = k.DisplayName,
  45. Description = k.Description,
  46. Emphasize = k.Emphasize,
  47. Required = k.Required,
  48. Checked = vm.ScopesConsented.Contains(k.Name) || k.Required
  49.  
  50. }).ToArray();
  51. //离线
  52. if (ConsentOptions.EnableOfflineAccess && resources.OfflineAccess)
  53. {
  54. vm.ResouceScopes = vm.ResouceScopes.Union(new Idr4ScopeViewModel[] {
  55. new Idr4ScopeViewModel{
  56.  
  57. Name = IdentityServer4.IdentityServerConstants.StandardScopes.OfflineAccess,
  58. DisplayName = ConsentOptions.OfflineAccessDisplayName,
  59. Description = ConsentOptions.OfflineAccessDescription,
  60. Emphasize = true,
  61. Checked = vm.ScopesConsented.Contains(IdentityServer4.IdentityServerConstants.StandardScopes.OfflineAccess)
  62. }
  63. });
  64. }
  65. return vm;
  66. }
  67. else
  68. {
  69. //客户端Scope不存在 可以在界面提示并记录日志
  70. return null;
  71. }
  72.  
  73. }
  74. else
  75. {
  76. //客户端不存在 可以在界面提示并记录日志
  77. return null;
  78.  
  79. }
  80.  
  81. }
  82. return null;
  83. }

CreateIdr4ConsentViewModelAsync

里面具体的话无非就是获取更具ReturnUrl地址获取用户交互接口相关的数据信息以及页面Scope绑定以及获取

值得注意的 选项required这种情况在界面上是 disabled属性 后台Action中是获取不到的,所以需要加一些影藏域

这里是Get Consent

  1. [HttpGet]
  2. public async Task<IActionResult> Consent(string ReturnUrl)
  3. {
  4. //获取请求授权信息
  5. var vm = await CreateIdr4ConsentViewModelAsync(ReturnUrl);
  6. if (vm != null)
  7. {
  8. return View(vm);
  9. }
  10. return View();
  11. }

Consent Get

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task<IActionResult> Consent(Idr4ConsentViewModel model)
  4. {
  5.  
  6. ConsentResponse consentResponse = null;
  7.  
  8. if (model == null)
  9. {
  10. ModelState.AddModelError("", "数据发送异常");
  11. }
  12. //有没有选择授权
  13.  
  14. if (model.ScopesConsented == null || model.ScopesConsented.Count() == )
  15. {
  16. ModelState.AddModelError("", "请至少选择一个权限");
  17. }
  18.  
  19. //同意授权
  20. if (model.Button == "yes")
  21. {
  22. //选择了授权Scope
  23. if (model.ScopesConsented != null && model.ScopesConsented.Any())
  24. {
  25. var scopes = model.ScopesConsented;
  26. if (ConsentOptions.EnableOfflineAccess == false)
  27. {
  28. scopes = scopes.Where(x => x != IdentityServer4.IdentityServerConstants.StandardScopes.OfflineAccess);
  29. }
  30.  
  31. consentResponse = new ConsentResponse
  32. {
  33. RememberConsent = model.RememberConsent,
  34. ScopesConsented = scopes
  35. };
  36.  
  37. }
  38. }
  39. //不同意授权
  40. else if (model.Button == "no")
  41. {
  42. consentResponse = ConsentResponse.Denied;
  43. }
  44. else
  45. {
  46. var vm1 = await CreateIdr4ConsentViewModelAsync(model.ReturnUrl);
  47. return View(vm1);
  48.  
  49. }
  50.  
  51. //无论同意还是不同意都是需要跳转
  52. if (consentResponse != null)
  53. {
  54.  
  55. var request = await _identityServerInteractionService.GetAuthorizationContextAsync(model.ReturnUrl);
  56. if (request == null)
  57. {
  58. ModelState.AddModelError("", "客户端登录验证不匹配");
  59. }
  60. //if (consentResponse == ConsentResponse.Denied)
  61. //{
  62. // string url = new Uri(request.RedirectUri).Authority;
  63. // return Redirect(url);
  64. //}
  65.  
  66. //沟通Idr4服务端实现授权
  67. await _identityServerInteractionService.GrantConsentAsync(request, consentResponse);
  68.  
  69. return Redirect(model.ReturnUrl);
  70.  
  71. }
  72.  
  73. var vm = await CreateIdr4ConsentViewModelAsync(model.ReturnUrl);
  74. if (vm != null)
  75. {
  76. return View(vm);
  77. }
  78.  
  79. return View();
  80. }

Consent Post

  1. @using SSOServer.Models;
  2. @model Idr4ConsentViewModel
  3. @{
  4. Layout = null;
  5. }
  6.  
  7. <!DOCTYPE html>
  8.  
  9. <html>
  10. <head>
  11. <meta name="viewport" content="width=device-width" />
  12. <title>确认授权页面</title>
  13. </head>
  14. <body>
  15. <div>
  16. <div><img src="@Model.ClientLogoUrl" width="" height="" /></div>
  17. <div>@Model.ClientName</div>
  18. <div><a href="@Model.ClientUrl" target="_blank"> @Model.ClientUrl</a></div>
  19. </div>
  20. <div>
  21. <div asp-validation-summary="All"></div>
  22. <form asp-action="Consent" class="consent-form">
  23. <input type="hidden" asp-for="ReturnUrl" />
  24. <div>请求你的授权</div>
  25.  
  26. @if (Model.IdentityScopes.Any())
  27. {
  28. <div class="panel panel-default consent-buttons">
  29. <div class="panel-heading">
  30. <span class="glyphicon glyphicon-user"></span>
  31. 个人信息
  32. </div>
  33. <ul class="list-group">
  34. @foreach (var scope in Model.IdentityScopes)
  35. {
  36. <li class="list-group-item">
  37. <label>
  38. <input class="consent-scopecheck"
  39. type="checkbox"
  40. name="ScopesConsented"
  41. id="scopes_@scope.Name"
  42. value="@scope.Name"
  43. checked="@scope.Checked"
  44. disabled="@scope.Required" />
  45. @if (scope.Required)
  46. {
  47. <input type="hidden"
  48. name="ScopesConsented"
  49. value="@scope.Name" />
  50. }
  51. <strong>@scope.DisplayName</strong>
  52. @if (scope.Emphasize)
  53. {
  54. <span class="glyphicon glyphicon-exclamation-sign"></span>
  55. }
  56. </label>
  57. @if (scope.Required)
  58. {
  59. <span><em>(必需)</em></span>
  60. }
  61. @if (scope.Description != null)
  62. {
  63. <div class="consent-description">
  64. <label for="scopes_@scope.Name">@scope.Description</label>
  65. </div>
  66. }
  67. </li>
  68. }
  69. </ul>
  70. </div>
  71. }
  72. @if (Model.ResouceScopes.Any())
  73. {
  74. <div class="panel panel-default">
  75. <div class="panel-heading">
  76. <span class="glyphicon glyphicon-tasks"></span>
  77. 应用授权
  78. </div>
  79. <ul class="list-group">
  80. @foreach (var scope in Model.ResouceScopes)
  81. {
  82. <li class="list-group-item">
  83. <label>
  84. <input class="consent-scopecheck"
  85. type="checkbox"
  86. name="ScopesConsented"
  87. id="scopes_@scope.Name"
  88. value="@scope.Name"
  89. checked="@scope.Checked"
  90. disabled="@scope.Required" />
  91. @if (scope.Required)
  92. {
  93. <input type="hidden"
  94. name="ScopesConsented"
  95. value="@scope.Name" />
  96. }
  97. <strong>@scope.DisplayName</strong>
  98. @if (scope.Emphasize)
  99. {
  100. <span class="glyphicon glyphicon-exclamation-sign"></span>
  101. }
  102. </label>
  103. @if (scope.Required)
  104. {
  105. <span><em>(必需)</em></span>
  106. }
  107. @if (scope.Description != null)
  108. {
  109. <div class="consent-description">
  110. <label for="scopes_@scope.Name">@scope.Description</label>
  111. </div>
  112. }
  113. </li>
  114. }
  115. </ul>
  116. </div>
  117. }
  118. @if (Model.AllowRememberConsent)
  119. {
  120. <div class="consent-remember">
  121. <label>
  122. <input class="consent-scopecheck" asp-for="RememberConsent" />
  123. <strong>记住选择</strong>
  124. </label>
  125. </div>
  126. }
  127. <div class="consent-buttons">
  128. <button name="button" value="yes" class="btn btn-primary" autofocus>是, 允许</button>
  129. <button name="button" value="no" class="btn">否,不允许</button>
  130.  
  131. </div>
  132. </form>
  133.  
  134. </div>
  135.  
  136. </body>
  137. </html>

Consent View

posted @ 2021-03-26 11:05  dreamw  阅读(239)  评论(0编辑  收藏  举报