博客
关于我
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记
阅读量:397 次
发布时间:2019-03-06

本文共 3923 字,大约阅读时间需要 13 分钟。

任务16:oauth2 + oidc 实现 client部分

实现 client 之前启动一下上一节的 server,启动之前需要清除一些代码

注释 Program 的 MigrateDbContext

public static void Main(string[] args){    BuildWebHost(args)        //.MigrateDbContext
((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
(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
();

客户端设置为5001来启动,然后服务端设置为5000

mvcCookieAuthSample 的 Program

public static IWebHost BuildWebHost(string[] args) =>            WebHost.CreateDefaultBuilder(args)                .UseEnvironment("Development")                .UseUrls("http://localhost:5000")                .UseStartup
() .Build();

修改服务端的 Config 配置跳转地址

public static IEnumerable
GetClients(){ return new List
{ 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

启动服务端,客户端,可以看到跳转到登录界面

登录之后会跳转到

在客户端 About.cshtml 页面显示 identity 的 claims

@{    ViewData["Title"] = "About";}

@ViewData["Title"]

@ViewData["Message"]

@*

Use this area to provide additional information.

*@
@foreach (var claim in User.Claims) {
@claim.Type
@claim.Value
}

启动程序,跳转之后,点击 About 进入 About 页面

主要返回了服务端 Config 中配置的信息

public static IEnumerable
GetIdentityResources() { return new List
{ new IdentityResources.OpenId(), new IdentityResources.Profile(), new IdentityResources.Email(), }; }

课程链接

本作品采用进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

你可能感兴趣的文章
简单的xml读取存储方法(未优化)
查看>>
Making the grade 和Sonya and Problem Wihtout a Legend
查看>>
Nginx---惊群
查看>>
项目中常用的审计类型概述
查看>>
(九)实现页面底部购物车的样式
查看>>
python-day3 for语句完整使用
查看>>
ButterKnife使用问题
查看>>
为什么讨厌所谓仿生AI的说法
查看>>
ORACLE 客户端工具
查看>>
基于LabVIEW的入门指南
查看>>
weblogic之cve-2015-4852
查看>>
Java注释
查看>>
C++ 函数重载
查看>>
abstract关键字的使用
查看>>
.NET微信网页开发之使用微信JS-SDK调用微信扫一扫功能
查看>>
使用mybatis-generator生成底层
查看>>
Mybatis【5】-- Mybatis多种增删改查那些你会了么?
查看>>
计算输入的一句英文语句中单词数
查看>>
lvs+keepalive构建高可用集群
查看>>
6 个 Linux 运维典型问题
查看>>