带有客户端 Angular 的 IdentityServer4 中具有无效签名的令牌
Token with invalid signature in IdentityServer4 with client Angular
我正在开发一个带有客户端 Angular2 的应用程序 Web,以及 ASP.NET Core 2 中的后端和 IdentityServer4,最后是一个受 IdentityServer4 身份验证服务器保护的 webapi。
我成功实现了后端,并且mi webapi受到保护。我的客户端 Angular 也受到保护,并使用 mi 客户端中的登录名和在 IdentityServer 中配置的客户端,并在流程中设置了 AllowedGrantTypes 以及密码和用户。问题是令牌的生成。
我可以生成一个有效载荷和 header 有效的令牌,但我没有使签名有效。
在 jwt.io 我的令牌无效,我无法使用令牌访问用户信息,但是使用此令牌可以访问我的 webapi 中的信息。
有什么问题吗?如何修复?
我在 Angular 中的代码是这样的:
import { Component, OnInit } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc';
import { authConfig } from './auth.config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private oAuthService: OAuthService) {
this.oAuthService.tokenEndpoint = 'http://localhost:5000/connect/token';
this.oAuthService.userinfoEndpoint = 'http://localhost:5000/connect/userinfo';
this.oAuthService.clientId = 'angular-client';
this.oAuthService.responseType = 'id_token token';
this.oAuthService.scope = 'api1';
this.oAuthService.dummyClientSecret = 'password';
}
ngOnInit(): void {
this.login();
}
login() {
this.oAuthService.fetchTokenUsingPasswordFlow('aherrera', 'password').then((resp) => {
// Loading data about the user
return this.oAuthService.loadUserProfile();
}).then(() => {
// Using the loaded user data
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// tslint:disable-next-line:no-console
console.debug('given_name');
}
});
}
}
而且,我在 IdentityServer 中的代码是这样的:
new Client
{
ClientId = "angular-client",
ClientName = "Angular Client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
AllowAccessTokensViaBrowser = true,
EnableLocalLogin = true,
AllowedCorsOrigins =
{
"httpsd://localhost:4200",
"http://localhost:4200"
},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"api1"
},
ClientSecrets = new List<Secret>() {
new Secret("password".Sha256())
}
},
控制台的日志为:
请帮忙。
将 openid 范围添加到您的 Angular 代码中:
this.oAuthService.scope = 'openid api1';
您的客户端确实应该使用隐式流程进行配置
AllowedGrantTypes = GrantTypes.Implicit,
我正在开发一个带有客户端 Angular2 的应用程序 Web,以及 ASP.NET Core 2 中的后端和 IdentityServer4,最后是一个受 IdentityServer4 身份验证服务器保护的 webapi。 我成功实现了后端,并且mi webapi受到保护。我的客户端 Angular 也受到保护,并使用 mi 客户端中的登录名和在 IdentityServer 中配置的客户端,并在流程中设置了 AllowedGrantTypes 以及密码和用户。问题是令牌的生成。 我可以生成一个有效载荷和 header 有效的令牌,但我没有使签名有效。 在 jwt.io 我的令牌无效,我无法使用令牌访问用户信息,但是使用此令牌可以访问我的 webapi 中的信息。 有什么问题吗?如何修复?
我在 Angular 中的代码是这样的:
import { Component, OnInit } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc';
import { authConfig } from './auth.config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private oAuthService: OAuthService) {
this.oAuthService.tokenEndpoint = 'http://localhost:5000/connect/token';
this.oAuthService.userinfoEndpoint = 'http://localhost:5000/connect/userinfo';
this.oAuthService.clientId = 'angular-client';
this.oAuthService.responseType = 'id_token token';
this.oAuthService.scope = 'api1';
this.oAuthService.dummyClientSecret = 'password';
}
ngOnInit(): void {
this.login();
}
login() {
this.oAuthService.fetchTokenUsingPasswordFlow('aherrera', 'password').then((resp) => {
// Loading data about the user
return this.oAuthService.loadUserProfile();
}).then(() => {
// Using the loaded user data
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// tslint:disable-next-line:no-console
console.debug('given_name');
}
});
}
}
而且,我在 IdentityServer 中的代码是这样的:
new Client
{
ClientId = "angular-client",
ClientName = "Angular Client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
AllowAccessTokensViaBrowser = true,
EnableLocalLogin = true,
AllowedCorsOrigins =
{
"httpsd://localhost:4200",
"http://localhost:4200"
},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"api1"
},
ClientSecrets = new List<Secret>() {
new Secret("password".Sha256())
}
},
控制台的日志为:
请帮忙。
将 openid 范围添加到您的 Angular 代码中:
this.oAuthService.scope = 'openid api1';
您的客户端确实应该使用隐式流程进行配置
AllowedGrantTypes = GrantTypes.Implicit,