2016-04-06 22 views
0

今天我試圖在我的項目上實現外部Facebook登錄。 但是,當我嘗試登錄調用ExternalLogin的方法,這個方法不返回給Facebook登錄頁面C# - Owin Oauth不重定向到外部登錄頁面

我試着這樣做: https://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/

當我看到我的背景下,AuthenticationResponseChallenge爲空。

按照代碼:

Startup.Auth.cs

 app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     var fao = new FacebookAuthenticationOptions 
     { 
      AppId = APP_ID_FACEBOOK, //on Web.config 
      AppSecret = APP_SECRET_FACEBOOK //on Web.config 
     }; 

     fao.Scope.Add("email"); 
     fao.Scope.Add("basic_info"); 

     fao.Provider = new FacebookAuthenticationProvider() 
     { 
      OnAuthenticated = (context) => 
      { 
       context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook")); 
       foreach (var x in context.User) 
       { 
        var claimType = string.Format("urn:facebook:{0}", x.Key); 
        string claimValue = x.Value.ToString(); 
        if (!context.Identity.HasClaim(claimType, claimValue)) 
         context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook")); 

       } 
       return Task.FromResult(0); 
      } 
     }; 

     fao.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie; 
     app.UseFacebookAuthentication(fao); 

AccountController.cs

public ActionResult ExternalLogin(string provider, string returnUrl) 
    { 

     var result = new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Conta", new { ReturnUrl = returnUrl })); 
     return result; 
    } 


    private const string XsrfKey = "XsrfId"; 

    internal class ChallengeResult : HttpUnauthorizedResult 
    { 
     public ChallengeResult(string provider, string redirectUri) 
      : this(provider, redirectUri, null) 
     { 
     } 

     public ChallengeResult(string provider, string redirectUri, string userId) 
     { 
      LoginProvider = provider; 
      RedirectUri = redirectUri; 
      UserId = userId; 
     } 

     public string LoginProvider { get; set; } 
     public string RedirectUri { get; set; } 
     public string UserId { get; set; } 

     public override void ExecuteResult(ControllerContext context) 
     { 
      var properties = new AuthenticationProperties { RedirectUri = RedirectUri }; 
      if (UserId != null) 
      { 
       properties.Dictionary[XsrfKey] = UserId; 
      } 

      context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider); 
     } 
    } 

我試圖用我的Facebook應用程序憑據創建一個空的項目和工作正常,但在這個項目中這不起作用

任何人都可以幫助我嗎?

+0

你的意思是什麼不起作用?你能解釋一下嗎? –

+0

挑戰者不創建URL登錄Facebook和ExternalLogin的響應302和空 – aarcangelo

回答

0

一天後,我找到了解決辦法

我的webconfig與

<authentication mode="Forms"> 
    <forms loginUrl="~/Login" name=".ASPXFORMSAUTH" /> 
</authentication> 

當我改

<authentication mode="None" /> 

的challeger開始重定向到Facebook登錄提交表單後。