任务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. 【调试】GDB使用总结

    启动 在shell下敲gdb命令即可启动gdb,启动后会显示下述信息,出现gdb提示符. ➜ example gdb GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1 Cop ...

  2. P1216-DP【橙】

    在这道题中,我第一次用了memset,确实方便,不过需要注意的是只有全部赋值-1和0的时候才能使用它,否则他能干出吓死人的事.以及memset在cstring头文件里,在本地就算不include也能照 ...

  3. java项目实战-tomcat-SpringMVC-基本用法01-day25

    目录 1. maven创建 war项目 2. SpringMVC 1. maven创建 war项目 什么是jar项目 什么是war项目? jar项目: 由main方法来开始的 直接依赖JVM就能编译运 ...

  4. 基于python的租房网站-房屋出租租赁系统(python+django+vue)

    该项目是基于python/django/vue开发的房屋租赁系统/租房平台,作为本学期的课程作业作品.欢迎大家提出宝贵建议. 功能介绍 平台采用B/S结构,后端采用主流的Python+Django进行 ...

  5. 【SHELL】反斜杠解决多个shell实例扩展

    本意是想获取代码仓相对路径,代码如下 base_dir=`pwd` repo forall -c '{     user_dir=$(realpath --relative-to="$bas ...

  6. bcc的简单学习

    bcc的简单学习 安装 # 安装部分依赖项目 yum install cmake llvm -y dnf install -y bison cmake ethtool flex git iperf3 ...

  7. [转帖]SQL SERVER中什么情况会导致索引查找变成索引扫描

    https://www.cnblogs.com/kerrycode/p/4806236.html SQL Server 中什么情况会导致其执行计划从索引查找(Index Seek)变成索引扫描(Ind ...

  8. [转帖]TiKV集群搭建

    https://www.cnblogs.com/luohaixian/p/15227788.html 1.准备环境 准备4台ubuntu 16.04虚拟机 部署规划: 节点类型 CPU 内存 存储 部 ...

  9. [转帖]Springboot配置kafka用户名密码

    华为云开发者联盟 Springboot配置kafka用户名密码 Springboot配置kafka用户名密码 SpringBoot配置kafka用户名密码 Springboot配置kafka用户名密码 ...

  10. Python学习之十八_django的学习(二)

    Python学习之十八_django的学习(二) 前言 前面学习了基本的django的使用. 这里想着稍微深入一点学习templates 以及进行级联的路由展示. 修改配置文件 要想使用 templa ...