Facebook在弹出窗口中重新进行身份验证

时间:2013-05-19 14:19:43

标签: c# facebook-graph-api authentication

我写了一个Windows窗体应用程序,它连接到Facebook的用户个人资料,在facebook c#sdk的帮助下读取他的墙并在窗口小部件中显示他的更新。

一切正常,但facebook不会让我的用户更改其凭据,这意味着重新登录为其他用户。

我的身份验证流程示意如下:

在登录流程初始化程序中:

  

注意:对话框显示在弹出窗口中。如果您完整显示对话框   浏览器,可以注销,但弹出这个功能   似乎缺乏。

private void loginButton_Click(object sender, EventArgs e){     
    browser = new WebBrowser();
    this.browser.Navigated += new WebBrowserNavigatedEventHandler(browser_Navigated);
    //+ all types of gui settings for the borwser
    form.Controls.Add(browser);

    Uri url=new Uri("https://www.facebook.com/dialog/oauth?client_id=" + {*MyAppsId*}+
      "&redirect_uri=" + "https://www.facebook.com/connect/login_success.html" +
      "&response_type=token"+
      "&auth_type=reauthenticate"+
      "&display=popup"+
      "&scope=read_stream"

    browser.Navigate(url);// +try/catches - omitted
}

导航响应解析:

注意:只使用c#sdk类解析响应FacebookOAuthResult和FacebookClient

private void browser_Navigated(object sender, WebBrowserNavigatedEventArgs e){
    FacebookOAuthResult oauthResult; //helps to streamline parsing
    if (fb.TryParseOAuthCallbackUrl(e.Url, out oauthResult)){ //type of fb is FacebookClient of c# sdk
        if (oauthResult.IsSuccess){
            accessToken = oauthResult.AccessToken;
            //from here start reaing user's feed with the help of access token
        }
    }
}

我的意思是,当点击登录按钮时,系统会提示用户输入 用户名 和密码。

但是,系统会提示用户仅重新输入密码,而不是重新输入用户名。

如果我使用不带"&auth_type=reauthenticate"参数的网址,则不会提示任何内容,直接向我发送访问令牌。

如何强制Facebook要求用户名?

1 个答案:

答案 0 :(得分:0)

经过几天的战斗,答案就在我的鼻子底下。

您只需使用访问令牌致电https://www.facebook.com/logout.php

        WebBrowser browser = new WebBrowser();
        browser.Navigated += browser_NavigatedLogout;

        string logoutUrl = "https://www.facebook.com/logout.php?" +
            "next=https://www.facebook.com/connect/login_success.html" +
            "&access_token=" + MyAccessToken;


        try
        {
            browser.Navigate(new Uri(logoutUrl));
        }
        catch (System.Exception other)
        {
            MessageBox.Show("error logging out: "+other.Message);
        }

并且处理程序是:

    private void browser_NavigatedLogout(object sender, WebBrowserNavigatedEventArgs e)
    {
        if (e.Url.AbsoluteUri == "https://www.facebook.com/connect/login_success.html")
        {
           //here anything you need to do after the user logged off
        }
    }
相关问题