具有redux -oidc客户端的identityserver4请求了访问令牌-但未将客户端配置为通过浏览器接收访问令牌

Day*_*yan 5 redux redux-thunk redux-saga react-redux identityserver4

我的identityserver4客户端如下所示:

new Client {
    ClientId = "openIdConnectClient",
    ClientName = "Example Implicit Client Application",
    //AllowedGrantTypes = GrantTypes.Implicit,
    AllowedGrantTypes = GrantTypes.ClientCredentials,
    ClientSecrets =
    {
       new Secret("secret".Sha256())
    },
    AllowOfflineAccess = true,
    AllowAccessTokensViaBrowser = true,
    AccessTokenLifetime = 30,
    AllowedScopes = new List<string>
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        "role",
        "customAPI.write"
    },
    RedirectUris = new List<string> {"http://localhost:8080/callback"},
    PostLogoutRedirectUris = new List<string> {"https://localhost:44330"},
    AllowedCorsOrigins = new List<string>
     {
         "http://127.0.0.1:8080",
         "http://localhost:8080",
         "*"
     },
}
Run Code Online (Sandbox Code Playgroud)

在react应用程序中,我的userManager类如下所示:

 import { createUserManager } from 'redux-oidc';

const userManagerConfig = {
  client_id: 'openIdConnectClient',
  redirect_uri: `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}/callback`,
  //response_type: 'code id_token token',
  response_type: 'token id_token',
  scope: 'openid profile email role',
  authority: 'http://localhost:50604',
  silent_redirect_uri: `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}/silent_renew.html`,
  automaticSilentRenew: true,
  filterProtocolClaims: true,
  loadUserInfo: true,
};

const userManager = createUserManager(userManagerConfig);

export default userManager;
Run Code Online (Sandbox Code Playgroud)

问题是:当我尝试从redux-oidc示例应用程序调用我的identityserver4时。我收到以下错误:

客户端请求访问令牌-但客户端未配置为通过浏览器接收访问令牌

我希望你理解这个问题。请有人帮助我。我已经为下面的示例应用程序提供了链接。

Redux-oidc示例应用程序链接

DaI*_*mTo 11

您的代码包含两种不同的授权类型。身份服务器4中的不同授予类型具有不同的要求。这里有一些信息可帮助您了解所使用的不同类型。它还可以帮助您了解为什么会遇到此问题。

GrantTypes.ClientCredentials

客户端凭据是最简单的授予类型,用于服务器到服务器的通信-令牌始终代表客户端而不是用户请求。

使用这种授予类型,您可以将令牌请求发送到令牌端点,并获得代表客户端的访问令牌。客户端通常必须使用其客户端ID和密码与令牌端点进行身份验证。

new Client
    {
        ClientId = "client",

        // no interactive user, use the clientid/secret for authentication
        AllowedGrantTypes = GrantTypes.ClientCredentials,

        // secret for authentication
        ClientSecrets =
        {
            new Secret("secret".Sha256())
        },

        // scopes that client has access to
        AllowedScopes = { "api1" }
    }
Run Code Online (Sandbox Code Playgroud)

GrantTypes.Implicit

隐式授予类型针对基于浏览器的应用程序进行了优化。仅用于用户身份验证(服务器端和JavaScript应用程序),或者用于身份验证和访问令牌请求(JavaScript应用程序)。

在隐式流中,所有令牌都通过浏览器传输,因此不允许使用高级功能(如刷新令牌)。如果要通过浏览器通道传输访问令牌,则还需要在客户端配置中明确允许它:

Client.AllowAccessTokensViaBrowser = true;


 new Client
    {
        ClientId = "mvc",
        ClientName = "MVC Client",
        AllowedGrantTypes = GrantTypes.Implicit,

        // where to redirect to after login
        RedirectUris = { "http://localhost:5002/signin-oidc" },

        // where to redirect to after logout
        PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

        AllowedScopes = new List<string>
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile
        },
        AllowAccessTokensViaBrowser = true
    }
Run Code Online (Sandbox Code Playgroud)