我有一个localhost网站,我使用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); } }
动作形式认证控制器:
[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 TaskLoginCallback(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
对象中返回.我也试图在FB.logout()
没有if
声明的情况下打电话,但它没有用.
也许你可以说,这种行为是由未经授权的用户状态引起的,但是在服务器端登录之后,用户实际上已登录:在我的网站和Facebook上也是如此.
似乎FB.logout函数中当前存在错误.在调用它之后,用户无法使用JS SDK再次登录此应用程序,因为FB.login函数返回
Object {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]); }