ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-08-27 22:30:30  阅读:182  来源: 互联网

标签: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
来源: https://www.cnblogs.com/aoximin/p/16619474.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有