title: How to Get Access Token and User Info: IdentityServer4 With AspNet Identity
body: <p>Good day everyone,</p>
  <p>So I successfully integrated IdentityServer 4 using AspNet Identity for authentication  into my project. It redirects to the client after authentication, so thats fine. The problem now is that I would like to get the user details on the client, like their username, email, firstname and lastname. I wanted to call the userinfo endpoint with a token, but I cannot get a token. I get the following errors:</p>
  <ol>
  <li>Client not authorized for client credentials flow, check the AllowedGrantTypes setting - When I use <code>GrantType.Authorization</code> Code in client config</li>
  <li>client cannot request openid scopes in client credentials flow - When I use <code>GrantType.ClientCredentials</code></li>
  </ol>
  <p>The The client has been setup in startup.cs as;</p>
  <pre><code>services.AddAuthentication(options =&gt;
              {
                  options.DefaultScheme = &quot;cookie&quot;;
                  options.DefaultChallengeScheme = &quot;oidc&quot;;
              })
              .AddCookie(&quot;cookie&quot;)
              .AddOpenIdConnect(&quot;oidc&quot;, options =&gt;
              {
                  options.Authority = ConfigurationManager.AppSettings[&quot;AuthorityUrl&quot;];
                  options.ClientId = ConfigurationManager.AppSettings[&quot;ClientId&quot;];
                  options.ClientSecret = ConfigurationManager.AppSettings[&quot;ClientSecret&quot;];
                  options.RequireHttpsMetadata = false;
                  options.GetClaimsFromUserInfoEndpoint = true;
  
                  options.ResponseType = &quot;code&quot;;
                  options.UsePkce = true;
                  options.ResponseMode = &quot;query&quot;;
  
                  options.Scope.Add(&quot;openid&quot;);
                  options.Scope.Add(&quot;profile&quot;);
                  options.SaveTokens = true;
  
              });
  </code></pre>
  <p>And my client configuration currently is as follows;</p>
  <pre><code>new Client
                      {
                           ClientName = &quot;Admin Client&quot;,
                           ClientId = &quot;admin.client&quot;,
                           AllowedGrantTypes = GrantTypes.Code,
                           ClientSecrets = { new Secret(&quot;secretkey&quot;.Sha256()) },
                           AllowedScopes =new List&lt;string&gt; {
                                 IdentityServerConstants.StandardScopes.OpenId,
                                 IdentityServerConstants.StandardScopes.Profile
                              },
                           RedirectUris = {&quot;https://localhost:44324/signin-oidc&quot;},
                           FrontChannelLogoutUri = &quot;https://localhost:44324/signout-oidc&quot;,
                           PostLogoutRedirectUris = {&quot;https://localhost:44324/signout-callback-oidc&quot;},
  
                           AllowOfflineAccess = true,
                           RequirePkce = true,
                           RequireConsent = false,
                           AllowPlainTextPkce = false
  
                      }
  </code></pre>
  <p>To get token, I do this (when I use <code>GrantType.ClientCredentials</code> in Client config;</p>
  <pre><code>using var client = new HttpClient();
              var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
              {
                  Address = _discoveryDocument.TokenEndpoint,
                  ClientId = ConfigurationManager.AppSettings[&quot;ClientId&quot;],
                  ClientSecret = ConfigurationManager.AppSettings[&quot;ClientSecret&quot;],
                  Scope = &quot;openid&quot;
  
              });
  </code></pre>
  <p>And I call user like this:</p>
  <pre><code>var response = await client.GetUserInfoAsync(new UserInfoRequest
              {
                  Address = _discoveryDocument.UserInfoEndpoint,
                  Token = token
              });
  </code></pre>
  <p>but it never gets to the userinfo though.
  Please note that I have been changing the GrantTypes in the client config to Hybrid, AuhorizationCode, it still doesn't work. When I use AuthorizationCode grantType and Use <code>RequestAuthorizationCodeTokenAsync</code>, it request for Code in the parameters, but I don't know how to get the authorization code (sorry, I'm new to this please).</p>
  <p>So please how should I configure my client in order to be able to request token that I can use in the userinfo endpoint or is there any way I can get the user info?</p>
  <p>Please help.
  Thanks.</p>