Click here to Skip to main content
13,895,921 members
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 18 Dec 2018
Licenced CPOL

Reading Objects From the Config File at Startup

, 18 Dec 2018
Rate this:
Please Sign up or sign in to vote.
How to deserialize objects from a web application"s config file


This tip reveals a very simple way to read sections of a web application's config file as objects, without requiring the Options pattern or dependency injection.


In ASP.NET Core, it's quite easy to configure a a group of related settings in the config file, normally appsettings.json, to be deserialized as classes and injected into DI clients. It is, however, apparently not as simple to simply deserailize an object from this file and use it before the DI container has been fully initialized.

Using the Code

As an example, I have taken code of mine where I configure password rules for ASP.NET Identity by reading these rules as an object from appsettings.json. Normally, I would hard code these settings, as they seldom change during the lifetime of an application, but in a project where I'm including password generation, and I want generated passwords to conform to the same rules as those configured for Identity, I thought it would be nice to store these password options in appsettings.json.

This is how I store the password rules in the config file:

  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=Flair;Trusted_Connection=True;
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
  "Authentication": {
    "PasswordRules": {
      "RequiredLength": 8,
      "RequiredUniqueChars": 2,
      "RequireNonAlphanumeric": false,
      "RequireLowercase": false,
      "RequireUppercase": false,
      "RequireDigit": true
  "AllowedHosts": "*"

And this is how I read these settings and configure Identity, in Startup.cs:

var pwdSection = Configuration.GetSection("Authentication:PasswordRules");
var pwdOpts = pwdSection.Get<PasswordOptions>();
services.Configure<IdentityOptions>(options =>
    options.Password = pwdOpts;

The options.Password property of the IdentityOptions being configured is a simple object of class PasswordOptions. All I have to do is make sure the "Authentication:PasswordRules" section of my config file is a serialization of an object.

The magic happens with the call:

var pwdOpts = pwdSection.Get<PasswordOptions>()

This would not normally be possible, unless I include the following NuGet package in the project.


This was kindly revealed by this StackOverflow answer.


This is the first version of this tip.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Brady Kelly
Founder Erisia Web Development
South Africa South Africa
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET MVC, with SQL Server, with special fondness for MVC and jQuery. I have been in this business for about eighteen years, and am currently trying to master Angular 4 and .NET Core, and somehow find a way to strengthen my creative faculties.
- Follow me on Twitter at @bradykelly

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 18 Dec 2018
Article Copyright 2018 by Brady Kelly
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid