Qiuzman
Qiuzman

Reputation: 1761

How to CreateJsonString from appsettings section

I previously stored my box json config as a separate file which I would store outside of my application and get the location of it and pull the config and ReadToEnd. However I want due to the stress of managing folder permissions and what not for IIS, I prefer to just store the json config inside my appsettings.json. However, I am not sure how I actually can convert the json structure of that section into my code. My code so far is as follows:

this.token = token;
var config = BoxConfigBuilder.CreateFromJsonString(configuration.GetSection("Box:ProductionConfig").ToString()).Build();
_session = new BoxJWTAuth(config);

However when running this is draws the folloing netwonsoft error:

"Unexpected character encountered while parsing value: M. Path '', line 0, position 0."

I am thinking that I need to convert the appsetting configure section to a json string but not sure how to do that.

My appsettings.json is as follows:

 {"Box": {
"ProductionConfig": {
  "boxAppSettings": {
    "clientID": “dsafafdsafasdfasdfasdfasdfsadfsdaf”,
    "clientSecret": “dfasdfasdfasdfasdfdsfdfedfdf”,
    "appAuth": {
      "publicKeyID": "kdjfkalsfjakslf;a",
      "privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----\nkdsajfl;sajdkfl;asjdkfl;sajdkfl;asjkdfl;asjdkfl;asjfklas;djfkdasld;fjklsa;fjskal;f\n——END ENCRYPTED PRIVATE KEY-----\n",
      "passphrase": “dasfsafsadfasdfasdfasdfsa”
    }
  },
  "enterpriseID": "dsafasfdsafasdf"
},
"ProductionDomain": "https://testing.app.box.com"},}

My previous code I used would read the file as follows except ProductionConfig was actually a path to the json file:

        this.token = token;
        var boxProductionConfigLocation = Path.Combine(System.IO.Directory.GetParent(env.ContentRootPath).ToString(), configuration["Box:ProductionConfig"]);
        var config = BoxConfigBuilder.CreateFromJsonString(System.IO.File.ReadAllText(boxProductionConfigLocation)).Build();
        _session = new BoxJWTAuth(config);

Upvotes: 1

Views: 263

Answers (1)

Code Name Jack
Code Name Jack

Reputation: 3333

The problem is coming from configuration.GetSection("Box:ProductionConfig").ToString(). ToString will not fetch the string, instead use,

configuration.GetValue<string>("Box:ProductionConfig")

Alernaively you can ry,

configuration.GetSection("Box:ProductionConfig")                                                 .Get<string>()

Update: With the updated appsetings I see that is just a normal config and not a json string so it cannot be read directly. You will have to create a backing class with public properties and then convert it back to json.

//First create these classes
public class ProductionConfig
{
    public BoxAppSettings BoxAppSettings { get; set; }
    public string EnterpriseID { get; set; }
}

public class BoxAppSettings
{
    public string ClientID { get; set; }
    public string ClientSecret { get; set; }
    public AppAuth AppAuth { get; set; }
}

public class AppAuth
{
    public string PublicKeyID { get; set; }
    public string PrivateKey { get; set; }
    public string Passphrase { get; set; }
}

//then read it like this
this.token = token;
var configJson= JsonSerializer.Serialize(configuration.GetSection("Box:ProductionConfig").Get<ProductionConfig>());
var config = BoxConfigBuilder.CreateFromJsonString(configJson).Build();
_session = new BoxJWTAuth(config);

Config binding is reflection based and hence it cant bind dynamically to JsonElement.

Upvotes: 1

Related Questions