box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5

Home Page:https://developer.box.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

await boxJWT.AdminTokenAsync() method not returning back using browser and get stuck

narayana476 opened this issue · comments

Hi, I am using Box.V2 .NET SDK downloaded from website https://developer.box.com/sdks-and-tools/#official-ui-libraries
and face below browser issue with .NET Framework 4.5 version or .NET Framework 4.7.2 version

Below line var adminToken = await boxJWT.AdminTokenAsync(); is not returning back after generated token and code is not flowing to next line when used in default .NET Framework webforms website with .NET Framework 4.5 version or .NET Framework 4.7.2 version

public static async Task CreateFolderTwoAsync(String folderName, string parentBoxFolderId)
{
BoxFolder result = null;
var boxConfig = new BoxConfigBuilder(ClientId, ClientSecret, EnterpriseId, PrivateKey.Replace(@"\n", "\n"), PrivateKeyPassword, PublicKeyId)
.Build();
var boxJWT = new BoxJWTAuth(boxConfig);
var adminToken = await boxJWT.AdminTokenAsync();
var adminClient = boxJWT.AdminClient(adminToken);
BoxFolderRequest request = new Box.V2.Models.BoxFolderRequest { Name = folderName, Parent = new Box.V2.Models.BoxRequestEntity { Id = parentBoxFolderId } };
result= await adminClient.FoldersManager.CreateAsync(request);
return result;
}

image

Hi @narayana476,
Can you try to use JWT json config file instead of passing all details manually (docs):
var config = BoxConfigBuilder.CreateFromJsonString(jsonConfig).Build();
It may help us to exclude some possible sources of that problem.
Best,
@lukaszsocha2

Had same problem with var config = BoxConfigBuilder.CreateFromJsonString(jsonConfig).Build();
My browser is stalled and code is not flowing after successful token generation. Confirm if any additional software installation required for async methods while using Visaul Studio 2019 default installation..

Hi @narayana476,
it's really hard to debug it not knowing your project setup. The sample you provided seems to be correct and we cannot reproduce your error. Can you please send us your entire project (preferably link to GitHub), so that we can test it?

code available in this path# https://github.com/narayana476/BoxBrowserTest
Use highlighted BoxPersonal subproject as Startup Project and Contact.aspx as startpage and click the button
'Box Create Two' button for debug test

image

Hi @narayana476
I see that you are trying to run async sdk methods as synchronous. To properly handle synchronization context in this case, you should add .ConfigureAwait(false); to all awaited calls in CreateFolderTwoAsync method.
Example:
adminToken = await boxJWT.AdminTokenAsync().ConfigureAwait(false);

This is not ideal and error-prone approach (e.g deadlocks could occur in some cases). Instead, you can try to make the on click handler asynchronous as well. To do this you need to add Async="true" to the Page component of the Contact.aspx file. Then you can change signature of the on BoxCreateTwo_Click to
protected async void BoxCreateTwo_Click(object sender, EventArgs e)
and await call to CreateFolderTwoAsync normally.

Also, you uploaded the config of your application to this repo and I'm not sure if that was intended. Consider recreating this application or regenerating the keys at least.

Hi @mwwoda,

I see the code is working fine now with .ConfigureAwait(false) for my await calls in web browser. Thanks for the fix
I am removing public access to my github project which was using Developer ClientID and ClientSecrets keys only for testing purpose. This fix resolved our production system issue.

Great! I'm glad your problem has been fixed.