任务20:Consent Controller Get请求逻辑实现
接着上一节的思路,实现一下 ConsentController
根据流程图在构造函数注入 IClientStore,IResourceStore,IIdentityServerInteractionService
构造函数
private readonly IClientStore _clientSotre;
private readonly IResourceStore _resourceStore;
private readonly IIdentityServerInteractionService _identityServerInteractionService;private ConsentController(IClientStore clientStore,IResourceStore resourceStore,IIdentityServerInteractionService identityServerInteractionService)
{_clientSotre = clientStore;_resourceStore = resourceStore;_identityServerInteractionService = identityServerInteractionService;
}
Index
public IActionResult Index(string returnUrl)
{var model = BuildConsentViewModel(returnUrl);if (model == null){}return View(model);
}
BuildConsentViewModel
private async Task<ConsentViewModel> BuildConsentViewModel(string returnUrl)
{var request = await _identityServerInteractionService.GetAuthorizationContextAsync(returnUrl);if (request == null)return null;var client = await _clientSotre.FindEnabledClientByIdAsync(request.ClientId);var resources = await _resourceStore.FindEnabledResourcesByScopeAsync(request.ScopesRequested);return CreateConsentViewModel(request, client, resources);
}
CreateConsentViewModel
private ConsentViewModel CreateConsentViewModel(AuthorizationRequest request, Client client, Resources resources)
{var vm = new ConsentViewModel();vm.ClientName = client.ClientName;vm.ClientLogoUrl = client.LogoUri;vm.ClientUrl = client.ClientUri;vm.AllowRemeberConsent = client.AllowRememberConsent;vm.IdentityScopes = resources.IdentityResources.Select(i => CreateScopeViewModel(i));vm.ResourceScopes = resources.ApiResources.SelectMany(i => i.Scopes).Select(x => CreateScopeViewModel(x));return vm;
}
在获取 vm.ResourceScopes 的时候我们用到了 SelectMany,如果我们使用 resources.ApiResources.Select 的话,我们会得到一个 List<List>,而我们想要得到的是一个 List,所以通过 SelectMany 会把 List<List> 展开得到里面的每一个 List
CreateScopeViewModel
private ScopeViewModel CreateScopeViewModel(IdentityResource identityResource)
{return new ScopeViewModel{Name = identityResource.Name,DisplayName = identityResource.DisplayName,Description = identityResource.Description,Required = identityResource.Required,Checked = identityResource.Required,Emphasize = identityResource.Emphasize,};
}private ScopeViewModel CreateScopeViewModel(Scope scope)
{return new ScopeViewModel{Name = scope.Name,DisplayName = scope.DisplayName,Description = scope.Description,Required = scope.Required,Checked = scope.Required,Emphasize = scope.Emphasize,};
}
ConsentViewModel 添加 ClientUrl
public string ClientUrl { get; set; }
ScopeViewModel 修改字段类型为 bool
public bool Emphasize { get; set; }
public bool Required { get; set; }
课程链接
http://video.jessetalk.cn/course/explore
相关文章
ASP.NET Core分布式项目实战(Consent视图制作)--学习笔记
ASP.NET Core分布式项目实战(Identity Server 4回顾,Consent 实现思路介绍)--学习笔记
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记
ASP.NET Core分布式项目实战(oauth2与open id connect 对比)--学习笔记
ASP.NET Core分布式项目实战(详解oauth2授权码流程)--学习笔记
ASP.NET Core分布式项目实战(oauth密码模式identity server4实现)--学习笔记
ASP.NET Core分布式项目实战(第三方ClientCredential模式调用)--学习笔记
ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记
ASP.NET Core分布式项目实战(业务介绍,架构设计,oAuth2,IdentityServer4)--学习笔记
ASP.NET Core分布式项目实战(课程介绍,MVP,瀑布与敏捷)--学习笔记
ASP.NET Core快速入门 -- 学习笔记汇总