gpt4 book ai didi

facebook - 尝试使用 Facebook JS SDK 从 Facebook 注销时,getLoginStatus 返回未知状态

转载 作者:行者123 更新时间:2023-11-30 05:21:39 29 4
gpt4 key购买 nike

我有一个本地主机网站,我在其中使用 Facebook C# SDK 通过 Facebook 实现了登录。

启动配置类:

    public class ExternalLoginConfig
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
AppId = ConfigSettings.FacebookAppId,
AppSecret = ConfigSettings.FacebookAppSecret,
Scope = { "email" },
Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = context =>
{
var accessToken = context.AccessToken;
var facebookClient = new FacebookClient(accessToken);

var result = facebookClient.Get("me", new { fields = "email,first_name,last_name" }) as JsonObject;

string email = null;
string firstName = null;
string lastName = null;

if (result != null)
{
email = result.ContainsKey("email") ? (string) result["email"] : null;
firstName = result.ContainsKey("first_name") ? (string) result["first_name"] : null;
lastName = result.ContainsKey("last_name") ? (string) result["last_name"] : null;
}

if (firstName != null)
{
context.Identity.AddClaim(new Claim(ClaimTypes.GivenName, firstName));
}
if (lastName != null)
{
context.Identity.AddClaim(new Claim(ClaimTypes.Surname, lastName));
}
if (email != null)
{
context.Identity.AddClaim(new Claim(ClaimTypes.Email, email));
}

return Task.FromResult(0);
},
OnApplyRedirect = context =>
{
context.Response.Redirect(context.RedirectUri + "&auth_type=reauthenticate");
}
}
};
app.UseFacebookAuthentication(facebookAuthenticationOptions);
}
}

Actions form Authentication controller:

    [HttpPost]
[AllowAnonymous]
public virtual ActionResult Login(string provider, string returnUrl)
{
ControllerContext.HttpContext.Session.RemoveAll();

return new ExternalLoginResult(provider,
Url.Action("LoginCallback", "Oauth", new { ReturnUrl = returnUrl }));
}

[AllowAnonymous]
public async Task<ActionResult> LoginCallback(string returnUrl, string error)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

if (loginInfo == null)
{
return Redirect(returnUrl);
}

User user = null;
string userName = Guid.NewGuid().ToString();
string firstName = loginInfo.ExternalIdentity.FindFirstValue(ClaimTypes.GivenName);
string lastName = loginInfo.ExternalIdentity.FindFirstValue(ClaimTypes.Surname);
string email = loginInfo.ExternalIdentity.FindFirstValue(ClaimTypes.Email);
string externalProviderName = loginInfo.Login.LoginProvider;
string externalProviderKey = loginInfo.Login.ProviderKey;

var externalAuthenticationInfo = new ExternalAuthenticationInfo()
{
Username = userName,
Email = email,
FirstName = firstName,
LastName = lastName,
ExternalProviderName = externalProviderName,
ExternalProviderKey = externalProviderKey
};

var loginResult = userProvider.ExternalLogin(externalProviderKey, email, out user);

switch (loginResult)
{
case LoginResult.Success:
{
AuthenticationHelper.SetAuthenticatedUserId(user.ID);
break;
}
case LoginResult.NotRegistered:
{
var registerResult = userProvider.Register(userName, email, null, externalAuthenticationInfo);

if (registerResult.IsValid)
{
AuthenticationHelper.SetAuthenticatedUserId(registerResult.Result.ID);
}

break;
}
}

return Redirect(returnUrl);
}

Facebook JS SDK 初始化:

    window.fbAsyncInit = function () {
FB.init({
appId: '@ConfigSettings.FacebookAppId',
xfbml: true,
version: 'v2.4'
});
};

(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

我正在尝试使用 Facebook JS SDK 将用户从 facebook 中注销,但是调用:

    FB.getLoginStatus(function facebookLogoutCallback(facebookResponse) {
if (facebookResponse.status !== 'connected') {
return;
}

FB.logout(facebookLogoutCallback);
});

导致状态 unknown 而不是 connected,它在 facebookResponse 对象中返回。我也试过在没有 if 语句的情况下调用 FB.logout(),但没有成功。

也许你可以说,这种行为是由未经授权的用户状态引起的,但在服务器端登录后,用户实际上已经登录了:在我的网站和 Facebook 上也是如此。

最佳答案

目前 FB.logout 函数中似乎存在错误。调用它后,用户无法使用 JS SDK 再次登录此应用程序,因为 FB.login 函数返回
对象 { status="unknown", authResponse=null}

编辑:
发现在 FB.logout() 之后创建了一个名为“fblo_*”的 cookie,这似乎是造成这种情况的原因。我不能确切地说出它存在的原因以及它的作用,但删除它会使登录重新生效。

因此我创建了一个小脚本来查找这个 cookie 并在我调用 FB.login() 之前删除它,您可能想在点击事件 ( https://developers.facebook.com/docs/reference/javascript/FB.login/v2.5 ) 中调用它。

function delete_cookie(name) 
{
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
}


var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
if(cookies[i].split("=")[0].indexOf("fblo_") != -1)
delete_cookie(cookies[i].split("=")[0]);
}

关于facebook - 尝试使用 Facebook JS SDK 从 Facebook 注销时,getLoginStatus 返回未知状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34725123/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com