任务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. 【驱动】SPI驱动分析(一)-SPI协议简介

    1. 什么是SPI SPI全拼Serial Peripheral interface(串行外围设备接口),是由Motorola(摩托罗拉)在MC68HCXX系列处理器上定义的,主要应用于EEPROM( ...

  2. Chrome 控制台 换行编写js调试代码

    转载请注明出处: 在 chrome 浏览器的console 控制台编写 js 调试或验证代码时,每输一行换行时,就会执行当前行的函数,再重新换行输入时,就会将之前的代码忽略,这种方式就会导致 chro ...

  3. nginx.conf 配置解析及常用配置

    本文为博主原创,未经允许不得转载: nginx.conf 配置文件配置解析 #定义 Nginx 运行的用户和用户组.默认nginx的安装用户为 nobody user www www: #启动进程,通 ...

  4. 问题--缺少 cryptography 包

    1.问题 raise RuntimeError( RuntimeError: 'cryptography' package is required for sha256_password or cac ...

  5. 让vs支持wsl调试

    WSL安装 wsl --install -d Ubuntu 等一会提示输入用户名,不用管它,直接关闭,下次打开wsl,会以无密码的root用户打开 wsl卸载 wsl --unregister Ubu ...

  6. Go-获取密码的sha值

    // 对用户密码进行加密 func EncodePwd(pwd string) string { s := sha256.New() s.Write([]byte(pwd)) data := s.Su ...

  7. Django数据导入导出神器django-import-export使用

    前言 Django以快速开发闻名,但是如果处理数据的导出导入还需要自己写脚本,那就有违"Python之禅"了-- 而且导数据通常需要不同的格式,Excel.csv.json等,每种 ...

  8. Booking.com如何在毫秒内搜索数百万个地点

    译自:How Booking.com Searches Through Millions of Locations in Milliseconds Booking.com是一家与酒店.旅馆.度假租赁等 ...

  9. UData查询引擎优化-如何让一条SQL性能提升数倍

    1 UData-解决数据使用的最后一公里 1.1 背景 在大数据的范畴,我们经历了数据产业化的历程,从各个生产系统将数据收集起来,经过实时和离线的数据处理最终汇集在一起,成为我们的主题域数据,下一步挖 ...

  10. js数组修改后会互相影响

    // 假设httpServe 是服务器返回来的数据 // 我们这里有一个需求, // 某一个区域需要对这一份数据进行展示 // 另一个区域需要只需要展示前1条数据 let httpServe = [ ...