带有Credentials.FromSessionAsync()的Forge API NullReferenceException [英] Forge API NullReferenceException with Credentials.FromSessionAsync()
问题描述
我正在
但是当我单击"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凭据登录后发生的情况:
-
Autodesk登录页面将您重定向到Forge应用程序的回调URL (通常设置为
api/forge/callback/oauth
之类的东西) -
在您的代码中有一个处理程序,用于
api/forge/callback/oauth
路由,该路由调用 -
api/forge/callback/oauth
路由处理程序,然后将您重定向回应用程序的主页 -
主页向
api/forge/oauth/token
端点发出请求,以从会话中获取访问令牌
在您的情况下,"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.CreateFromCodeAsyncthe 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 pagethe 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屋!