2

我在 Session [“accesstoken”] 中设置从 facebook 检索到的 accesstoken。当我尝试检索它时,我被 nullreferenceexception 击中。

为什么?

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

在调试模式下,我从 result.ExtraData["accesstoken"] 复制了值,并通过 formscollection 将其添加到代码中(如您在 Facebook.cs 中所见),并且令牌有效。我应该改用 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 功能:

私人无效 AddAdditionalPermissions() { 字符串 facebook_urlAuthorize_base = " https://graph.facebook.com/oauth/authorize "; 字符串 facebook_urlGetToken_base = " https://graph.facebook.com/oauth/access_token "; 字符串 facebook_AppID = "1secretappIdhere242341"; 字符串 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);             
    }

脸书.cs

公共无效 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);
    }
4

1 回答 1

1

我认为您的会话令牌 cookie 丢失了。尝试不终止当前页面响应,通过替换

Response.Redirect(urlAuthorize, true);       

Response.Redirect(urlAuthorize, false);     

看看它是否有帮助。

于 2013-05-25T21:09:18.803 回答