一步一步学习IdentityServer3 (3)
在上一篇中配置一个基础的idrserver服务端
这篇文章将对服务端做一些变化,这里我先贴一下上一章中的代码
证书:
static class Certificate
{
public static X509Certificate2 Get()
{
var assembly = typeof(Certificate).Assembly;
using (var stream = assembly.GetManifestResourceStream("OAuthWeb.IdrConfig.idsrv3test.pfx"))
{
return new X509Certificate2(ReadStream(stream), "idsrv3test");
}
} private static byte[] ReadStream(Stream input)
{
byte[] buffer = new byte[ * ];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, , buffer.Length)) > )
{
ms.Write(buffer, , read);
}
return ms.ToArray();
}
}
}
Clients:
public class Clients
{
public static IEnumerable<Client> Get()
{
return new[]
{
//js客户端
new Client
{
Enabled = true,
ClientName = "JS Client",
ClientId = "js",
Flow = Flows.Implicit, RedirectUris = new List<string>
{
"http://192.168.0.42:44319/Home/Contact"
}, AllowedCorsOrigins = new List<string>
{
"http://localhost:20241"
}, AllowAccessToAllScopes = true
},
//客户端模式(client credentials)
new Client
{
ClientName = "Silicon-only Client",
ClientId = "silicon",
Enabled = true,
AccessTokenType = AccessTokenType.Reference, Flow = Flows.ClientCredentials, ClientSecrets = new List<Secret>
{
new Secret("F621F470-9731-4A25-80EF-67A6F7C5F4B8".Sha256())
}, //指明该注册client允许的scopes
AllowedScopes = new List<string>
{
"api1"
}
},
//密码模式(resource owner password credentials)
new Client
{
ClientName = "Silicon on behalf of Carbon Client",
ClientId = "carbon",
Enabled = true,
AccessTokenType = AccessTokenType.Reference, Flow = Flows.ResourceOwner, ClientSecrets = new List<Secret>
{
new Secret("21B5F798-BE55-42BC-8AA8-0025B903DC3B".Sha256())
}, AllowedScopes = new List<string>
{
"api1"
}
},
//简化模式(implicit)
new Client
{
Enabled = true,
ClientName = "SSO",
ClientId = "mvc",
Flow = Flows.Implicit,
RequireConsent=false,
ClientSecrets=new List<Secret> {
new Secret("21B5F798-BE55-42BC-8AA8-0025B903DC3B".Sha256())
}, AllowedScopes = new List<string> {
Constants.StandardScopes.OpenId,
Constants.StandardScopes.Profile
}
} };
}
}
Scopes:
public class Scopes
{
public static List<Scope> Get()
{
return new List<Scope>
{
IdentityServer3.Core.Models.StandardScopes.OpenId,
IdentityServer3.Core.Models.StandardScopes.Profile, //注册一个新的scope,在注册client时会指明只允许这个api1的scope,客户端在请求token的时候会指明申请的scope
new Scope
{
Name = "api1"
}
};
}
}
users:
public class Users
{
public static List<InMemoryUser> Get()
{
return new List<InMemoryUser>
{
new InMemoryUser
{
Username = "bob",
Password = "secret",
Subject = "", Claims = new[]
{
new Claim(Constants.ClaimTypes.GivenName, "Bob"),
new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
new Claim(Constants.ClaimTypes.Email, "bob.smith@email.com")
}
},
new InMemoryUser
{
Username = "alice",
Password = "secret",
Subject = ""
}
};
}
}
结合上一章节贴了一些代码,代码中略有删减
有了这个idrserver 怎么来做自己的SSO呢?
如我有一个网站A 需要IdrServer提供认证, 现在涉及到了OpenID
新建一个站点A 添加nuget包
Microsoft.Owin.Security.Cookies;
Microsoft.Owin.Security.OpenIdConnect;
Microsoft.Owin.Host.SystemWeb;
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "http://192.168.0.42:10011/lym", //这里写你idrserver的地址
ClientId = "mvc", //Client 要对应
Scope = "openid profile",//Client 要对应
RedirectUri = "http://192.168.0.42:44319/", //登陆成功后的跳转地址,要对应
PostLogoutRedirectUri = "http://192.168.0.42:44319/", //如上
ClientSecret = "21B5F798-BE55-42BC-8AA8-0025B903DC3B",
ResponseType = "id_token token", //参考配置说明 还有授权码 code
SignInAsAuthenticationType = "Cookies"
});
访问站点A 就会转到SSO登陆页面如下图:

我这里自己定义的登陆界面,可以修改成自己的样式,风格,能看到登陆界面 说明这一步成功了,下一篇文章将介绍自定义登陆页面的操作
一步一步学习IdentityServer3 (3)的更多相关文章
- 一步一步学习IdentityServer3 (1)
学习之初: IdentityServer3我自己最开始了解到的就是做一个SSO单点登录,后面发现还有单独的认证服务功能,其实它还可以做APIs的访问控制,资源授权,另外还可以为提供第三方登录,其他的自 ...
- 一步一步学习IdentityServer3 (2)
下面就来做一个例子:IdentityServer3服务端的配置 VS2015创建一个MVC项目 IdrOAuth 用来授权的认证的站点
- 一步一步学习IdentityServer3 (4)
其实上述例子 很多都很找到 但是在实际生态环境中给例子有很多不一样的地方 比如自定已登录界面怎么做? 怎么访问自己的用户数据库实现登录? 怎么在接口中使用,在接口中又怎么实现与Idr3结合授权? 等等 ...
- 12.Linux软件安装 (一步一步学习大数据系列之 Linux)
1.如何上传安装包到服务器 有三种方式: 1.1使用图形化工具,如: filezilla 如何使用FileZilla上传和下载文件 1.2使用 sftp 工具: 在 windows下使用CRT 软件 ...
- (转) 一步一步学习ASP.NET 5 (四)- ASP.NET MVC 6四大特性
转发:微软MVP 卢建晖 的文章,希望对大家有帮助.原文:http://blog.csdn.net/kinfey/article/details/44459625 编者语 : 昨晚写好的文章居然csd ...
- (转) 一步一步学习ASP.NET 5 (二)- 通过命令行和sublime创建项目
转发:微软MVP 卢建晖 的文章,希望对大家有帮助. 注:昨天转发之后很多朋友指出了vNext的命名问题,原文作者已经做出了修改,后面的标题都适用 asp.net 5这个名称. 编者语 : 昨天发了第 ...
- 一步一步学习SignalR进行实时通信_1_简单介绍
一步一步学习SignalR进行实时通信\_1_简单介绍 SignalR 一步一步学习SignalR进行实时通信_1_简单介绍 前言 SignalR介绍 支持的平台 相关说明 OWIN 结束语 参考文献 ...
- 一步一步学习SignalR进行实时通信_8_案例2
原文:一步一步学习SignalR进行实时通信_8_案例2 一步一步学习SignalR进行实时通信\_8_案例2 SignalR 一步一步学习SignalR进行实时通信_8_案例2 前言 配置Hub 建 ...
- 一步一步学习SignalR进行实时通信_9_托管在非Web应用程序
原文:一步一步学习SignalR进行实时通信_9_托管在非Web应用程序 一步一步学习SignalR进行实时通信\_9_托管在非Web应用程序 一步一步学习SignalR进行实时通信_9_托管在非We ...
- 一步一步学习SignalR进行实时通信_7_非代理
原文:一步一步学习SignalR进行实时通信_7_非代理 一步一步学习SignalR进行实时通信\_7_非代理 SignalR 一步一步学习SignalR进行实时通信_7_非代理 前言 代理与非代理 ...
随机推荐
- [代码]--c#获取系统时间
public DateTime GetServerDataTime() { string sql = " select top 1 getdate() from sysobjects &qu ...
- QWidget窗体中使用Q_OBJECT后无法添加背景图片或背景色
在继承自QWiget的窗体中,设置背景图片或背景色比较简单的方法是使用setStyleSheet()函数,比如在构造函数中可以这样来设置背景图片: this->setStyleSheet(&qu ...
- 【洛谷P2114】起床困难综合征 位运算+贪心
题目大意:给定 N 个操作,每个操作为按位与.或.异或一个固定的数字,现在要求从 0 到 M 中任选一个数字,使得依次经过 N 个操作后的值最大. 题解:位运算有一个重要的性质是:位运算时,无进位产生 ...
- Java 泛型类型基础
为什么要使用泛型? 未使用泛型的情况: // 创建列表类 List list = new ArrayList(); // 添加一个类型为 String 的列表元素 list.add("hel ...
- 滚动条事件,当页面滚动到距顶部一定高度时某DIV自动隐藏和显示
$(function () { //绑定滚动条事件 //绑定滚动条事件 $(window).bind(&q ...
- oracle:delete和truncate
oracle中清空表数据的两种方法 1.delete from t 2 .truncate table t 区别: 1.delete是dml操作:truncate是ddl操作,ddl隐式提交不能回滚 ...
- oracle按照in的顺序进行排序
oracle按照in的顺序进行排序 ,,) order by case id end;
- centos下安装python3.6.2
一.下载 官网地址:https://www.python.org/downloads/source/ 我下载的是最新的3.6.2rc版本 # cd /opt/ wget https://www.pyt ...
- nodejs npm install -g 全局安装
1. npm install xxx -g 时, 模块将被下载安装到[全局目录]中. [全局目录]通过 npm config set prefix "目录路径" 来设置. 比如说, ...
- bzoj千题计划188:bzoj1923: [Sdoi2010]外星千足虫 (高斯—若尔当消元法解异或方程组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1923 #include<cstdio> #include<cstring> ...