ASP.NET Core分布式项目实战(Consent视图制作)
文章目录
任务19:Consent视图制作
按照上一节 Consent 的思路
在 mvcCookieAuthSample 项目的 Controllers 文件夹下新建一个 ConsentController
在 Views 文件夹下新建一个 Consent 文件夹,然后在该文件夹下新建一个 Index 视图
在 ViewModels 文件夹下新建一个 ConsentViewModel
在 ViewModels 文件夹下新建一个 ScopeViewModel
ScopeViewModel.cs
namespace mvcCookieAuthSample.ViewModels { public class ScopeViewModel { public string Name { get; set; } public string DisplayName { get; set; } public string Description { get; set; } public string Emphasize { get; set; } public string Required { get; set; } public bool Checked { get; set; } } }
这个定义来自于 IdentityServer4 官方文档中 Identity Resource 和 API Resource 的模型
https://identityserver4.readthedocs.io/en/latest/reference/identity_resource.html

ConsentViewModel.cs
namespace mvcCookieAuthSample.ViewModels { public class ConsentViewModel { public string ClientId { get; set; } public string ClientName { get; set; } public string ClientLogoUrl { get; set; } /// <summary> /// 是否允许第二次登录不需要再次授权 /// </summary> public bool AllowRemeberConsent { get; set; } // 对两种用户分别做出选择 public IEnumerable<ScopeViewModel> IdentityScopes { get; set; } public IEnumerable<ScopeViewModel> ResourceScopes { get; set; } } }
接下来需要把两个 ViewModel 的信息显示在 Index 里面,实现类似微博授权页面

_ScopeListitem.cshtml
@using mvcCookieAuthSample.ViewModels; @model ScopeViewModel <li> </li>
Index.cshtml
@using mvcCookieAuthSample.ViewModels; @model ConsentViewModel <p>Consent Page</p> <div class="row page-header"> <div class="col-sm-10"> @if (string.IsNullOrWhiteSpace(Model.ClientLogoUrl)) { <div><img src="@Model.ClientLogoUrl"/></div> } <h1> @Model.ClientName <small>希望使用您的账户</small> </h1> </div> </div> <div class="row"> <div class="col-sm-8"> <form asp-action="Index"> @if (Model.IdentityScopes.Any()) { <ul class="list-group"> @foreach (var scope in Model.IdentityScopes) { @Html.Partial("_ScopeListitem", scope) } </ul> } @if (Model.ResourceScopes.Any()) { <ul class="list-group"> @foreach (var scope in Model.IdentityScopes) { @Html.Partial("_ScopeListitem", scope) } </ul> } </form> </div> </div>
上半部分展示图标和提示,下半部分分为三部分,分别列出 IdentityScopes 和 ResourceScopes,以及选择是否记住,最后实现授权
课程链接
http://video.jessetalk.cn/course/explore
相关文章
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,瀑布与敏捷)–学习笔记
欢迎各位读者加入微信群一起学习交流,
在公众号后台回复“加群”即可~~

原文出处:微信公众号【郑子铭 DotNet NB】
原文链接:https://mp.weixin.qq.com/s/vwC-6jKC_MFH3t7FzYG7jA
本文观点不代表Dotnet9立场,转载请联系原作者。