okta-sdk-dotnet icon indicating copy to clipboard operation
okta-sdk-dotnet copied to clipboard

OKTA-SDK-Dotnet null reference exception

Open mmusaev opened this issue 2 years ago • 5 comments

Describe the bug?

I followed this example to create an instance of GroupAPI:

var privateKey = new JsonWebKeyConfiguration { P = “{{P}}”, Kty = “RSA”, Q = “{{Q}}”, D = “{{D}}”, E = “{{E}}”, Kid = “{{P}}”, Qi = “{{Qi}}” };

var configuration = new Configuration { OktaDomain = “https://{{yourOktaDomain}}”, AuthorizationMode = AuthorizationMode.PrivateKey, ClientId = “{{clientId}}”, Scopes = new List { “okta.users.read”, “okta.apps.read”, “okta.groups.manage”, “okta.groups.manage” }, // Add all the scopes you need PrivateKey = privateKey };

services.AddScoped(_ => new GroupApi(configuration ));

However, I get null reference exception when creating GroupApi passing the configuration object. After ton of raised exceptions, the code eventually succeeds. I am able to connect to my okta tenant and get the response data from okta api calls.

Thank you, Marat

What is expected to happen?

There should not be any exceptions raised

What is the actual behavior?

I don't expect so many exceptions raised.

Reproduction Steps?

To reproduce the problem, select the checkbox in the Exceptions Settings window within Visual Studio 2022. I am using the latest okta-sdk-dotnet version.

Additional Information?

The code is used within NET 6 api project in Program.cs class.

.NET Version

The code is used within NET 6 api project in Program.cs class.

SDK Version

Happens both in OKTA.SDK 6.0.7 and 6.0.10.

OS version

Windows 10

mmusaev avatar May 17 '23 23:05 mmusaev

Hi @mmusaev , Thanks for reporting this issue. I'll file an internal ticket for our team.

Internal Ref: OKTA-611865

laura-rodriguez avatar May 18 '23 13:05 laura-rodriguez

This is a great find @mmusaev and something I hope gets updated soon for the dotnet SDK.

andrewqsmith avatar Jul 18 '23 19:07 andrewqsmith

The NullReferenceException is thrown while creating an instance of any API client. In my case its the UserApi. On digging further, its because of the call to configuration = Sdk.Client.Configuration.GetConfigurationOrDefault(configuration);

this in turn calls configBuilder.Build().GetSection("okta").GetSection("client").Bind(compiledConfig);

and since there is no okta:client or okta:testing section in the config, the null reference exception is thrown on the Bind method.

chai-deshpande avatar Jul 21 '23 07:07 chai-deshpande

I modified OKTA SDK code to fix the issue locally. In my opinion, a library code should be not responsible for initializing the settings but it should be the responsibility of a client application to set the configuration(the separation of concerns). I added an extensibility point to the code to allow the client app to provide that logic. Otherwise, the library code will be making a best guess and provide extra options that will have unintended side effects, as is the case here.

mmusaev avatar Jul 21 '23 22:07 mmusaev

I agree wholly with everyone's assessment here. Until a fix is in place, a quick dirty workaround is to use the overload accepting the IAsynchronousClient parameter. Keyword here is dirty but it accomplishes the same as the first constructor, just without configuration validation. I see there's a PR in place that fixes this issue but hasn't had any movement in months.

Workaround:

        var asyncClient = new ApiClient(config.OktaDomain);
        var userApi = new UserApi(asyncClient, config);

mmoreno79 avatar Mar 28 '24 15:03 mmoreno79

@mmusaev I apologize for the delay in review and resolution of this issue. Using the reproduction code provided I am not able to reproduce the issue locally on a machine that lacks Okta configuration of any kind. Is this still an issue? Is there perhaps a specific kind of project where the exception occurs? If you are able to provide a reference project that reproduces the issue that would be greatly helpful. cc @mmoreno79. Thank you for your engagement and thanks for using Okta!

bryanapellanes-okta avatar May 23 '24 13:05 bryanapellanes-okta

closing due to inactivity.

bryanapellanes-okta avatar Jun 12 '24 16:06 bryanapellanes-okta