0

I am using OpenIddict in .Net Core 6.0 for token-based authentication and authorization for my REST/Web API It works within Visual Studio IIS Express but not on my Local IIS server.

When I call the Token API using Visual Studio's IIS Express, I get the correct token in the response

Request: POST - http://localhost:5023/token

BODY - x-www-form-urlencoded client_id:xxxxx client_secret:xxxxxx grant_type:client_credentials

Response:

{
    "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IkYzNTYzRTU0OTkwNEZFOENFRjg2NkI1RjRCNjc1MDU0NzFGQjcwQjciLCJ4NXQiOiI4MVktVkprRV9venZobXRmUzJkUVZISDdjTGMiLCJ0eXAiOiJhdCtqd3QifQ.eyJzdWIiOiJrYW1hbCIsIm5hbWUiOiJrYW1hbCIsIm9pX3Byc3QiOiJrYW1hbCIsImNsaWVudF9pZCI6ImthbWFsIiwib2lfdGtuX2lkIjoiNThiYWJkODctNmFjZC00MTc2LWIxNTctMGE2YWNlMGRkMjY3IiwianRpIjoiOTBlOWNmOWItNjNjMi00MDVhLTk3ZjQtM2Q4YjQyYTEwMjI3IiwiZXhwIjoxNjc4NzkyNDY1LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjUwMjMvIiwiaWF0IjoxNjc4Nzg4ODY1fQ.fj7iNpDrESf6duOIaPKEiL1wmSsMaHUVJpkEsY0LlE-4KVfplnkEtfVOXpF3B-NyEtbwEZnGKadtXMEBAs2168p8egkDe-X2Kkc1jJPv0joO_iJlcBXdT4e0kLQr-L8j3b0Cro1hw7K1FQimgwR04PIPqVOj4m4Q0xzge7-Ism5-i-JYtuIdGQNt2gfd-z_DTjIuV3zHXLv0qVGu6uv5b0aCM0EZFuzXOg_4WurqwM68I4lhHycS5KI2NiqRJnubA6de_uwjsVwzKvNOA_q5MG6XXh3xy8v_NwL4qrDPZj3mO9VXc2RZDYt4IIeASNTlpJ9e92Q89odF3ZTYrnh_yA",
    "token_type": "Bearer",
    "expires_in": 3599
}

However, when I call the Token API using Local IIS Server, I get the the 500 Internal Server error. On debugging I found that in the method public async Task Exchange(), request object is set as null in the following line:

var request = HttpContext.GetOpenIddictServerRequest();

This is how I setup OpenIddict in my Program.cs (.NET Core 6.0)

builder.Services.AddOpenIddict()
        .AddCore(options =>
        {
            // Configure OpenIddict to use the EF Core stores/models.
            options.UseEntityFrameworkCore()
                   .UseDbContext<OpenIDDBContext>();
        })
    .AddServer(options =>
    {        
        options
            .SetTokenEndpointUris("/token");
        options
               .AllowClientCredentialsFlow();

        // Encryption and signing of tokens
        options
            .AddEphemeralEncryptionKey()
            .AddEphemeralSigningKey()
            .DisableAccessTokenEncryption();

        // Register scopes (permissions)
        //options.RegisterScopes("api");

        // Register signing and encryption details
        options.AddDevelopmentEncryptionCertificate()
            .AddDevelopmentSigningCertificate();

        // Register the ASP.NET Core host and configure the ASP.NET Core-specific options.
        options
            .UseAspNetCore()
            .EnableTokenEndpointPassthrough()
            .DisableTransportSecurityRequirement();
    })
    .AddValidation(options =>
    {
        // Import the configuration from the local OpenIddict server instance.
        options.UseLocalServer();

        // Register the ASP.NET Core host.
        options.UseAspNetCore();
    });

Can anyone please point out what needs to be corrected so that Token authentication works on Local IIS Server we well.

Thanks !

Gags
  • 827
  • 2
  • 13
  • 29
  • Try using [failed request tracing](https://learn.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis) to see details about 500 error, this will generate detail log file, which will help you to identify the problem. – samwu Mar 15 '23 at 03:05
  • @samwu Failed Request Tracing did not provide anything more that could help. – Gags Mar 16 '23 at 07:52
  • You can also try to debug the cause using IIS Debug Diagnostics Tool, about how to use the Debug Diagnostics tool you can refer to this link: [How to use the Debug Diagnostics tool](https://support.microsoft.com/en-us/topic/how-to-use-the-debug-diagnostics-tool-to-troubleshoot-a-process-that-has-stopped-responding-in-iis-995db9a3-a3be-6d20-cf2f-c48101a64444#:~:text=follow%20these%20steps%3A-,Click%20Start%2C%20click%20Run%2C%20type%20the%20path%20of%20the%20Debug,analyze%2C%20and%20then%20click%20Open.). – samwu Mar 17 '23 at 10:22

0 Answers0