带有Credentials.FromSessionAsync()的Forge API NullReferenceException [英] Forge API NullReferenceException with Credentials.FromSessionAsync()

查看:51
本文介绍了带有Credentials.FromSessionAsync()的Forge API NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在

但是当我单击"AUTODESK登录"时会引发错误

看来Credentials对象被返回为NULL.它为获取该对象而调用的代码如下:

 公共静态异步任务<凭据>FromSessionAsync(){如果(HttpContext.Current.Session == null ||HttpContext.Current.Session ["ForgeCredentials"] == null)返回null;凭证凭证= JsonConvert.DeserializeObject< Credentials>(HttpContext.Current.Session ["ForgeCredentials"].ToString());if(credentials.ExpiresAt< DateTime.Now)等待凭据.RefreshAsync();返回凭证;} 

我不确定该代码块中的会话凭据来自何处.

我是Forge API的新手,所以将不胜感激.

谢谢格里

解决方案

以下是按时间顺序使用Autodesk凭据登录后发生的情况:

在您的情况下,"ForgeCredentials"该属性由于某些原因而无法使用.可能是本应用来定义session属性的代码失败了.尝试逐步浏览 api/forge/callback/oauth 路由处理程序的代码,并查看以下代码行是否成功执行:

  HttpContext.Current.Session.Add("ForgeCredentials",JsonConvert.SerializeObject(credentials)); 

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

But when I click AUTODESK Sign In it throws an error

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

解决方案

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));

这篇关于带有Credentials.FromSessionAsync()的Forge API NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆