Identity Server 4 从入门到落地(三)—— 创建Web客户端
书接上回,我们已经搭建好了基于Identity Server 4的认证服务和管理应用(如果还没有搭建,参看本系列前两部分,相关代码可以从github下载:https://github.com/zhenl/IDS4Admin )。
现在我们来创建Web客户端。基本上可以按照Identity Server官网教程来: https://identityserver4.readthedocs.io/en/latest/quickstarts/2_interactive_aspnetcore.html 。只不过我使用的是.Net 6,代码上有少许出入。
首先创建一个Asp.Net Core Web项目,使用MVC项目模板,在创建时选择不需要身份认证。然后引入程序包 Microsoft.AspNetCore.Authentication.OpenIdConnect。
接下来修改Program.cs,.Net 6引入了简洁模式,代码看上去有些不同。
using System.IdentityModel.Tokens.Jwt;
var builder = WebApplication.CreateBuilder(args);
//增加的代码
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:4010";
options.RequireHttpsMetadata = false;
options.ClientId = "myclient";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});
//增加结束
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); //增加的代码
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.RequireAuthorization(); //增加的代码
app.Run();
增加的代码进行了注释。需要注意的是,由于我们的测试认证服务器运行在http://localhost:4010,没有使用HTTPS协议,所以增加了options.RequireHttpsMetadata = false。
还需要修改lanuch.json中的代码,将项目改变为自启动项目,不依赖IIS Express:
{
"profiles": {
"IDS4Client": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7002;http://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
最后,修改Index.cshtml,显示用户信息:
@{
ViewData["Title"] = "Home Page";
}
@using Microsoft.AspNetCore.Authentication
<h2>Claims</h2>
<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>
}
</dl>
<h2>Properties</h2>
<dl>
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
{
<dt>@prop.Key</dt>
<dd>@prop.Value</dd>
}
</dl>
客户端就编写完成了,然后使用管理应用(我这里是http://localhost:4003)向认证服务的数据库中增加这个客户端的定义。设置项很多,可以参考现有的管理客户端进行设置,也可以克隆现有的客户端进行修改。主要的设置是基本信息和认证注销部分,特别注意别忘了设置客户端密钥,并且密钥需要与客户端代码中的相同,还有就是允许作用域也要和代码中的相同。这两部分的截图如下:


设置完成后,访问这个客户端http://localhost:7002,会重定位到认证服务器提示登录,登录后,会出现下面的界面:

同意确认后,回到我们需要访问的界面:

最后,增加登出链接,将这个链接增加到_Layout.chstml中,在菜单代码下面增加这个链接:
<ul class="navbar-nav">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Logout">Logout</a>
</ul>
简化起见,我们在HomeController中增加登出代码,很简单:
public IActionResult Logout()
{
return SignOut("Cookies", "oidc");
}
到此,采用Identity Server 4进行认证管理的Web客户端就初步搭建完成了。下一步,我们需要搭建一个受Identity Server 4 保护的Web Api。
上述代码可以github下载: https://github.com/zhenl/IDS4ClientDemo,喜欢的话给个star。
Identity Server 4 从入门到落地(三)—— 创建Web客户端的更多相关文章
- Identity Server 4 从入门到落地(九)—— 客户端User和Role的解析
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(十)—— 编写可配置的客户端和Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(十一)—— Docker部署
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(十二)—— 使用Nginx集成认证服务
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(四)—— 创建Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(六)—— 简单的单页面客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(七)—— 控制台客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(八)—— .Net Framework 客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
随机推荐
- Shadertoy 教程 Part 2 - 圆和动画
Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...
- Logic strength modeling
7.9 Verilog HDL提供了信号争用.双向通过门.电阻式MOS器件.动态MOS.电荷共享的精确建模,并通过允许标量净信号值具有全范围的未知值和不同强度级别或强度级别的组合来实现其他依赖于技术的 ...
- 数值的整数次方 牛客网 剑指Offer
数值的整数次方 牛客网 剑指Offer 题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方 class Solution: #run ...
- 『学了就忘』Linux基础命令 — 19、目录操作的相关命令
目录 1.ls命令 2.cd命令 (1)绝对路径和相对路径 (2)cd命令的简化用法 3.pwd命令 4.mkdir命令 5.rmdir命令 常用目录操作的相关命令: ls命令 cd命令 pwd命令 ...
- Win powershell执行策略配置
参考连接:https://blog.csdn.net/jeffxu_lib/article/details/84710386 参考连接:http://www.cragsman.org/index.ph ...
- Qt 隐藏标题栏后实现窗口拖动、设置窗口透明
隐藏标题栏 setWindowFlags(Qt::CustomizeWindowHint); setWindowFlags(Qt::FramelessWindowHint); 两个函数都可以去掉标题栏 ...
- Maven打包web项目报错:webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update)
问题描述 使用Maven打包项目的时候,出现错误: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing ...
- webpack 打包图片资源
webpack 打包图片资源 /** * loader: 1. 下载 2. 使用(配置) * plugins:1. 下载 2. 引入 3.使用 */ // 用来拼接绝对路径的方法 const {res ...
- Linux基础一:基础命令
Linux是什么,是干什么用的? 1.Linux是一个操作系统,电脑=软件+硬件,而操作系统就是特殊的软件 2.Linux系统内一切皆文件 3.bash shell 是红帽默认的shell(shell ...
- linux 系统ssh超时设置
1.修改client端的etc/ssh/ssh_config添加以下:(在没有权限改server配置的情形下) ServerAliveInterval 60 #client每隔60秒发送一次请求给se ...