无法登录Facebook

时间:2012-03-27 18:30:15

标签: facebook-c#-sdk

刚刚开始使用facebook c#sdk的v6构建应用程序,并坚持使用facebook的登录过程。我的页面中有以下代码:

    string client_id = ConfigurationManager.AppSettings["APIKey"].ToString();
    string client_secret = ConfigurationManager.AppSettings["Secret"].ToString();
    string redirect_uri = ConfigurationManager.AppSettings["Url"].ToString() + "default2.aspx";

    FacebookClient objClient = new FacebookClient();

    var fbLoginUrl = objClient.GetLoginUrl(new { client_id = client_id, 
                                                 client_secret = client_secret, 
                                                 redirect_uri = redirect_uri, 
                                                 response_type = "code",
                                                 display = "popup",
                                                 scope = "manage_pages,publish_stream,read_stream", 
                                                 state = "" });
    //msg.Text = fbLoginUrl.ToString();
    Response.Redirect(fbLoginUrl.ToString());

当我转到该页面时,重定向登录永远不会发生。我做错了什么?

1 个答案:

答案 0 :(得分:1)

使用MVC 3(查看已发布的示例),我将创建登录开始的视图。控制器是:

        public ActionResult Logon()
    {
        _fb = new FacebookClient();

        var csrfToken = Guid.NewGuid().ToString();
        Session["fb_csrf_token"] = csrfToken;

        var state = Convert.ToBase64String(Encoding.UTF8.GetBytes(_fb.SerializeJson(new { returnUrl = returnUrl, csrf = csrfToken })));

        var fbLoginUrl = _fb.GetLoginUrl(
            new
            {
                client_id = AppId,
                client_secret = Appsecret,
                redirect_uri = RedirectUri,
                response_type = "code",
                scope = Scope,
                state = state
            });
        return Redirect(fbLoginUrl.AbsoluteUri);
    }

其中returnUrl是我的应用程序(/ home / fbhome)中的连接视图区域,RedirectUri是视图(loginresult),它将执行第二步:

        public ActionResult Loginresult(string code, string state)
    {
        if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(state))
            return RedirectToAction("Index", "Home");

        // first validate the csrf token
        _fb = new FacebookClient();
        dynamic decodedState;
        try
        {
            decodedState = _fb.DeserializeJson(Encoding.UTF8.GetString(Convert.FromBase64String(state)), null);
            var exepectedCsrfToken = Session["fb_csrf_token"] as string;
            // make the fb_csrf_token invalid
            Session["fb_csrf_token"] = null;

            if (!(decodedState is IDictionary<string, object>) || !decodedState.ContainsKey("csrf") || string.IsNullOrWhiteSpace(exepectedCsrfToken) || exepectedCsrfToken != decodedState.csrf)
            {
                return RedirectToAction("Index", "Home");
            }
        }
        catch
        {
            // log exception
            return RedirectToAction("Index", "Home");
        }

        try
        {
            dynamic result = _fb.Post("oauth/access_token",
                                      new
                                      {
                                          client_id = AppId,
                                          client_secret = Appsecret,
                                          redirect_uri = RedirectUri,
                                          code = code
                                      });

            Session["fb_access_token"] = result.access_token;

            if (result.ContainsKey("expires"))
                Session["fb_expires_in"] = DateTime.Now.AddSeconds(result.expires);

            if (decodedState.ContainsKey("returnUrl"))
            {
                if (Url.IsLocalUrl(decodedState.returnUrl))
                    return Redirect(decodedState.returnUrl);
                return Redirect(decodedState.returnUrl + "/notlocal");
            }

            return RedirectToAction("Index", "Home");
        }
        catch
        {
            // log exception
            return RedirectToAction("Index", "Home");
        }
    }

所以,据我所知,用户进入网站(/ home / index)然后点击链接连接(/ home / loggon),facebook回答(/ home / loginresult),用户被重定向到/ home / fbhome

希望这有帮助

抱歉我的英文!