繁体   English   中英

具有客户端Angular的IdentityServer4中具有无效签名的令牌

[英]Token with invalid signature in IdentityServer4 with client Angular

我正在使用Client Angular2开发一个应用程序网络,并使用IdentityServer4开发ASP.NET Core 2中的后端,最后使用identserver IdentityServer4保护一个webapi。 我成功实现了后端,并且mi webapi得到了保护。 我的客户端Angular也受到保护,并使用在IdentityServer中配置的客户端中的mi客户端登录,并在其中设置AllowedGrantTypes,并设置密码和用户流。 问题是令牌的生成。 我可以生成带有有效负载和标头有效的令牌,但是我没有使签名有效。 在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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM