C# .NET:服务器无法验证Angular客户端[IdentityServer]

8

我有以下设置:

前端:AngularJS应用程序

后端:WebApi与Identity Server一起验证客户端

在我的后端中,我这样创建一个新的内存客户端:

new Client
{
    Enabled = true,
    ClientId = "myapp.mycompany",
    ClientUri = "https://myapp.mycompany.com",
    ClientName = "My Client",
    Flow = Flows.Implicit,
    AllowAccessToAllScopes = true,
    IdentityTokenLifetime = 300,
    AccessTokenLifetime = 3600,
    RequireConsent = false,
    RedirectUris = new List<string>
    {
        "https://myapp.mycompany.com/assets/idSrv/callback.html",
        "https://myapp.mycompany.com/assets/idSrv/silentrefreshframe.html"
    },
    PostLogoutRedirectUris = new List<string>
    {
        "https://myapp.mycompany.com/index.html"
    }
},

在我的前端代码中,我有以下声明客户端的代码,我使用oidc-token-manager.js客户端

var authority = 'https://sts.mycompany.com/identity';

return {
    baseUri: protocol,
    tokenConfig: {
        'client_id': 'myapp.mycompany',
        'authority': authority,
        'redirect_uri': 'https://myapp.mycompany.com/assets/idSrv/callback.html',
        'post_logout_redirect_uri': 'https://myapp.mycompany.com/index.html',
        'response_type': 'id_token token',
        'scope': 'openid profile roleScope webApiScope',
        'silent_redirect_uri': 'https://myapp.mycompany.com/assets/idSrv/silentrefreshframe.html',
        'silent_renew': true
    },
    isDebugging: isDebugging
};

当我尝试访问我的网站时:

http://myapp.mycompany.com

我遇到了以下错误:

客户端应用程序未知或未经授权。

我已启用日志记录,以下是我的日志记录:

"Unknown client or not enabled: myapp.mycompany"
 "{
  \"RedirectUri\": \"https://myapp.mycompany.com/assets/idSrv/callback.html\",
  \"SubjectId\": \"unknown\",
  \"Flow\": \"AuthorizationCode\",
  \"RequestedScopes\": \"\",
  \"Raw\": {
    \"state\": \"18141519257414835\",
    \"nonce\": \"8585758378803323\",
    \"client_id\": \"myapp.mycompany\",
    \"redirect_uri\": \"https://myapp.mycompany.com/assets/idSrv/callback.html\",
    \"response_type\": \"id_token token\",
    \"scope\": \"openid profile roleScope webApiScope\"
  }
}"

End authorize request
3001: "Endpoint failure" / "Endpoints" (Failure), Context: EventContext { ..., Details: EndpointDetail { EndpointName: "authorize" }

1
客户端ID和重定向URI在客户端和服务器之间是相同的,不确定为什么它说无法识别客户端。 - Eric Bergman
我已将其添加到Cors Origins,但仍然得到相同的结果。“客户端应用程序未知或未经授权。” - Eric Bergman
有人能帮忙吗? - Eric Bergman
你确定你的服务器正在提供https服务吗? - Leo Bartkus
看起来你没有正确地注册你的客户端。你确定你的客户端已经被添加到身份服务器客户端存储中了吗? - bobek
2个回答

5

也许您需要在CORS来源中允许客户端根,我发现流程不同。
我看到您的客户端配置使用了隐式流,但服务器却显示授权码流!

  "myApp": {
    "ClientId": "spa-myApp",
    "ClientName": "myAppSPA",
    "ClientUri": "http://localhost:4200",
    "RequireConsent": false,
    "AllowedGrantTypes": [ "implicit" ],
    "AllowAccessTokensViaBrowser": true,
    "RedirectUris": [
      "http://localhost:4200/assets/html/popup-login-redirect.html",
      "http://localhost:4200/assets/html/silent-refresh-redirect.html"
    ],
    "PostLogoutRedirectUris": [ "http://localhost:4200?postLogout=true" ],
    "FrontChannelLogoutUri": "http://localhost:4200?frontchannellogout=true",
    "FrontChannelLogoutSessionRequired": true,
    "AllowedCorsOrigins": [ "http://localhost:4200" ], // here you have to add your client root
    "AllowedScopes": [ "openid", "profile", "qsdqsdqs", "qdqsd" ],
    "IdentityTokenLifetime": 18000,
    "AccessTokenLifetime": 18000
  },

我已将其添加到Cors Origins,但仍然获得相同的结果。"客户端应用程序未知或未经授权。" - Eric Bergman
流程不匹配,请检查我更新的答案。 - Fateh Mohamed
在服务器中,我使用隐式流:Flow = Flows.Implicit,在我的客户端代码中,我指定了scoperesponse_type的正确值。我必须提到我正在使用IdentityServer3和oidc-token-manager.js客户端。我注意到日志中出现了“AuthorizationCode”的字样,这让我感到困惑,因为为什么Flow会从Implicit转换为AuthorizationCode呢? - Eric Bergman
服务器中的流程是隐式的。 - Eric Bergman

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接