2013-09-22 36 views
2

我使用VisualStudio 2013預覽的MVC5模板,它有很好的Startup.Auth.cs配置,它可以跨越我嘗試的所有社交域。然而FaceBook要求你指定返回主機。精細。所以我有一個用於本地主機的Facebook應用和一個用於部署應用的Facebook應用。我希望應用程序知道它的部署位置並傳遞適當的密鑰,但在Startup.Auth.cs位置中遇到問題。有沒有更好的地方可以做到這一點?使用FaceBook OAuth mvc5與不同的LocalHost和部署密鑰

public void ConfigureAuth(IAppBuilder app) 
    { 
     // Enable the application to use a cookie to store information for the signed in user 
     // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
     app.UseSignInCookies(); 

     // Uncomment the following lines to enable logging in with third party login providers 
     //app.UseMicrosoftAccountAuthentication(
     // clientId: "", 
     // clientSecret: ""); 

     if (HttpContext.Current.Request.IsLocal) 
     { 
      app.UseFacebookAuthentication(
       appId: "1234localid", 
       appSecret: "123123123123123"); 
     } 
     else 
     { 
      app.UseFacebookAuthentication(
       appId: "4321deployid", 
       appSecret: "123123123123123"); 
     } 

這似乎總是解決第二個選項。就好像/AppStart/Startup.Auth.cs已解析,它不知道它何時IsLocal。

回答

2

當前請求絕對不是您想要查找的位置。請求可能是任何服務器上的本地請求。無論如何,在應用程序啓動時,可能根本沒有任何請求。

您希望應用程序的行爲有所不同,具體取決於應用程序的部署位置。您知道部署它時部署的位置,這可能是決定您需要什麼時間進行Facebook身份驗證的最佳時機。

ConfigureAuth方法是Owin用來初始化應用程序的Startup類的一部分。

你可以有不同的啓動類,你可以在web.config中配置Owin應該使用哪一個。 在部署到你的服務器可能有這樣的事情:

<appSettings> 
    <add key="owin:appStartup" value="YourNamespaceHere.ProductionStartup" /> 
</appSettings> 

在本地計算機上,你可以用這個。

<appSettings> 
    <add key="owin:appStartup" value="YourNamespaceHere.Startup" /> 
</appSettings> 

ProductionStartup類具有部署場景所需的Facebook代碼,Startup類用於測試。

您可以閱讀更多關於OWIN Startup here

相關問題