ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记
任务16:oauth2 + oidc 实现 client部分
实现 client 之前启动一下上一节的 server,启动之前需要清除一些代码
注释 Program 的 MigrateDbContext
public static void Main(string[] args)
{
BuildWebHost(args)
//.MigrateDbContext<ApplicationDbContext>((context, services) => {
// new ApplicationDbContextSeed().SeedAsync(context, services)
// .Wait();
//})
.Run();
}
RegisterViewModel
[Required]
//[DataType(DataType.EmailAddress)]
//public string Email{get;set;}
public string UserName { get; set; }
启动程序,使用 Config 中的 TestUser 登录

登录成功,不过现在是在本地,接下来需要把它放到客户端里面
新建一个 Asp.Net Core MVC 网站 MvcClient
在 startup 的 ConfigureServices 中添加 Authentication
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "client";
options.ClientSecret = "secret";
options.SaveTokens = true;
});
}
在 startup 的 Configure 中的 UseMvc 前添加 Authentication
app.UseAuthentication();
在 Program 的 CreateWebHostBuilder 中配置 Urls
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5001")
.UseStartup<Startup>();
客户端设置为5001来启动,然后服务端设置为5000
mvcCookieAuthSample 的 Program
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseEnvironment("Development")
.UseUrls("http://localhost:5000")
.UseStartup<Startup>()
.Build();
修改服务端的 Config 配置跳转地址
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client()
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.Implicit,// 隐式模式
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:5001/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },
//AllowedScopes = {"api"},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OpenId,
}
}
};
}
客户端的 Controller 打上 Authorize 标签
[Authorize]
public class HomeController : Controller
修改客户端 launchSettings.json 中的 applicationUrl
"applicationUrl": "http://localhost:5001",
"sslPort": 0
启动服务端,客户端,可以看到跳转到登录界面

登录之后会跳转到 http://localhost:5001/

在客户端 About.cshtml 页面显示 identity 的 claims
@{
ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>
@*<p>Use this area to provide additional information.</p>*@
<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dt>@claim.Value</dt>
}
</dl>
启动程序,跳转之后,点击 About 进入 About 页面

主要返回了服务端 Config 中配置的信息
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
}
课程链接
http://video.jessetalk.cn/course/explore


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记的更多相关文章
- ASP.NET Core分布式项目实战
ASP.NET Core开发者成长路线图 asp.net core 官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/ ...
- 【笔记目录1】ASP.NET Core分布式项目实战
当前标签: ASP.NET Core分布式项目实战 共2页: 1 2 下一页 35.Docker安装Mysql挂载Host Volume GASA 2019-06-20 22:02 阅读:51 评论 ...
- 【笔记目录2】ASP.NET Core分布式项目实战
当前标签: ASP.NET Core分布式项目实战 共2页: 上一页 1 2 11.ClientCredential模式总结 GASA 2019-03-11 12:59 阅读:26 评论:0 10. ...
- 【ASP.NET Core分布式项目实战】(三)整理IdentityServer4 MVC授权、Consent功能实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理(内容可能会有部分,推荐看源视频学习) 前言 由于之前的博客都是基于其他的博客进行开发,现在重新整理一下方便 ...
- ASP.NET Core分布式项目实战-目录
前言 今年是2018年,发现已经有4年没有写博客了,在这4年的时光里,接触了很多的.NET技术,自己的技术也得到很大的进步.在这段时光里面很感谢张队长以及其他开发者一直对.NET Core开源社区做出 ...
- 【ASP.NET Core分布式项目实战】(二)oauth2 + oidc 实现 server部分
本博客根据http://video.jessetalk.cn/my/course/5视频整理(内容可能会有部分,推荐看源视频学习) 资料 我们基于之前的MvcCookieAuthSample来做开发 ...
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...
- 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像
Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...
- 【ASP.NET Core分布式项目实战】(六)Gitlab安装
Gitlab GitLab是由GitLabInc.开发,使用MIT许可证的基于网络的Git仓库管理工具,且具有wiki和issue跟踪功能.使用Git作为代码管理工具,并在此基础上搭建起来的web服务 ...
- ASP.NET Core分布式项目-3.oauth2与open id connect 对比
oauth2 open id connect
随机推荐
- 如何实现纯网页语音视频聊天和桌面分享?(附源码,PC版+手机版)
在网页里实现文字聊天是比较容易的,但若要实现视频聊天,就比较麻烦了.本文将实现一个纯网页版的视频聊天和桌面分享的Demo,可直接在浏览器中运行,不需要安装任何插件. 一. 主要功能及支持平台 1.本D ...
- Canvas实现画布的缩放
主要介绍三种方式: 首先创建一个index.html文件 <!DOCTYPE html> <html lang="en"> <head> < ...
- 图文ASP.Net MVC Razor页面中HtmlHelper帮助程序的写法
将以下内容复制到cshtml文件中 @using Microsoft.AspNetCore.Html @{ ViewData["Title"] = ""; } ...
- LightOJ 1094
题意:就是求一个树的直径,也就是求任意两点的最大距离. 做法:跑两遍DFS,详见代码. #include<iostream> #include<cstdio> #include ...
- md文件的基本常用编写语法
md简介 .md即markdown文件的基本常用编写语法,是一种快速标记.快速排版语言,现在很多前段项目中的说明文件readme等都是用.md文件编写的,而且很多企业也在在鼓励使用这种编辑方式.下面就 ...
- 异步httpClient(Async HttpClient)
一.简介 二.mvn依赖 三.客户端 3.1 官网实例 3.2. 根据官方文档的介绍,简单封装了一个异步HttpClient工具类 3.3 基本原理 四.参考文档 一.简介 HttpClient提供了 ...
- CoreDNS -- DNS服务与服务发现
CoreDNS -- DNS服务与服务发现 DNS服务器 手册:https://coredns.io/manual/toc/ Github:https://github.com/coredns/cor ...
- [转帖]细说ASCII、GB2312/GBK/GB18030、Unicode、UTF-8/UTF-16/UTF-32编码
参考: <编码标准-GB2312 GBK GB18030> <字符编码笔记:ASCII,Unicode 和 UTF-8> <字体编辑用中日韩汉字Unicode编码表> ...
- [转帖]【SQL SERVER】锁机制
https://www.cnblogs.com/WilsonPan/p/12618849.html 锁定是 SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 基 ...
- [转帖]TiDB Control 使用说明
https://docs.pingcap.com/zh/tidb/stable/tidb-control TiDB Control 是 TiDB 的命令行工具,用于获取 TiDB 状态信息,多用于调试 ...