简单与Facebook C#SDK不灵登录 [英] Simple log in with Facebook C# SDK not working

查看:233
本文介绍了简单与Facebook C#SDK不灵登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来Facebook的应用程序和最近读的可用文档。然后,我继续获得的NuGet Facebook的C#SDK和阅读上手教程

我修改了一点。我有3页:


  • 的Default.aspx:有与Facebook登录按钮

  • Protected.aspx:我最终想成为无首测不可访问的页面

  • DoSignIn.aspx:包含令牌的请求,并重定向回Default.aspx的


  • 在Default.aspx的,我有一个按钮,我重定向到Protected.aspx页面。就可以了,我有我展示令牌的标签。


现在,我的问题是这样的:


  • 当我点击与Facebook登录按钮,该按钮并没有改变,表明我登陆,那只能说明我登录,如果我做在页面上刷新。

  • 如果我点击按钮我重定向到Protected.aspx页,我不能看到标签上的标记,因为DoSignIn.aspx页从未被调用。

如果有人能指出我什么,我做错了(我几乎复制了教程的例子),我将非常感激。我花了近3小时,这一摆弄和搜索谷歌和SO遍,不知道去哪里了搜索。

感谢您的时间。

PS。我有最新的Facebook C#SDK和App ID似乎做工精细,当我手动刷新我的浏览器,我的确登录。

我添加了code为3页:

Default.aspx的:

 <%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
< /头>
<身体GT;
    < D​​IV ID =FB-根>
    < / DIV>
    <脚本类型=文/ JavaScript的>
        window.fbAsyncInit =功能(){
            FB.init({
                APPID:I_INSERTED_MY_APP_ID_HERE',//应用程序ID
                状态:真正的,//检查登录状态
                饼干:真,//启用cookies来允许服务器访问会话
                XFBML://真正解析XFBML
            });
            FB.Event.subscribe('auth.authResponseChange',函数(响应){
                如果(response.status ==='连接'){
                    //用户登录并已经验证您的
                    //应用程序,response.authResponse用品
                    //用户的ID,一个有效的访问令牌,一个签名
                    //请求,以及时的访问令牌
                    //和签名的请求每个到期
                    VAR UID = response.authResponse.userID;
                    VAR的accessToken = response.authResponse.accessToken;                    // - 处理访问令牌 -
                    //完成后的服务器完成登录
                    //这是一个形式,因为后,我们不希望使用AJAX
                    VAR形式=使用document.createElement(表);
                    form.setAttribute(法,后);
                    form.setAttribute(行动,DoSignIn.aspx');                    VAR字段=使用document.createElement(输入);
                    field.setAttribute(类型,隐藏);
                    field.setAttribute(姓名,的accessToken');
                    field.setAttribute(价值的accessToken);
                    form.appendChild(场);                    document.body.appendChild(表);
                    form.submit();                }否则如果(response.status ==='NOT_AUTHORIZED'){
                    //用户登录到Facebook,
                    //但一直没有验证您的应用程序
                }其他{
                    //用户没有登录Facebook。
                }
            });
        };        //异步加载SDK
        (函数(D){
            VAR JS,ID ='Facebook的jssdk',REF = d.getElementsByTagName('脚本')[0];
            如果(d.getElementById(ID)){返回; }
            JS = d.createElement('脚本'); js.id = ID; js.async = TRUE;
            js.src =//connect.facebook.net/en_US/all.js;
            ref.parentNode.insertBefore(JS,REF);
        }(文件));
    < / SCRIPT>
    <表ID =form1的=服务器>
    < D​​IV>
        &所述;股利类=FB-登录按钮数据显示-面=真的数据宽度=400数据最大值-行=1>
            通过Facebook登陆< / DIV>
        < BR />
        < ASP:按钮的ID =Button1的=服务器的OnClick =的button1_Click文本=去保护/>
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

Default.aspx.cs:

 公共部分类_Default:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
    保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        的Response.Redirect(Protected.aspx);
    }
}

DoSignIn.aspx.cs:

 公共部分类DoSignIn:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        VAR的accessToken =请求[的accessToken];
        会议[的accessToken] =的accessToken;        的Response.Redirect(Default.aspx的);
    }
}

Protected.aspx.cs:

 公共部分类保护:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(会话[的accessToken]!= NULL)
        {
            Label1.Text =会话[的accessToken]的ToString()。
        }
        其他
        {            Label1.Text =未验证;
        }
    }


解决方案

好吧,我已经花了几个小时寻找,终于找到为什么它不工作。它无关的code以上,但与Facebook的错误。实际上,似乎很多人不能有auth.authResponseChange启用作为会话刷新。我发现FB了一些错误报告描述了完整的问题,其中之一是这里

此问题开发/本地主机/当我有。我已经搬到了一个不同的方式来整合Facebook登录时,删除上述样品之前,我尝试下一个域名上传一个实际的托管服务器上。对于原因,我无法解释,它只是从那里工作。

总之,对于Facebook登录我目前追求的实现是解释的>。

I'm new to the Facebook apps and recently read the doc available. Then, I proceeded to get the Facebook C# SDK from NuGet and read the get started tutorial.

I modified it a little. I have 3 pages:

  • Default.aspx : has the "log in with facebook" button
  • Protected.aspx : the page that I ultimately want to be unaccessible without first logging in
  • DoSignIn.aspx : contains the request for the token and redirects back to Default.aspx

  • On the Default.aspx, I have a button that redirects me to the Protected.aspx page. On it, I have a label showing me the token.

Now, my issue is this:

  • When I click on the "log in with facebook" button, the button doesn't change to show that I'm logged in. It only shows that I'm logged in if I do a refresh on the page.
  • If I click on the button to redirect me to the Protected.aspx page, I can't see the token in the Label because the DoSignIn.aspx page was never called.

If someone could point me in what I do wrong (as I nearly copied the tutorial example), I would be very grateful. I spent the past 3 hours fiddling with this and searching Google and SO all over and don't know where to search anymore.

Thank you for your time.

PS. I have the latest Facebook C# SDK and the App ID seems to work fine as when I manually refresh my browser, I am indeed logged in.

I add the code for the 3 pages:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div id="fb-root">
    </div>
    <script type="text/javascript">
        window.fbAsyncInit = function () {
            FB.init({
                appId: 'I_INSERTED_MY_APP_ID_HERE', // App ID
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true  // parse XFBML
            });


            FB.Event.subscribe('auth.authResponseChange', function (response) {
                if (response.status === 'connected') {
                    // the user is logged in and has authenticated your
                    // app, and response.authResponse supplies
                    // the user's ID, a valid access token, a signed
                    // request, and the time the access token 
                    // and signed request each expire
                    var uid = response.authResponse.userID;
                    var accessToken = response.authResponse.accessToken;

                    // - Handle the access token -
                    // Do a post to the server to finish the logon
                    // This is a form post since we don't want to use AJAX
                    var form = document.createElement("form");
                    form.setAttribute("method", 'post');
                    form.setAttribute("action", 'DoSignIn.aspx');

                    var field = document.createElement("input");
                    field.setAttribute("type", "hidden");
                    field.setAttribute("name", 'accessToken');
                    field.setAttribute("value", accessToken);
                    form.appendChild(field);

                    document.body.appendChild(form);
                    form.submit();

                } else if (response.status === 'not_authorized') {
                    // the user is logged in to Facebook, 
                    // but has not authenticated your app
                } else {
                    // the user isn't logged in to Facebook.
                }
            });
        };

        // Load the SDK Asynchronously
        (function (d) {
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        } (document));
    </script>
    <form id="form1" runat="server">
    <div>
        <div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1">
            Log in with Facebook</div>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go to protected" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Protected.aspx");
    }
}

DoSignIn.aspx.cs:

public partial class DoSignIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var accessToken = Request["accessToken"];
        Session["AccessToken"] = accessToken;

        Response.Redirect("Default.aspx");
    }
}

Protected.aspx.cs:

public partial class Protected : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AccessToken"] != null)
        {
            Label1.Text = Session["AccessToken"].ToString();
        }
        else
        {

            Label1.Text = "Not authenticated";
        }
    }

解决方案

Right, I have spent hours searching and finally found why it is not working. It has nothing to do with the code above but with a Facebook bug. Actually, it seems that many people can't have the auth.authResponseChange to fire as the session refreshes. I found several bugs reports on FB describing the complete issue, one of them being here.

This issue I had when developing on /localhost/. I had already moved to a different way to integrate Facebook Login when, before deleting the above samples, I tried uploading it on an actual hosting server under a domain name. For a reason I can't explain, it just works from there.

Anyway, the implementation for the Facebook login I'm currently pursuing is explained here.

这篇关于简单与Facebook C#SDK不灵登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆