gpt4 book ai didi

c# - HttpContext.Current.Session ["accesstoken".ToString() 为 null 应该设置值时

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:50 25 4
gpt4 key购买 nike

我在 Session["accesstoken"] 中设置从 facebook 检索的 accesstoken。当我尝试检索它时,我遇到了 nullreferenceexception。

为什么?

在我开始使用 AddAdditionalPermissions 方法之前它确实有效,但我看不出这会如何干扰 session ...?

在 Debug模式下,我从 result.ExtraData["accesstoken"] 复制了值,并通过 formscollection(如您在 Facebook.cs 中看到的那样)将其添加到代码中,并且 token 有效。我应该改用 cookie 吗?

AccountController.cs

    public ActionResult ExternalLoginCallback(string returnUrl)
{
AuthenticationResult result =
OAuthWebSecurity.VerifyAuthentication(
Url.Action("ExternalLoginCallback",
new { ReturnUrl = returnUrl }));
if (!result.IsSuccessful)
{
return RedirectToAction("ExternalLoginFailure");
}

// Save the accesstoken into session
Session["accesstoken"] = result.ExtraData["accesstoken"];
Session["id"] = result.ExtraData["id"];

if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{
AddAdditionalPermissions();
return RedirectToLocal(returnUrl);
}(...etcetera)

AdditionalPermissions 函数:

private void AddAdditionalPermissions() { string facebook_urlAuthorize_base = "https://graph.facebook.com/oauth/authorize"; string facebook_urlGetToken_base = "https://graph.facebook.com/oauth/access_token"; string facebook_AppID = "1secretappIdhere242341"; string facebook_AppSecret = "1secretcodeheteq4426";

        string scope = "publish_stream"; 
string urlAuthorize = facebook_urlAuthorize_base;
urlAuthorize += "?client_id=" + facebook_AppID;
urlAuthorize += "&redirect_uri=" + "http://localhost:2730/";
urlAuthorize += "&scope=" + scope;

Response.Redirect(urlAuthorize, true);
}

Facebook.cs

public void PostToFacebook(FormCollection fm) { var fb = new FacebookClient();

        fb = new FacebookClient(HttpContext.Current.Session["accesstoken"].ToString());
var args = new Dictionary<string, object>();
args["message"] = "abc";

fb.Post("/me/feed", args);
}

最佳答案

我认为您的 session token cookie 丢失了。尝试不通过替换

来终止当前页面响应
Response.Redirect(urlAuthorize, true);       

Response.Redirect(urlAuthorize, false);     

看看有没有帮助。

关于c# - HttpContext.Current.Session ["accesstoken".ToString() 为 null 应该设置值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16753717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com