前言

前面已经解释了两个案例了,通信原理其实已经很清楚了,那么纯js客户端是怎么处理的呢?

正文

直接贴例子哈。

https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/4_JavaScriptClient

那么解释一下其实怎么做的吧。

那么就直接来看这个javascriptClient 是怎么实现的吧。

public void Configure(IApplicationBuilder app)
{
app.UseDefaultFiles();
app.UseStaticFiles();
}

这个两个就是给静态文件设置路由,这个不过多介绍,.net core 系列已经解读过其源码了。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<button id="login">Login</button>
<button id="api">Call API</button>
<button id="logout">Logout</button> <pre id="results"></pre> <script src="oidc-client.js"></script>
<script src="app.js"></script>
</body>
</html>

这个的话,那么引入了oidc-client.js 和 app.js。

app.js 肯定就是自己项目的代码了。

oidc-client 可以看下官方介绍。https://github.com/IdentityModel/oidc-client-js

也就是说用好了oidc-client就可以了。

首先实例了一个客户端:

var config = {
authority: "https://localhost:5001",
client_id: "js",
redirect_uri: "https://localhost:5003/callback.html",
response_type: "code",
scope:"openid profile api1",
post_logout_redirect_uri : "https://localhost:5003/index.html",
};
var mgr = new Oidc.UserManager(config);

然后登录的时候:

function login() {
mgr.signinRedirect();
}

这里面根据我们前面的例子,也能猜到其实就是去查idenityserver的wellknown 跳转到登录地址。

然后identityServer 注册好jsClient 就行。

// JavaScript Client
new Client
{
ClientId = "js",
ClientName = "JavaScript Client",
AllowedGrantTypes = GrantTypes.Code,
RequireClientSecret = false, RedirectUris = { "https://localhost:5003/callback.html" },
PostLogoutRedirectUris = { "https://localhost:5003/index.html" },
AllowedCorsOrigins = { "https://localhost:5003" }, AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
}
}

下面直接看效果就好了。

首先去拿identityserver 的公开信息。

然后访问identityServer:

然后就到了登录页面了。

然后进行登录。

还是和以前一样,这里可以直接运行例子更加直观。

然后看下客户端拿到授权码后如何获取token的。

然后通过token 获取到了userinfo。

这里解释一下这里有几个token,一个是access_token 这个是来表示授权令牌,比如访问一些接口的api。

然后这个id_token 是身份令牌,在线解析一下哈:

那么这个身份令牌是用来干什么的。授权令牌用来访问api授权的,那么身份令牌用来干什么的呢?

这样我们不要猜,直接看官方怎么说。

https://auth0.com/docs/secure/tokens/id-tokens#learn-more

ID tokens are used in token-based authentication to cache user profile information and provide it to a client application, thereby providing better performance and experience. The application receives an ID token after a user successfully authenticates, then consumes the ID token and extracts user information from it, which it can then use to personalize the user's experience.

上面说id tokens 是基于认证token 来缓存用户的信息提供给客户端,提供更好的性能和体验。在认证后应用收到id token,使用id token 收集用户的信息,可以被使用与私有化的用户体验。

我们知道auth2.0 管理授权,但是不管理用户信息。当然了auth 2.0 可以通过api 获取到用户信息,但是这是另外一回事。

比如说我们访问简书,跳转到qq登录,然后又登录回来,这其中包括了认证和授权。认证应该返回id tokens,来证明这个用户认证了。授权应该返回access_token 表示授权token。

然后文档中也给了例子。

For example, suppose you have a regular web app that you register it with Auth0 and configure to allow users to login with Google. Once a user logs in, use the ID token to gather information such as name and email address, which you can then use to auto-generate and send a personalized welcome email.

比如说,假设你有一个常规的web app,这个app 你允许用户他能给个google 登录。 一但用户登录,使用这个id token 可以收集用户信息,比如说名字邮件,这样你就可以给这个用户发送邮件。

上面那些id_token 中好像没有什么东西,那么其实我们可以增加一些东西。

全部可以看这个:

https://docs.authing.cn/v2/concepts/id-token.html

那么作为客户端下次访问的时候如何去读取数据呢?

也就是从下面这里去读数据:

当然里面还会去执行是否会话过期。

这里面就是检查会话是否过期。

这个有什么用呢?

假如我们在identity server 中退出了登录,那么会发生什么呢?

下面是我identity server 退出后的结果:

那么就会访问https://localhost:5001/connect/authorize。

这里就不看细节了,讲一下效果。

cookie 清除了,会话结束了。

前面是有identity 会话的。

那么这cookie 有什么用呢?

如果cookie 在的话,那么会把cookie带上去访问identity server,identity 如果确定这个会话没有过期,那么不会进入登录界面,而是直接callback 回来。

现在都是不带数据库的,后面把数据库例子说明一下。

最新文章

  1. iOS推送流程
  2. 《React Native入门与实战》读书笔记(1)
  3. c++第一天
  4. C# 使用 fckeditor 上传文件中文名乱码的问题---转
  5. oracle:db-link使用
  6. PHP的OB缓存(输出缓存)
  7. usb驱动开发2之代码地图
  8. linux su和sudo命令的区别(转)
  9. Hdu 2475-Box LCT,动态树
  10. 附加数据库 对于 服务器“00-PC”失败
  11. Myeclipse+Tomcat安装与配置
  12. SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
  13. 2017-06-22初识python
  14. 14 Fragment 碎片总结
  15. 爬虫(七)图片懒加载技术、selenium和PhantomJS
  16. java当中的强引用,软引用,弱引用,虚引用
  17. 【HDU1846】Brave Game(博弈论)
  18. go语言使用go-sciter创建桌面应用(五) 加载元素资源
  19. Jmeter录制badboy
  20. C#学习笔记(四):switch语句

热门文章

  1. SAP 定义客户端
  2. Prometheus安装教程
  3. ansible管理windows主机
  4. .NET混合开发解决方案24 WebView2对比CefSharp的超强优势
  5. Python基础知识+题目练习,我不信你能做出这道题
  6. Windows下maven配置环境变量
  7. CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
  8. ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
  9. VS无线振弦采集仪的常见问题
  10. kubernetes 调度