首页 > 解决方案 > JS 报告服务器控制台错误:授权服务器在其响应中没有“用户名”字段,令牌假定为无效

问题描述

我的带有客户端 ui 应用程序的 js 报表服务器正在使用通过在身份服务器中添加一个声明(例如 Claim('username', 'user name value'))提交的用户名。4 版本 1.1。但是最近客户端已将 identityserver 4 版本升级到 2.2.0 和 netcoreapp2.1 。

现在客户端 ui 应用程序收到“未经授权”错误。当我在本地运行应用程序时,我在 jsreport 服务器控制台中看到一个错误:错误:授权服务器在其响应中没有“用户名”字段,令牌假定为无效。

我试图在示例中找到解决方案: https ://github.com/bjrmatos/jsreport-with-authorization-server-sample但他们尚未升级最新的.net 核心和身份服务器的示例,我看到了节点Node.js 示例链接“ https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/NodeJsApi ”不存在。所以我无法解决这个问题。有人可以帮我吗?

提前致谢。

标签: identityserver4jsreport

解决方案


我找到了解决此错误的解决方案,方法是添加带有用户名的声明类型并在 api 资源范围内声明:

         new ApiResource
            {
                Name="jsreportscope",
                DisplayName = "JavaScript based reporting platform",
                Scopes=
                {
                    new Scope()
                    {
                        Name="jsreportscope",
                        UserClaims = new List<string>(){"username" }
                    }
                },
                UserClaims = new List<string>(){"username"},
                ApiSecrets = { new Secret("yoursecret".Sha256()) },

            }

但是现在它解决了之前的问题,我通过添加与用户名字段值的值与身份服务器 1.1 版本匹配的声明类型来修复,但现在我们已将身份服务器版本升级到 2.1 并再次收到错误。它能够授权身份服务器的任何用户访问报告。这是我正在使用的jsreport.config.js代码:

{
   "store": { "provider": "fs" },
   "httpPort": 5004,
   "allowLocalFilesAccess": true,
   "extensions": {
    "authentication": {
       "cookieSession": {
       "secret": "<your strong secret>"
    },
    "admin": {
         "username": "IdentityServer4User@domain.com",
         "password": "Password"
    },
    "authorizationServer": {
       "tokenValidation": {
           "endpoint": "http://localhost:5000/connect/introspect",
           "usernameField": "username",
           "activeField": "active",
        "scope": {
           "valid": ["jsreportscope"]
        },
        "auth": {
           "type": "basic",
           "basic": {
               "clientId": "jsreport",
               "clientSecret": "yoursecret"
           }
        }
      }
    },
    "enabled": true
   },
   "authorization": {
     "enabled": true
   },
   "sample-template": {
      "createSamples": true
   }
 }
} 

但是现在我可以登录并从报表服务器访问报表,如果仅由用户IdentityServer4User@domain.com登录,任何其他用户都会收到未经授权的错误。在报表服务器控制台中,错误显示如下:

错误:从授权服务器返回的用户名“OtherIdentityServer4User@domain.com”不是 jsreport 用户。
我不想将所有身份服务器用户添加到 js 报表服务器。


推荐阅读