2015-11-04 69 views
3

我開發了一個ASP.NET MVC5應用程序。我在我的應用程序中使用了Facebook外部認證。 當我使用「Locallhost」域調試此應用程序時,Facebook登錄工作正常,但是當我在主服務器中發佈應用程序時,AuthenticationManager.GetExternalLoginInfo()返回null,它在URL中給我這樣一個錯誤:使用facebook SDK登錄MVC5應用程序時獲取錯誤(error = access_denied)

http://xxxxx.com/Account/ExternalLoginCallback?ReturnUrl=%2Fen&error=access_denied#_=_ 

我已經設置了「網站URL」爲「http://xxxx.com」和「有效的OAuth重定向的URI」,如Facebook的發展控制檯「http://xxxx.com/signin-facebook」。

我在Startup.Outh.cs文件設置爲:

var FacebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions(); 
     FacebookOptions.AppId = ConfigurationManager.AppSettings["Facebook_User_Key"]; 
     FacebookOptions.AppSecret = ConfigurationManager.AppSettings["Facebook_Secret_Key"]; 
     FacebookOptions.Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider() 
     { 
      OnAuthenticated = async context => 
      { 
       context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); 
       foreach (var claim in context.User) 
       { 
        var claimType = string.Format("urn:facebook:{0}", claim.Key); 
        string claimValue = claim.Value.ToString(); 
        if (!context.Identity.HasClaim(claimType, claimValue)) 
         context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook")); 
       } 

      } 
     }; 
     FacebookOptions.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie; 
     app.UseFacebookAuthentication(FacebookOptions); 

我不知道爲什麼外部登錄不只是在服務器與我的主域名工作。請幫助我解決這個問題。

+0

您的應用處於開發模式?如果是,用戶在您的應用中扮演角色?你是否要求任何尚未批准的權限? – The1Fitz

回答

0

我遇到幾乎相同的症狀你描述:

不久:

Facebook的身份驗證在本地主機上運行良好,該項目上傳到另一臺服務器(和Facebook改變了網站的網址後控制檯),認證沒有成功。

我建議你回滾到MVC模板代碼,如果可以的話 - 注意你對中間件代碼(Startup.Auth.sc)所做的任何更改。 特別注意與本地配置交互的代碼,例如本地服務的磁盤I/O和操作系統權限。

我的具體情況:

從Owin /武士刀開始支持的WebAPI項目的Visual Studio的模板,外部登錄是與Facebook,微軟和谷歌的OAuth中間件完美的工作,在本地主機上測試時。

後來我將代碼添加到Startup.Auth.sc,因爲我需要進一步的身份驗證活動。

因此,這是原代碼:

public void ConfigureAuth(IAppBuilder app) 
    { 
     // see WebAPI template of Visual Studio 2013/2015 
     ... 
     app.UseFacebookAuthentication(
      appId: 99999999, 
      appSecret: *******); 
    } 

,這是替換:

public void ConfigureAuth(IAppBuilder app) 
    { 
     // see WebAPI template of Visual Studio 2013/2015 
     ... 
     app.UseFacebookAuthentication(GetFacebookAuth());    
    } 
    private FacebookAuthenticationOptions GetFacebookAuth() 
    { 
     string picRequest = 
      String.Format("/me/picture?redirect=false&width={0}&height={0}", ProfileInfoClaimsModel.PIC_SIDE_PX); 
     var facebookProvider = new FacebookAuthenticationProvider() 
     { 
      OnAuthenticated = async (context) => 
      { 
       var client = new FacebookClient(context.AccessToken); 
       dynamic me = client.Get("/me?fields=id,name,locale"); 
       dynamic mePicture = client.Get(picRequest); 

       // storing temporary social profile info TO A LOCAL FOLDER 
       // uploading the local folder to a service WITH A LOCAL CREDENTIAL FILE 
       ... 
      } 

     }; 
     var options = new FacebookAuthenticationOptions() 
     { 
      AppId =, 
      AppSecret = ******, 
      Provider = facebookProvider, 
     }; 
     return options; 
    } 

您可能注意到,我的意見會使問題明顯 - 代碼指向本地資源。

然後,我將該項目發佈到運行帶有IIS 8.5的Windows Server 2012的虛擬服務器(通過Amazon EC2)。 從那一刻起,我一直在從/ signin-facebook重定向error=access_denied

我決定遵循this好老觀念,並回到原來的模板代碼。很快我發現我忘了配置新的服務器。例如,代碼引用的文件夾不存在,並且該網站沒有創建它的權限。

顯然,這解決了它。

相關問題