首页 > 其他分享 >identity4 系列————纯js客户端案例篇[四]

identity4 系列————纯js客户端案例篇[四]

时间:2022-08-27 22:38:08浏览次数:70  
标签:app js token https 客户端 id localhost identity4

前言

前面已经解释了两个案例了,通信原理其实已经很清楚了,那么纯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 回来。

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

标签:app,js,token,https,客户端,id,localhost,identity4
From: https://www.cnblogs.com/aoximin/p/16619474.html

相关文章

  • js删除css样式
    js删除css样式_百度知道 https://zhidao.baidu.com/question/680409425108037292.html 1、如果使用class加的样式的话,可以使用document.getElementById("objid......
  • js给onclick事件赋值,动态传参数实例解说
    js给onclick事件赋值,动态传参数实例解说_javascript技巧_脚本之家 https://www.jb51.net/article/35107.htm我们先看看错误的例子Html代码复制代码代码如下:<body><in......
  • Js 函数eval() 用法记录
    js函数eval()用法记录问题描述:遇到一串数据responseText,在浏览器中使用console函数输出responseText这个字段整体的时候,eg:console.log(“—————–responseText—-“......
  • js中alert的换行问题
    关于alert的内容还行问题,参考了很多人的意见,在这里我做一个总结,当然我们平时只要使用一种能达到效果的方法即可:这个与使用的浏览器也有关系在使用alert弹窗过程中,对于大......
  • js声明数组的四种方式
    js声明数组的四种方式_麦客子的博客-CSDN博客_js声明数组的写法 https://blog.csdn.net/a911711054/article/details/72869324<!DOCTYPEhtml><htmllang="en"><head......
  • SpringCloud 使用 LoadBalance 实现客户端负载均衡
    SpringCloud从2020.0.1版本开始,从Eureka中移除了Ribbon组件,使用LoadBalance组件来代替Ribbon实现客户端负载均衡。LoadBalance组件相对于Ribbon来说,仅支持两......
  • vscode可以通过改json让程序运行完不退出
    如图改相应的launch.json至于D盘test.exe是因为有中文路径"program":"C:\\Windows\\System32\\cmd.exe","args":["/c","${fileDirname}/${fileBasenameNoExtension}.......
  • js 函数的参数长度问题
    js函数的参数长度问题_Jamie_java的博客-CSDN博客_js传参数有长度限制 https://blog.csdn.net/baidu_27062827/article/details/52276635js函数,如果传入参数的长度太长......
  • js - 修改弹出窗口的标题
    js-修改弹出窗口的标题_xsscacy的博客-CSDN博客 https://blog.csdn.net/shen813/article/details/9087029先利用window.open(url)打开一个窗口,然后在利用设置document.t......
  • js高级
    一、面向对象的编程介绍1.面向过程编程pop面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候再一个一个的依次调用就可以了。......