6

FacebookのC#SDKを使用してFacebook経由でログインを実装しているlocalhostウェブサイトがあります。getLoginStatusは、Facebook JS 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 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); 
}); 

facebookResponseオブジェクトで返される状態unknown代わりのconnected、につながります。私もFB.logout()ifのステートメントなしで呼び出そうとしましたが、動作しませんでした。

おそらく、この動作は権限のないユーザーステータスによって引き起こされますが、サーバー側のログイン後にユーザーが実際にログインしていると言うこともできます。

+0

実際の問題は何ですか? – manonthemoon

答えて

8

現在、FB.logout機能にバグがあります。あることを発見
:FB.login機能が
オブジェクト{ステータスは=「不明」、authResponse = NULL}

EDITを返すためJS SDKを使用して、ユーザーが再びこのアプリケーションにログインすることができない、それを呼び出した後、これの理由と思われるFB.logout()の後に作成された "fblo_ *"というクッキー。なぜそこにあるのか、それが何であるのか正確には言えませんが、それを削除するとログインが再開します。

私はこのクッキーを探して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]); 
} 
+0

あなたが分割したいくつかの 'fblo_'クッキーに先行する空白がある小さなバグがあります。私はその空白を削除するためにjQuery '$ .trim'を使います。 – sjagr

+0

私はこの同意にも到着しました。同じ問題を抱えている人がいることを知ってうれしいです。何が起こっているのかわからない... – jperelli

関連する問題