C#学习教程:用Facebook登录C#分享


用Facebook登录C#

当我尝试使用我的网站上的facebook登录时,我有一个无限循环的刷新,
我已经在身体标签上声明了一个脚本

 window.fbAsyncInit = function () { FB.init({ appId: '337323336385***', // App ID status: true, // check login status cookie: true, // enable cookies to allow the server to access the session xfbml: true // parse XFBML }); // Additional initialization code here 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; // TODO: Handle the access token //alert("check"); // 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", '/FacebookLogin.ashx'); 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 //alert("Please "); } else { // the user isn't logged in to Facebook. //alert("jj"); alert("Please Sign into your account to access the site"); } }); }; // 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));  

当你看到这个函数时,它调用的是一个C#函数,它位于FacebookLogin.ashx的文件中,这个函数负责创建一个包含用户数据的Session变量

  public void ProcessRequest(HttpContext context) { var accessToken = context.Request["accessToken"]; context.Session["AccessToken"] = accessToken; HttpCookie cookie = new HttpCookie("FB"); string accessToken2 = context.Session["AccessToken"].ToString(); Facebook.FacebookClient client = new Facebook.FacebookClient(accessToken2); dynamic result = client.Get("me", new { fields = "name,id,link,gender" }); cookie["FBID"] = result.id; context.Response.Cookies.Add(cookie); context.Response.Redirect("/login.aspx"); } 

在页面加载之后,我检查此会话变量是否存在

  string accessToken; FacebookClient client; dynamic result; if (Session["AccessToken"] != null) { accessToken = Session["AccessToken"].ToString(); client = new FacebookClient(accessToken); result = client.Get("me", new { fields = "name,id,link,gender" }); if (gb.CheckExistanceByFBID(result.id)) { string FBID = result.id; var userDetails = context.Users.Where(x => x.FBID == FBID).Select(x => x).First(); HttpCookie cookie = new HttpCookie("userData", userDetails.UserName); cookie.Expires = DateTime.Now.AddMonths(2); cookie["UserName"] = userDetails.UserName; cookie["UserID"] = userDetails.UserID.ToString(); cookie["Password"] = userDetails.Password; cookie["isAdmin"] = userDetails.Admin.ToString(); cookie["Name"] = userDetails.DisplayName; cookie["FBID"] = userDetails.FBID; Response.Cookies.Add(cookie); System.Web.Security.FormsAuthentication.SetAuthCookie(userDetails.UserName, true); System.Web.Security.FormsAuthentication.Timeout.Add(new TimeSpan(40, 0, 0, 0)); Response.Redirect("/Default.aspx"); } else { //var accessToken = Session["AccessToken"].ToString(); //var client = new FacebookClient(accessToken); //dynamic result = client.Get("me", new { fields = "name,id,link,gender" }); accessToken = Session["AccessToken"].ToString(); client = new FacebookClient(accessToken); result = client.Get("me", new { fields = "name,id,link,gender" }); FBRegisterPanel.Visible = false; MainRegisterPanel.Visible = true; txtUserName.Text = result.name; } } 

点击facebook登录按钮后,我有一个无限循环的刷新问题!

我有同样的问题。

当login.aspx加载时,执行JS,将表单发布到FacebookLogin.ashx,重定向到login.aspx,这意味着JS再次运行并且循环继续。

(希望)通过执行以下操作,如果存储了OAuth令牌,则if语句将变为false,因此永远不会执行将表单提交到FacebookLogin.ashx的脚本。

 FB.Event.subscribe('auth.authResponseChange', function (response) { if (response.status === 'connected' && "<%= Session["AccessToken"].ToString() %>" == "") { ... } 

注意:即使没有要存储的访问令牌,也要确保Session [“AccessToken”]包含空字符串,因为:

您可以通过在Page_Init上执行此操作来执行此操作:

上述就是C#学习教程:用Facebook登录C#分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 if (Session["AccessToken"] == null) { Session["AccessToken"] = string.Empty; } 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1031555.html

发表评论

邮箱地址不会被公开。 必填项已用*标注