任务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部分)--学习笔记的更多相关文章

  1. ASP.NET Core分布式项目实战

    ASP.NET Core开发者成长路线图 asp.net core 官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/ ...

  2. 【笔记目录1】ASP.NET Core分布式项目实战

    当前标签: ASP.NET Core分布式项目实战 共2页: 1 2 下一页  35.Docker安装Mysql挂载Host Volume GASA 2019-06-20 22:02 阅读:51 评论 ...

  3. 【笔记目录2】ASP.NET Core分布式项目实战

    当前标签: ASP.NET Core分布式项目实战 共2页: 上一页 1 2  11.ClientCredential模式总结 GASA 2019-03-11 12:59 阅读:26 评论:0 10. ...

  4. 【ASP.NET Core分布式项目实战】(三)整理IdentityServer4 MVC授权、Consent功能实现

    本博客根据http://video.jessetalk.cn/my/course/5视频整理(内容可能会有部分,推荐看源视频学习) 前言 由于之前的博客都是基于其他的博客进行开发,现在重新整理一下方便 ...

  5. ASP.NET Core分布式项目实战-目录

    前言 今年是2018年,发现已经有4年没有写博客了,在这4年的时光里,接触了很多的.NET技术,自己的技术也得到很大的进步.在这段时光里面很感谢张队长以及其他开发者一直对.NET Core开源社区做出 ...

  6. 【ASP.NET Core分布式项目实战】(二)oauth2 + oidc 实现 server部分

    本博客根据http://video.jessetalk.cn/my/course/5视频整理(内容可能会有部分,推荐看源视频学习) 资料 我们基于之前的MvcCookieAuthSample来做开发 ...

  7. 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现

    本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...

  8. 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像

    Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...

  9. 【ASP.NET Core分布式项目实战】(六)Gitlab安装

    Gitlab GitLab是由GitLabInc.开发,使用MIT许可证的基于网络的Git仓库管理工具,且具有wiki和issue跟踪功能.使用Git作为代码管理工具,并在此基础上搭建起来的web服务 ...

  10. ASP.NET Core分布式项目-3.oauth2与open id connect 对比

    oauth2 open id connect

随机推荐

  1. prefer-rest-params

    使用剩余参数代替 arguments (prefer-rest-params) 剩余参数来自于ES2016.可以在可变函数中使用这个特性来替代arguments变量.arguments没有Array. ...

  2. vue prop 会接收不同的数据类型

    refAge: { type: Number, default: 0 }, refName: { type: String, default: '' }, hotDataLoading: { type ...

  3. 30 秒使用 Sealos 搭建个人密码管理器 Vaultwarden

    我与 LastPass 的曲折恋情 超过 8 年网龄的我,注册过很多网站帐号,每个网站的密码我都用不同的复杂密码.一开始我全靠脑力记忆这些密码,后来渐渐觉得记起来很困难,就记录在笔记本上.但是随着时间 ...

  4. P1228-递归【黄】

    这道大递归我一开始就找对了方向,不过了MLE,然后从网上搜索到了一个贼有用的概念--尾递归,即如果递归的下一句就是return且没有返回值或者返回值不含有递归函数则编译器会做优化,不会压入新的函数而是 ...

  5. 洛谷 P9683 A Certain Forbidden Index 题解

    题目链接:\(\color{Purple}\texttt{P9683 A Certain Forbidden Index}\). 填坑.提供一个相对好写的做法. 考虑把一堆不交的区间绑在一起问(即先询 ...

  6. Go 标准库之 io.Copy 和 ioutil.ReadAll

    1. go 标准库之 io.Copy 和 ioutil.ReadAll 1.1 介绍 go 标准库中通过 ioutil.ReadAll 实现数据流的读取,io.Copy 实现数据流的读取和写入. 那两 ...

  7. Nacos源码 (5) Grpc服务端和客户端

    Nacos 2.x在服务端与客户端直接增加了GRPC通信方式,本文通过2.0.2版本源码,简单分析GRPC通信方式: 服务器启动 客户端连接 客户端心跳 服务器监控检查 服务器 proto文件 api ...

  8. Linux进阶命令-grep

    Linux进阶命令----grep 目录 Linux进阶命令----grep grep 命令介绍 grep命令格式 常用选项 模式部分 匹配字符: 匹配次数: 位置锚定: grep 命令介绍 Linu ...

  9. SV 接口

    概述 接口 main bus有很多信号线 verilog会先将模块的输出信号拉出来,然后再将其连接到其他模块,进行不同模块之间的连接比较麻烦且容易出错 interface - 将端口封装到接口中 接口 ...

  10. ingress nginx 支持的K8S版本以及nginx版本信息