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
随机推荐
- 嵌入式Linux必读经典书籍(含下载方式)
最近,在知乎看到一个问题,"嵌入式Linux有哪些好书推荐".我读研期间也喜欢收藏一些书籍,每次看到京东有活动,总是忍不住想买一些书籍回来. 随着时间越来越久,我买的书越来越多,但 ...
- vue权限管理
https://www.bilibili.com/video/BV1nq4y1i7BU/?spm_id_from=333.788.recommend_more_video.6&vd_sourc ...
- RL 基础 | 如何注册自定义 gym 环境
如何 搭建 自定义 gym 环境:https://www.cnblogs.com/moonout/p/17174833.html 如何注册自定义 gym 环境: 博客:https://zhuanlan ...
- 部署开源项目管理工具focalboard
前言 focalboard是一款开源项目管理工具,类似Jira.Trello.官网地址 组件 版本 说明 Debian 12.1 操作系统 docker 20.10.7 容器运行时 docker-co ...
- 【C/C++】函数入参检查
// 统计变参数量 #define CALC_VA_COUNT(arg...) \ ({ \ int count = 0; \ int insideQuotes = 0; \ const char * ...
- [java] - servlet路径跳转
Index.jsp <a href="servlet/HelloServlet">servlet/HelloServlet</a><br> &l ...
- [转帖]JMeter 接口测试快速入门
https://my.oschina.net/choerodon/blog/5289725 JMeter简介 JMeter 的特性: 对于多种协议的功能测试和性能测试 Web - HTTP, HT ...
- [转帖]Prometheus-使用python开发exporter
exporter有很多,但想要特定需求的话,还需自行开发.在这里使用python写一个exporter,用于监控/root下的目录数量. 开发exporter需要使用prometheus_client ...
- [转帖]expect 实现 ssh免密登录的脚本
expect 实现 ssh免密登录的脚本 #!/bin/bash #Author:cosann #Version:0.2 #date:2022/7/27 #description:批量部署SSH免密登 ...
- Ergonomics JVM 的一种FullGC的说明
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html 2 Ergonomics Ergo ...