Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set facebook application web.config settings in code (C#)

I'm trying to set the web.config settings in a facebook app from code to avoid working directly with the web.config file. I've tried a custom ConfigurationSection class, and then using the WebConfigurationManager to reach the web.config file. The problem is that I can't get an instance of the Configuration object. This is my code:

public class FacebookConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("appId")]
public string AppID
{
    get { return (string)base["appId"]; }
    set { base["appId"] = value; }
}

[ConfigurationProperty("appSecret")]
public string AppSecret
{
    get { return (string)base["appSecret"]; }
    set { base["appSecret"] = value; }
}

[ConfigurationProperty("canvasPage")]
public string CanvasPage
{
    get { return (string)base["canvasPage"]; }
    set { base["canvasPage"] = value; }
}

[ConfigurationProperty("canvasUrl")]
public string CanvasUrl
{
    get { return (string)base["canvasUrl"]; }
    set { base["canvasUrl"] = value; }
}

[ConfigurationProperty("cancelUrlPath")]
public string CancelUrlPath
{
    get { return (string)base["cancelUrlPath"]; }
    set { base["cancelUrlPath"] = value; }
}

public FacebookConfigurationSection()
{
}

}

And the page that uses this:

protected void Button1_Click(object sender, EventArgs e)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

    FacebookConfigurationSection _config = new FacebookConfigurationSection(); 
    _config = config.GetSection("facebookSettings") as FacebookConfigurationSection;

    //FacebookConfigurationSection config = (FacebookConfigurationSection)System.Configuration.ConfigurationManager.GetSection("facebookSettings");
    if (!string.IsNullOrEmpty(TextBox1.Text))
        _config.AppID = TextBox1.Text.ToString();

    if (!string.IsNullOrEmpty(TextBox2.Text))
        _config.AppSecret = TextBox2.Text.ToString();

    if (!string.IsNullOrEmpty(TextBox3.Text))
        _config.CanvasPage = TextBox3.Text.ToString();

    if (!string.IsNullOrEmpty(TextBox4.Text))
        _config.CanvasUrl = TextBox4.Text.ToString();

    _config.CancelUrlPath = "";
    config.Save();
}

The web.config looks like this (the part I'm trying to work with):

<configSections>
    <section type="Facebook.FacebookConfigurationSection, Facebook" name="facebookSettings" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>

<facebookSettings
  appId = "xxxxxxxxxxxxxxx"
  appSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx"
  canvasPage = "xxxxxxxxxxxxxxxxxx"
  canvasUrl ="xxxxxxxxxxxxxxxxxx"
  cancelUrlPath = "" />

Doing this, gives me the "Object reference not set to an instance of an object." on _config, which tells me that nothing gets returned.

Is there anything "facebook specific" that causes this?

Another thing; I came across this new method of working with facebook settings in code:

FacebookContext.SetApplication( IFacebookApplication )

I haven't been able to find a good example that uses this. Has anyone worked with this before?

like image 907
Soeren Avatar asked Dec 10 '25 13:12

Soeren


2 Answers

Just use

var sec = ConfigurationManager.GetSection("facebookSettings"); 

FacebookConfigurationSection config = (sec as Facebook.FacebookConfigurationSection); 

config.AppID etc etc

like image 69
Folger Avatar answered Dec 12 '25 03:12

Folger


Try

section type="Facebook.FacebookConfigurationSection"

or, if you have no namespace

section type="FacebookConfigurationSection"

I assume the line that you commented out didn't work either?

FacebookConfigurationSection config = (FacebookConfigurationSection)System.Configuration.ConfigurationManager.GetSection("facebookSettings");
like image 26
Cosmin Avatar answered Dec 12 '25 03:12

Cosmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!