Set facebook web.config options in code (C #)

I am trying to set the web.config parameters in a facebook application from code to avoid working directly with the web.config file. I tried my own ConfigurationSection class and then using WebConfigurationManager to access the web.config file. The problem is that I cannot 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();
}

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 = "" />

When doing this, give me a reference to an object not set for the object instance. on _config, which tells me that nothing is returning.

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 )

, . - ?

+3
5

,

FacebookContext.SetApplication( IFacebookApplication )

, "ConfigurationSection"...

, ..

web.config XmlDocument ...

XmlDocument XmlDoc = new XmlDocument();
    XmlDoc.Load(Server.MapPath("web.config"));

    XmlAttribute appId = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@appId") as XmlAttribute;
    if (appId != null) appId.Value = TextBox1.Text.ToString();

    XmlAttribute appSecret = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@appSecret") as XmlAttribute;
    if (appSecret != null) appSecret.Value = TextBox2.Text.ToString();

    XmlAttribute canvasPage = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@canvasPage") as XmlAttribute;
    if (canvasPage != null) canvasPage.Value = TextBox3.Text.ToString();

    XmlAttribute canvasUrl = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@canvasUrl") as XmlAttribute;
    if (canvasUrl != null) canvasUrl.Value = TextBox4.Text.ToString();

    XmlDoc.Save(Server.MapPath("web.config"));

, ...

0

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

FacebookConfigurationSection config = (sec as Facebook.FacebookConfigurationSection); 

config.AppID ..

+2

section type="Facebook.FacebookConfigurationSection"

,

section type="FacebookConfigurationSection"

, , , ?

FacebookConfigurationSection config = (FacebookConfigurationSection)System.Configuration.ConfigurationManager.GetSection("facebookSettings");
+1

, <facebookSettings> :

<facebookSettings>
    <add key="appId " value="xxxxxxxxxxxxxxxx" />
    <add key="appSecret " value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" />
            ...
            ...
<facebookSettings>
0

, , , web.config,

System.Configuration.ConfigurationSection sec = System.Configuration.ConfigurationManager.GetSection("facebookSettings");
Facebook.FacebookConfigurationSection _config = (sec as Facebook.FacebookConfigurationSection); 

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();
0

Source: https://habr.com/ru/post/1795708/


All Articles