Pregunta

I am using the MVC5 template with the VisualStudio 2013 preview, it has the nice Startup.Auth.cs configurations, which work across all social domains that I try. FaceBook however requires that you specify the return host. Fine. So I have one Facebook App for localhost and one facebook app for deployed app. I'd like the app to know where it is deployed and pass the approriate keys, but am having problems within the Startup.Auth.cs location. Is there a better place to do this?

 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");
        }

It seems like this always resolves to the second option. As though when /AppStart/Startup.Auth.cs is resolved it is not aware of when it IsLocal or not.

¿Fue útil?

Solución

The current request is definitely not where you want to be looking. A request may be a local request on any server. In any case at application startup there probably isn't any request to look at at all.

You want the application to behave differently depending on where it is deployed. You know where it is deployed when you deploy it, this is probably the best time to make the decision on what time of Facebook authentication you need.

The ConfigureAuth method is part of the Startup class that Owin use to initialize your application.

You can have different Startup classes and you can configure in web.config which one Owin should use. On the server you deploy to you could have something like this:

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

On the local machine you could use this.

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

The ProductionStartup class has the Facebook code needed for the deployed scenario, the Startup class is for your testing.

You can read more about OWIN Startup here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top