Search code examples
c#identityserver4introspection

Accessing IdentityServer4 Introspection from .NET 4.5


I'm trying to access an Id4 server from an older client application. I'm able to get a token fine, but when I try to verify the token I'm getting a 415 - 'Unsupported Media Type' error when calling client.SendAsync. Any ideas on what setting I'm missing? These values work in Postman.

public bool IsTokenValid(string token)
    {
        bool isTokenValid = false;

        HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));            
        client.DefaultRequestHeaders.Add("Authorization", "Basic UHJvZml0U2FnZUFwaTpnR2VTZ0luRW9r");

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, _url + "/connect/introspect");
        request.Content = new StringContent("token=" + token);

        try
        {
            HttpResponseMessage response = client.SendAsync(request).Result; 
        }
        catch (Exception ex)
        {               
            Error = ex;
        }

        client.Dispose();

        return isTokenValid;

    }

Solution

  • Try and add a ‘Content-Type’ header with value ‘application/x-www-form-urlencoded’ or ‘multipart/form-data’ and I believe that should fix your problem.

    This is because the introspection endpoint returns the unsupported media type response code when the request does not have a form content type: https://github.com/IdentityServer/IdentityServer4/blob/63a50d7838af25896fbf836ea4e4f37b5e179cd8/src/Endpoints/IntrospectionEndpoint.cs