JWT: not able to get access token

Highlighted
New Contributor

JWT: not able to get access token

I am using Box .NET SDK. I have created a Box App with JWT and it has been authorized by admin. If I use a developer token all is well. If I try to get a UserToken or AdminToken the application hangs on the call to get the token. When I step through my code the call to AdminToken does not return, it just hangs.

var reader = new StreamReader(HostingEnvironment.MapPath("~/Scripts/BoxConfig.json"));
var json = reader.ReadToEnd();
var config = BoxConfig.CreateFromJsonString(json);
BoxJWTAuth boxJWT = new BoxJWTAuth(config);
var userToken = boxJWT.AdminToken();

 

Tags (1)
14 REPLIES 14
New Contributor

Re: JWT: not able to get access token

I am having this same issue. The developer token works w/out any issues but the I hang on the Session.AdminToken(); call everytime. I'm not sure what I am missing as I have tried updating every option in the custom app configuration I can think of, and even reauthorizing each time. Is there something wrong with the latest SDK for .NET framework?

Highlighted
New Contributor

Re: JWT: not able to get access token

I wasn't expecting this but when I tried this it appears I have an issue only when using .NET framework. If I attempt to obtain a token using .NET Core it works without issue. I tested using the nuget packages Box.V2 for framework and Box.V2.Core for core.

Highlighted
New Contributor

Re: JWT: not able to get access token

Here is some more added details I've found while digging into this issue:

 

The JWT AdminToken can be created with a .NET Core 3.0 Windows Console build.

I cannot get a token created using .NET Framework 4.5, 4.6, 4.7 OR .NET Core 3.0, 3.1 Windows Forms build.

 

There is some reason that the Windows Forms will not work but the Console works just fine. 

Highlighted
New Contributor

Re: JWT: not able to get access token

Ok, I think I may have found a solution/workaround.

 

In my program I'm trying to kickoff the authentication on the form constructor class and it always hangs. However, if I initialize the session and attempt to get a token in the 'Main" program I'm able to get one successfully. Not sure why the token can't pull from within a class but it works in the 'Main' so maybe that will help you.

Tags (2)
Highlighted
New Contributor

Re: JWT: not able to get access token

@gjregions Thank you for your work on this. I am trying to do this from a .NET MVC web application. There is no "Main" program. I am not sure how I can request an access token from a web application

Highlighted
First-time Contributor

Re: JWT: not able to get access token

I'm also experiencing this same issue. I have two different Core 3.1 console apps, running on different servers, and both are throwing a NullReferenceException when attempting to get the AdminToken. The line in red is the one throwing the exception.

 

token.jpg

 

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Box.V2.JWTAuth.BoxJWTAuth.GetToken(String subType, String subId)
at Box.V2.JWTAuth.BoxJWTAuth.AdminToken()

 

This error has been surfacing on and off over the last couple of days. At certain times, both apps fail with this exception. Later on, both will be working normally again and then later on both will be failing again.

 

Some other recent threads involving difficulty with tokens. The first is similar to what I'm experiencing, the other two involve a 400 error that may or may not be related:

 

https://community.box.com/t5/Platform-and-Development-Forum/Box-V2-JWTAuth-BoxJWTAuth-GetToken-throw...

 

https://community.box.com/t5/Platform-and-Development-Forum/Getting-an-Error-when-creating-a-User-Ac...

 

https://community.box.com/t5/Platform-and-Development-Forum/Can-t-get-access-token-when-Salesforce-s...

 

Highlighted
First-time Contributor

Re: JWT: not able to get access token

Same here. I have a win forms poc i did a couple of months ago, it was working just fine, now it just hangs. I updated the packages, nothing. I then started looking into it, tried a copy paste of the .net core example, new application, and now i am getting (on .net core) the null reference exception.

Highlighted
Occasional Contributor

Re: JWT: not able to get access token

I'm having the same problem, and I have not found a solution, but I want to add to the conversation.

In particular, I want to eliminate any concerns about various versions of .NET involved.

I downloaded the Box.V2 SDK source code from GitHub and linked it into my code. (If you want to do this yourself, (a) download the box-windows-sdk-v2-master.zip file from GitHub, (b) remove the NuGet reference to Box.V2 from your project, (c) add the Box.V2.csproj to your solution, (d) reference the (now local) Box.V2 project and (e) recompile.)

 

What I have found is the hang occurs in HttpRequestHandler.cs on line #177 in Box.V2 when linked to my ASP.NET application, but not when linked to a Console application.  There are no other differences in what I am doing to get a Box token, i.e. I am using the same computer, same user, same Box config file, same version of .NET, same VisualStudio, etc.

 

Below is a QuickWatch of the response object on line #177 in HttpRequestHandler.cs.  This is running in my ASP.NET MVC application.  The response status code from the POST to https://api.box.com/oauth2/token is “400 - Bad Request".

alfredbr_1-1588954721191.png

The response status code from the POST to https://api.box.com/oauth2/token from the Console app is  “200 - OK” 

 

alfredbr_0-1588955101209.png

 

 

I hope this adds to the conversation.  I really want this to work.

 
 

 

 

Highlighted
Occasional Contributor

Re: JWT: not able to get access token

A hint from @adblondin on this thread (https://community.box.com/t5/Platform-and-Development-Forum/NET-SDK-code-hangs-at-AdminToken/m-p/562...) led me to this approach:

Yes, you can get this to work if you create the AdminToken in a thread other than your main controller thread. 

This is what worked for me:

BoxClient _boxClient = null;
var thread = new System.Threading.Thread(() =>
{
   var boxJWT = new BoxJWTAuth(boxConfig); // create boxConfig elsewhere
   var adminToken = boxJWT.AdminToken(); // create adminToken in new thread
   _boxClient = boxJWT.AdminClient(adminToken); // set your _boxClient
});
thread.Start(); // start the thread
thread.Join(); // wait for the thread to finish (ugly code).
Debug.Assert(_boxClient != null); // success!

 

Highlighted
Occasional Contributor

Re: JWT: not able to get access token

Alternatively, if you have the Box.V2 SDK source, you can add .ConfigureAwait(false); to the end of line #41 in HttpRequestHandler.cs

 

alfredbr_0-1588966880294.png

 

Highlighted
Box Employee

Re: JWT: not able to get access token

Hey Everyone, 

 

We are aware of the issue with getting Admin Tokens using the .NET SDK. We have a PR that is working its way through the process. Once that PR lands successfully we will cut a new release of the SDK. Our ETA for this is early next week. 


Thanks,

John Townsend

Highlighted
Occasional Contributor

Re: JWT: not able to get access token

We also need a .ConfigureAwait(false); at the end of line 71 in HttpRequestHandler.cs in the ExecuteAsync() method.  (This is in addition to the one on line 41)

 

 

Highlighted
First-time Contributor

Re: JWT: not able to get access token

I have same issue, but I had solved fortunately.

 

Did everyone set client-id at the page(https://app.box.com/master/custom-apps)?

Once you set your application's client-id and push "Authorize New App" button at the page, this issue may be solved.


If you don't set  a client-id as an Authorized App, "Box.V2.Core 3.2x" will return "NullReferenceException" and  "Box.V2.Core 3.10.0"  will return "BadRequest".

 

I think this root cause is showing an inappropriate error message by "Box.V2.Core 3.2x".
Because in case of using "Box.V2.Core 3.10.0", we can recognize what we should do to solve this issue.

Highlighted
First-time Contributor

Re: JWT: not able to get access token

Any updates on this problem?

 

My app works fine on the desktop VS Studio environment I developed it on as well as a Windows Server box.  I have copied my code to my laptop and I can't get past the call to AdminToken.

 

var boxConfig = new BoxConfig(CLIENT_ID, CLIENT_SECRET, ENTERPRISE_ID, privateKey, JWT_PRIVATE_KEY_PASSWORD, JWT_PUBLIC_KEY_ID);
var boxJWT = new BoxJWTAuth(boxConfig);

var adminToken = boxJWT.AdminToken();

 

I am getting this exception::

System.NullReferenceException