Facebook集成会话问题

时间:2012-07-24 09:37:04

标签: facebook

这是我的UserLogin.aspx页面代码,其中包含Facebook用户的身份验证,问题是我在会话中维护令牌,我后来在Default.aspx上使用它。如果你看到Session [“facebook_token”] = authToken;

这一行

在我的Default.aspx页面上,我已经放置了这段代码,但是当我回到家或默认页面时,它给了我null值。

    if (Session["facebook_token"] != null)
        lblUserName.Text = Session["facebook_token"].ToString();


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialMediaModel;
using Facebook.Components;

public partial class UserLogin : System.Web.UI.Page
{
FacebookService _fbService = new FacebookService();
private const string FACEBOOK_API_KEY = "551810942508804";
private const string FACEBOOK_SECRET = "b63b8cca428b42935e6eab59976367b1";

protected void Page_Load(object sender, EventArgs e)
{
    // ApplicationKey and Secret are acquired when you sign up for
    _fbService.ApplicationKey = FACEBOOK_API_KEY;
    _fbService.Secret = FACEBOOK_SECRET;
    _fbService.IsDesktopApplication = false;

    string sessionKey = Session["facebook_session_key"] as String;
    string userId = Session["facebook_userId"] as String;

    string authToken = Request.QueryString["auth_token"];

    Session["facebook_token"] = authToken;

    // We have already established a session on behalf of this user
    if (!String.IsNullOrEmpty(sessionKey))
    {
        _fbService.SessionKey = sessionKey;
        _fbService.UserId = userId;
    }
    // This will be executed when facebook login redirects to our page
    else if (!String.IsNullOrEmpty(authToken))
    {
        _fbService.CreateSession(authToken);
        Session["facebook_session_key"] = _fbService.SessionKey;
        Session["facebook_userId"] = _fbService.UserId;
        Session["facebook_session_expires"] = _fbService.SessionExpires;
    }
    // Need to login
    else
    {
        Response.Redirect(@"http://www.facebook.com/login.php?api_key=" +   
       _fbService.ApplicationKey + @"&v=1.0");
    }


    }
}

1 个答案:

答案 0 :(得分:0)

  

string authToken = Request.QueryString["auth_token"];

     

Session["facebook_token"] = authToken;

看起来你正在每个页面加载(?)上执行这些行 - 而不检查是否有 是这个名称的GET参数。