我对oidc-client的Silen刷新有问题。登录正常,我可以获取令牌。但是,无提示刷新不会触发,什么也不会发生,并且当我订阅检查令牌特权的方法时(authservice.ts中subscribeevents中的方法),这些方法将永远不会触发,如果令牌存在,则方法isLoggedIn()始终返回true事件已过期!!!!!
有人可以帮我吗?
这是我的代码:
import { Component, OnInit } from '@angular/core';
import { UserManager } from 'oidc-client';
import { getClientSettings } from '../openIdConnectConfig';
import { AuthService } from '../services/auth.service';
@Component({
selector: 'app-silentrefresh',
templateUrl: './silentrefresh.component.html',
styleUrls: ['./silentrefresh.component.css']
})
export class SilentRefreshComponent implements OnInit {
constructor(private _authService:AuthService) {
}
ngOnInit() {
this._authService.refreshCallBack();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的authservice:
import { UserManagerSettings, UserManager, User } from 'oidc-client';
import { Injectable } from '@angular/core';
import { getClientSettings } from '../openIdConnectConfig';
@Injectable()
export class AuthService {
private _manager = …Run Code Online (Sandbox Code Playgroud) 我正在使用 oidc-client 构建 SPA,以登录到使用 Identity Server 4 构建的 IDP。
登录重定向似乎工作正常,但在 Firefox 上我遇到以下 CSP 问题
Content Security Policy: Ignoring "'unsafe-inline'" within script-src or style-src: nonce-source or hash-source specified (unknown)
Content Security Policy: The page's settings blocked the loading of a resource at self ("script-src 'unsafe-inline' 'sha256-VDXN0nOpFPQ102CIVz+eimHA5e+wTeoUUQj5ZYbtn8w='"). Source: !function(t){function __webpack_require_.... checksession:1
Content Security Policy: The page's settings blocked the loading of a resource at self ("script-src 'unsafe-inline' 'sha256-VDXN0nOpFPQ102CIVz+eimHA5e+wTeoUUQj5ZYbtn8w='"). Source: window.devToolsOptions = Object.assign(w.... checksession:1
Load denied by X-Frame-Options: http://localhost:5007/home/error?errorId=a74accc61bb821ee1f42f7013a306e90 does not permit cross-origin …Run Code Online (Sandbox Code Playgroud) javascript content-security-policy openid-connect identityserver4 oidc-client-js
isLoggedIn()使用odic-client 确定最简洁准确的方法是什么?
与Angualr2示例非常相似,我的第一种方法是:
// return true if user (token) exists.
public isLoggedIn(): Promise<boolean> {
return this.userManager.getUser().then(user => !!user);
}
Run Code Online (Sandbox Code Playgroud)
然后处理过期的令牌:
// return true if user (token) exists and not expired.
public isLoggedIn(): Promise<boolean> {
return this.userManager.getUser().then(user => {
if (!user) {
return false;
}
return !user.expired;
});
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序的一个要求是,如果OP已撤销会话,它不会显示用户已登录,因此,遵循此处的逻辑,我的下一个方法是:
// return true if user (token) and session (cookie) exists and sub matches and not expired.
public async isLoggedIn(): Promise<boolean> {
const session = await this.userManager.querySessionStatus().catch(() …Run Code Online (Sandbox Code Playgroud) 我有Identity Server 4的本地实例,我正在尝试按照本指南创建Javascript客户端。这使用oidc-client-js库,而我使用的是登录弹出窗口方法,因此我的登录事件处理程序如下所示:
signin(e) {
e.preventDefault();
this.oidcUserMgr.signinPopup({state:'some data'}).then(function(user) {
console.log("signed in", user.profile);
}).catch(function(err) {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud)
身份验证似乎可以正常工作-我已重定向到接受客户端请求,对我的登录进行身份验证并将我返回到客户端应用程序的Identity Server。但是,文档说user.profile上述代码中的对象应包含用户声明,但没有。这是use.profile我得到的:
该sub属性是刚刚通过身份验证的用户的正确ID。但我的身份服务器也发出响应其他领域我的客户要求(索赔profile和email),所以我应该看到的索赔,如name,preferred_username,email等)。我可以观察到IProfileService在IS4中调试我的实现时发出的这些声明。此外,如果我使用access_token带有用户对象的return来向本地运行的另一个API(ASP.NET Web API)发出请求,则可以在this.User.Claims以下位置看到这些声明:
那么如何在我的Javascript代码中掌握这些主张呢?
javascript asp.net openid-connect identityserver4 oidc-client-js
我在(oidc-client.js)+ .Net Core IdentityServer4处理的Angular 8中获得了授权。
一切似乎都正常,但是当我在第二个选项卡中打开相同的应用程序时,则需要我再次登录。IdentityServer4具有cookie,因此单击“ 登录”按钮就足够了,并且无需再次提供登录名/密码即可接收新令牌。无论如何,它仍然很烦人。
有什么办法解决吗?我在githubg上发现了一个可能会有所帮助的问题。
有人建议需要将令牌本地化从LocalStorage更改为SessionStorage。但是从个人角度而言,SessionStorage更好,我会保留在那个位置。
authorization session-storage identityserver4 oidc-client-js angular
我的理解是 - oidc-client生成随机数和状态并将其发送到授权服务器(身份服务器 4)。这是用来防止CSRF攻击、重放攻击的。
状态和随机数通过下面的signinredirect()示例示例发送
https://auth.azurewebsites.net/Account/Login?
ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3F
client_id%3DLocal%26
redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A4200%252Fauth-callback%252F%26
response_type%3Did_token%2520token%26
scope%3Dopenid%2520profile%2520Api%26
state%3D212ee56661074896aea2b6043d2b8a3f%26
nonce%3D393838b342d543d5910f38cbcab22fa0%26
loginType%3DInternal // my extra params
Run Code Online (Sandbox Code Playgroud)
问题 1 - 回调后状态未定义
状态添加到回调 URL 中,如下所示
http://localhost:4200/auth-callback#id_token=eyJhbG...
token_type=Bearer
&expires_in=300&
scope=openid%20profile%20Api&
state=155e3e4352814ca48e127547c134144e&
session_state=DPXW-ijMR4ST9iTSxgMwhsLq7aoknEZOnq3aFDooCFg.ifImJurwkwU6M5lwZXCUuw
Run Code Online (Sandbox Code Playgroud)
状态必须存在于用户中。但就我而言,我在回调方法中看到状态未定义
async completeAuthentication() {
await this.manager
.signinRedirectCallback()
.then(x => {
this.user = x;
this.user.state = x.state; // undefined
this.user.session_state = x.session_state;
})
.catch(errorData => {
const expired = errorData;
});
Run Code Online (Sandbox Code Playgroud)
问题 -
问题 2 - 随机数
id_token 中收到随机数值
created: 1594171097 …Run Code Online (Sandbox Code Playgroud) 我正在使用一个 angular SPA,它通过使用身份服务器 4和oidc 客户端 js来实现身份验证。
有些东西在静默访问令牌更新级别不起作用。预期的行为是访问令牌的自动更新,这要归功于调用/connect/authorize端点的 iframe 。此调用将身份服务器身份验证 cookie 与 HTTP 请求一起发送,这样做身份服务器知道用户会话仍然有效并且能够发出新的访问令牌,而无需用户再次交互登录。到目前为止,我很确定我的理解是正确的。
这是棘手的部分:我的期望是身份服务器身份验证 cookie 应该有一个滑动到期时间,以便每次调用/connect/authorize端点时其到期日期都会及时向前移动。换句话说,我预计在用户第一次登录后不需要用户进行其他交互式登录,因为每次静默更新 iframe 需要新的访问令牌时,用户会话到期日期都会自动向前移动.
为了获得这种行为,我在身份服务器级别设置了以下配置。
这是客户端配置(注意访问令牌生命周期为 2 分钟 = 120 秒):
new Client
{
ClientId = "web-client",
ClientName = "SPA web client",
AllowedGrantTypes = GrantTypes.Code,
RequireClientSecret = false,
RequirePkce = true,
RequireConsent = false,
AccessTokenLifetime = 120,
RedirectUris = { "https://localhost:4200/assets/signin-callback.html", "https://localhost:4200/assets/silent-callback.html" },
PostLogoutRedirectUris = { …Run Code Online (Sandbox Code Playgroud) 我有一个 .net core 3.1 MVC IdentityServer4 应用程序(由 Idsrv 的人员提供的定制快速入门应用程序),我使用它对使用 oidc-client 库构建的 SPA 进行身份验证,最近我能够复制问题关于尝试登录 Identity Server 时时不时出现的 400 错误。
在 Chrome/Firefox/Edge 上打开两个单独的选项卡,然后导航到两个选项卡上客户端应用程序的路径(例如 https://localhost:5001)。由于用户尚未登录,这两个选项卡都正确重定向到我的身份服务器实例的 URL (https://localhost:5002)。
使用用户名和密码登录第一个选项卡,甚至使用外部 OIDC 客户端 - 这工作正常。
尝试使用用户名和密码在第二个选项卡上登录
发生 400 错误,检查日志后出现以下 CORS 问题(我认为这并不相关):
[15:51:29 调试] IdentityServer4.Hosting.CorsPolicyProvider 对路径发出 CORS 请求:/Account/Login from origin: null 但被忽略,因为路径不适用于允许的 IdentityServer CORS 端点
如果我在浏览器中单击返回,登录页面会再次显示,然后我就可以按预期登录。
总而言之,一旦用户成功登录第一个选项卡,当用户第一次尝试登录 IdentityServer 时,第二个选项卡将始终抛出 400 错误。我真的很难弄清楚为什么会出现这种情况 - 而且 CORS 错误消息也有点奇怪,因为当我单击返回并尝试第二次登录时,它工作正常。
new Client
{
ClientId = "WebApp",
ClientName = "Web App",
AllowedGrantTypes = GrantTypes.Code,
RequireClientSecret = false,
RequirePkce = true,
AccessTokenType = AccessTokenType.Reference, …Run Code Online (Sandbox Code Playgroud) 我目前正在构建一个 Vue3 SPA 并尝试使用 oidc-client-ts javascript 库来验证用户身份。到目前为止,我已经成功地使用现有代码实现了登录/注销功能。我遇到的问题是我无法连接 UserManager 提供的 addUserLoaded 或 addUserSignedIn 事件(https://authts.github.io/oidc-client-ts/classes/UserManagerEvents.html)。需要注意的一点是,当您登陆我的域(我们会说 myapp.mydomain.com)时,您将被重定向到位于 myid.mydomain.com 的身份服务器,然后在登录成功后重定向回来。
启动时我有以下内容
fetch("config.json", {'method': "GET"})
.then(response => {
if(response.ok) {
response.json().then(appConfigData => {
window.appConfig = appConfigData;
createApp(App)
.use(store)
.use(router)
.use(AccordionPlugin)
.component("font-awesome-icon", FontAwesomeIcon)
.mount("#app");
});
} else{
alert("Server returned " + response.status + " : " + response.statusText);
}
});
Run Code Online (Sandbox Code Playgroud)
这个window.appConfig具有用于初始化UserManager的设置对象
这是我导出的 AuthService.ts 类。请注意我在 UserManager 中注册的构造函数中的事件。
import { UserManager, WebStorageStateStore, User, UserManagerEvents } from "oidc-client-ts";
import jwt_decode from "jwt-decode";
import store from "@/store";
export …Run Code Online (Sandbox Code Playgroud) 我正在使用带有 angular 7 的 oidc-client,并且我想启用日志记录。该文档建议我可以执行以下操作
Oidc.Log.logger = console;
Run Code Online (Sandbox Code Playgroud)
我无法完成这项工作,因为 Oidc 似乎不在窗口对象上??
oidc-client-js ×10
angular ×4
javascript ×2
asp.net ×1
c# ×1
csrf ×1
oauth-2.0 ×1
vue.js ×1
vuejs3 ×1