做网站需要多少职务,百事可乐运用了哪种网络营销方式,机械加工网名怎么起,信阳seo公司任务22#xff1a;Consent 确认逻辑实现接下来#xff0c;我们会在上一节的基础上添加两个按钮#xff0c;同意和不同意#xff0c;点击之后会把请求 post 到 ConsentController 处理#xff0c;如果同意会通过 return url 跳转到客户端#xff0c;如果不同意就会取消Consent 确认逻辑实现接下来我们会在上一节的基础上添加两个按钮同意和不同意点击之后会把请求 post 到 ConsentController 处理如果同意会通过 return url 跳转到客户端如果不同意就会取消同时客户端也会进行处理首先完善 ViewModel我们接收的是按钮同意或者不同意以及勾选的 checkbox最终以 ScopesConsented 的形式返回一个 stringvalue 是选中的 scope 的名称在 ViewModels 下新建 InputConsentViewModel用于接收 Consented 信息InputConsentViewModelpublic class InputConsentViewModel
{public string Button { get; set; }public IEnumerablestring ScopesConsented { get; set; }public bool RememberConsent { get; set; }public string ReturnUrl { get; set; }
}
ReturnUrl 是 AccountController 传到 ConsentController 的它们之间是通过 get 来传的传完之后我们在 ConsentController 的 Index 中拿到我们需要把它绑定到 ConsentViewModel因为它最终需要通过 post 发回来ConsentController[HttpPost]
public async TaskIActionResult Index(InputConsentViewModel viewModel)
{viewModel.ReturnUrl
}
这里面可以拿到 ReturnUrl那它是怎么过来的呢我们需要在 Consent 的 view 表单 index.cshtml 里面把它填过了至少需要一个比如 hidden 控件它里面需要有一个 ReturnUrlindex.cshtmlform asp-actionIndex methodpost
input typehidden asp-forReturnUrl/
同时需要在 ConsentViewModel 中加入 ReturnUrlConsentViewModelpublic string ReturnUrl { get; set; }
接着可以在 ConsentController 的 BuildConsentViewModel 中给 Viewmodel 赋值 ReturnUrlConsentControllervar vm CreateConsentViewModel(request, client, resources);
vm.ReturnUrl returnUrl;
return vm;
完成之后客户端就可以 index.cshtml 中展示的时候有一个隐藏的 ReturnUrl它最终在 post 的时候会被包含到整个 Form 表单所以我们可以在 ConsentController 的 Index 中拿到 viewModel 的 ReturnUrl当我们点击“是”之后会跳转到客户端如果点击“否”也会跳转回去所以我们需要在 ConsentController 的 index 中接收然后 Redirect 到一个 url那么在什么地方拿这个 url 呢我们会用到之前讲到 InteractionServiceConsentController[HttpPost]
public async TaskIActionResult Index(InputConsentViewModel viewModel)
{ConsentResponse consentResponse null;if (viewModel.Button no){consentResponse ConsentResponse.Denied;}else if (viewModel.Button yes){if (viewModel.ScopesConsented ! null viewModel.ScopesConsented.Any()){consentResponse new ConsentResponse{RememberConsent viewModel.RememberConsent,ScopesConsented viewModel.ScopesConsented,};}}if (consentResponse ! null){var request await _identityServerInteractionService.GetAuthorizationContextAsync(viewModel.ReturnUrl);await _identityServerInteractionService.GrantConsentAsync(request, consentResponse);return Redirect(viewModel.ReturnUrl);}return View();
}
接着在 Consent 的视图中补充显示同意按钮以及 RememberIndex.cshtmldiv classrowdiv classcol-sm-8form asp-actionIndex methodpostinput typehidden asp-forReturnUrl/if (Model.IdentityScopes.Any()){div classpaneldiv classpanel-headingspan classglyphicon glyphicon-user/span用户信息/divul classlist-groupforeach (var scope in Model.IdentityScopes){Html.Partial(_ScopeListitem, scope)}/ul/div}if (Model.ResourceScopes.Any()){div classpaneldiv classpanel-headingspan classglyphicon glyphicon-tasks/span应用权限/divul classlist-groupforeach (var scope in Model.IdentityScopes){Html.Partial(_ScopeListitem, scope)}/ul/div}divlabelinput typecheckbox asp-forRememberConsent/strong记住我的选择/strong/label/divdivbutton namebutton valueyes classbtn btn-primary autofocus同意/buttonbutton namebutton valueno取消/buttonif (!string.IsNullOrEmpty(Model.ClientUrl)){a hrefModel.ClientUrl classpull-right btn btn-defaultspan classglyphicon glyphicon-info-sign/spanstrongModel.ClientUrl/strong/a}/div/form/div
/div
因为最终 AllowRemeberConsent 的 checkbox 需要 psot 回去就是在 InputConsentViewModel 中有一个 RememberConsent所以我们需要把 ConsentViewModel 的 AllowRemeberConsent 改为 RememberConsent因为 RememberConsent 与 ReturnUrl 这两个属性与 InputConsentViewModel 中一致所以直接继承ConsentViewModelpublic class ConsentViewModel : InputConsentViewModel
{public string ClientId { get; set; }public string ClientName { get; set; }public string ClientLogoUrl { get; set; }public string ClientUrl { get; set; }// 对两种用户分别做出选择public IEnumerableScopeViewModel IdentityScopes { get; set; }public IEnumerableScopeViewModel ResourceScopes { get; set; }
}
ConsentControllervm.RememberConsent client.AllowRememberConsent;
因为在 Config.cs 中传了两个 ResourcesConfigIdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OpenId,
OpenId 是必须需要的因为客户端接收的时候使用的是 oidc它会根据 OpenId 获取用户信息Startup.AddOpenIdConnect(oidc, options
所以我们需要在 _ScopeListitem.cshtml 中把选中的 scope 传回去_ScopeListitem.cshtmlinput typecheckboxnameScopesConsentedidscopes_Model.NamevalueModel.NamecheckedModel.CheckeddisabledModel.Required/if (Model.Required){input typehidden nameScopesConsented valueModel.Name/}
在 Conifg 中添加上 ClaimsConifgpublic static ListTestUser GetTestUsers()
{return new ListTestUser{new TestUser{SubjectId 1,Username mingsonzheng,Password 123456,Claims new ListClaim{new Claim(name, mingson),new Claim(website, https://www.cnblogs.com/MingsonZheng/),}}};
}
启动服务端再启动客户端访问 http://localhost:5001/自动跳转到 5000 登录登录之后进入授权界面勾选 profile 点击同意跳转到 5001说明登录成功点击 About查看返回信息可以看到带回来了 Conifg 里面的信息这些信息包含在 Profile 中返回回来的Conifgnew IdentityResources.Profile(),
现在我们已经走完了流程后面会在这个基础之上进行重构课程链接http://video.jessetalk.cn/course/explore相关文章ASP.NET Core分布式项目实战运行Consent Page--学习笔记ASP.NET Core分布式项目实战Consent Controller Get请求逻辑实现--学习笔记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.NETnbsp;Core分布式项目实战第三方ClientCredential模式调用--学习笔记ASP.NET Core分布式项目实战客户端集成IdentityServer--学习笔记ASP.NET Core分布式项目实战业务介绍架构设计oAuth2IdentityServer4--学习笔记ASP.NET Core分布式项目实战课程介绍MVP瀑布与敏捷--学习笔记ASP.NET Core快速入门 -- 学习笔记汇总