-2

I am working on the Forge API tutorial at https://learnforge.autodesk.io/#/?id=learn-autodesk-forge but am having an issue. I am using the .NET (C#) examples and have followed through to the end but when I try to run the app it gives an error. When I run it I get the login page as I am supposed to

Forge Signin

But when I click AUTODESK Sign In it throws an error

enter image description here

It appears the Credentials object is being returned as NULL. The code it is calling to get this object is as follows:

   public static async Task<Credentials> FromSessionAsync()
        {
            if (HttpContext.Current.Session == null || 
                HttpContext.Current.Session["ForgeCredentials"] == null)
                return null;

            Credentials credentials = JsonConvert.DeserializeObject<Credentials>(HttpContext.Current.Session["ForgeCredentials"].ToString());
            if (credentials.ExpiresAt < DateTime.Now) await credentials.RefreshAsync();
            return credentials;
        }    

I am not sure where the session Credentials are coming from in that block of code.

I am new to the Forge API so any help will be greatly appreciated.

Thanks Gerry

Gerry
  • 23
  • 5

1 Answers1

0

Here's what's happening after you log in with your Autodesk credentials, in chronological order:

  • the Autodesk login page redirects you back to your Forge application's callback URL (usually set to something like api/forge/callback/oauth)

  • in your code there's a handler for the api/forge/callback/oauth route which calls Credentials.CreateFromCodeAsync

  • the Credentials.CreateFromCodeAsync method adds a "ForgeCredentials" property to the session

  • the api/forge/callback/oauth route handler then redirects you back to your application's main page

  • the main page makes a request to the api/forge/oauth/token endpoint to get an access token from the session

In your case the "ForgeCredentials" property is not available for some reason. It could be that the code that's supposed to define the session property fails. Try stepping through the code of the api/forge/callback/oauth route handler, and see if the following line of code is executed successfully:

HttpContext.Current.Session.Add("ForgeCredentials", JsonConvert.SerializeObject(credentials));
Petr Broz
  • 8,891
  • 2
  • 15
  • 24