SlideShare a Scribd company logo
.NET Core + ASP.NET Core Training Course
Session 8
.NET Core
What we learned?
Session 6, 7 Overview
• Overview on HTTP Protocol
• Introducing ASP.NET Core applications
• Startup Class
• Middleware in ASP.NET core
• Working with Static Files
• Error Handling
• Logging
.NET Core
What we’ll learn today?
Session 8 Agenda
• Working with Configuration
• Working with app secrets
.NET Core
Configuration in ASP.NET Core
Configuration
ASP.NET Core supports
• JSON
• XML
• INI
• environment variables
• command line arguments
• in-memory collection
formats for storing application configuration data. We can also write our own custom
configuration provider
.NET Core
Getting and setting configuration settings
Configuration – Getting Started
ASP.NET Core’s configuration system has been re-architected from previous versions
of ASP.NET, which relied on System.Configuration and XML configuration files like
web.config.
The new configuration model provides streamlined access to key/value based settings
that can be retrieved from a variety of sources.
.NET Core
Getting and setting configuration settings
Configuration – Getting Started
To work with settings, it is recommended to instantiate a Configuration in our
application’s Startup class.
If a name/value pair is written to Configuration, it is not persisted.
We should configure at least one source in order for Configuration to function correctly.
.NET Core
Getting and setting configuration settings
Configuration – Getting Started
.NET Core
hierarchical configuration settings
Configuration – hierarchical
It’s not unusual to store configuration values in a hierarchical structure, especially when
using external files (e.g. JSON, XML, INI).
values can be retrieved using a : separated key,
starting from the root of the hierarchy
.NET Core
Using the built-in sources
Configuration – built-in sources
We are not limited to using a single configuration source.
.NET Core
Using the built-in sources
Configuration – built-in sources
The order in which configuration sources are specified is important, as this establishes
the precedence with which settings will be applied if they exist in multiple locations.
The IHostingEnvironment service is used to get the current environment. In the
Development environment, the highlighted line of code above would look for a file
named appsettings.Development.json and use its values, overriding any other values, if
it’s present.
.NET Core
Using the built-in sources
Configuration – built-in sources
.NET Core
reloadOnChange
Configuration – reloadOnChange
When specifying files as configuration sources, you can optionally specify whether changes
to the file should result in the settings being reloaded. This is configured by passing in a true
value for the reloadOnChange parameter when calling AddJsonFile or similar file-based
extension methods.
Warning
You should never store passwords or other
sensitive data in configuration provider code or
in plain text configuration files.
.NET Core Configuration – Overriding values
Overriding
values
.NET Core Configuration – reloadOnChange
Overriding
values
.NET Core
Using Options and configuration objects
Configuration – Using Options
The options pattern enables using custom options classes to represent a group of related
settings.
• A class needs to have a public read-write property for each setting and a
constructor that does not take any parameters
• Options can be injected into our application using the IOptions<TOptions>
accessor service.
• setup the IOptions<TOptions> service you call the AddOptions extension method
during startup in our ConfigureServices method
.NET Core
Using Options and configuration objects
Configuration – Using Options
.NET Core
Working with options
Configuration – Working with options
When you bind options to configuration, each property in your options type is bound to a
configuration key of the form property:subproperty:....
For example, the MyOptions.Option1 property is bound to the key Option1, which is read
from the option1 property in appsettings.json.
Note that configuration keys are case insensitive.
Use the AddSingleton<IConfigureOptions<TOptions>> extension method to register a
custom IConfigureOptions<TOptions> service to configure options using objects that must
be obtained from the service container (for example, to read settings from a database)
.NET Core
Writing custom providers
Configuration – Writing custom providers
Implement the IConfigurationSource interface, which exposes a Build method. The build
method configures and returns an IConfigurationProvider.
.NET Core
Writing custom providers
Configuration – Writing custom providers
.NET Core
Writing custom providers
Configuration – Writing custom providers
By convention you can also add an AddEntityFrameworkConfiguration extension method
for adding the configuration source
.NET Core
Summery
Configuration – Summery
Required Package(s):
• Microsoft.Extensions.Configuration.Json
• Microsoft.Extensions.Configuration.Ini
• Microsoft.Extensions.Configuration.Xml
.NET Core
Summery
Configuration – Summery
Reading configuration keys:
var val = Configuration.GetValue<int>("key-name", defaultValue: 10);
For binding configuration structure to a class use:
Microsoft.Extensions.Configuration.Binder
.NET Core
Safe storage of app secrets during development
Configuration – app secrets
• Use the Secret Manager tool to keep secrets out of your code
• We should never store passwords or other sensitive data in source code
• We shouldn’t use production secrets in development and test mode
• The Secret Manager tool helps prevent sensitive data from being checked into source
control
• The Secret Manager tool is a project tool that can be used to store secrets for a .NET
Core project during development
• The Secret Manager tool does not encrypt the stored secrets and should not be
treated as a trusted store. It is for development purposes only
.NET Core
Safe storage of app secrets during development
Configuration – app secrets
• Add SecretManager.Tools to the tools section of the project.json file and run dotnet
restore
• Test the Secret Manager tool by running the following command:
• dotnet user-secrets –h
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0",
"Microsoft.Extensions.SecretManager.Tools": "1.0.0" },
.NET Core
Safe storage of app secrets during development
Configuration – app secrets
• The Secret Manager tool operates on project specific configuration settings that are
stored in your user profile. To use user secrets the project must specify a userSecretsId
value in its project.json file. The value of userSecretsId is arbitrary, but is generally
unique to the project.
• Add a userSecretsId for your project in its project.json file:
• "userSecretsId": "aspnet-WebApp1-c23d27a4-eb88-4b18-9b77-2a93f3b15119",
• Use the Secret Manager tool to set a secret. For example, in a command window from
the project directory enter the following:
• dotnet user-secrets set MySecret ValueOfMySecret
.NET Core
Safe storage of app secrets during development
Configuration – app secrets
• dotnet user-secrets set MySecret ValueOfMySecret --project c:workWebApp1
Accessing user secrets via configuration
You access Secret Manager secrets through the configuration system. Add the
Microsoft.Extensions.Configuration.UserSecrets as a dependency in your project.json file and run
dotnet restore.
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
.NET Core
Safe storage of app secrets during development
Configuration – app secrets
How the Secret Manager tool works
The secret manager tool abstracts away the implementation details, such as where and how the values are
stored. You can use the tool without knowing these implementation details. In the current version, the values
are stored in a JSON configuration file in the user profile directory:
• Windows: %APPDATA%microsoftUserSecrets<userSecretsId>secrets.json
• Linux: ~/.microsoft/usersecrets/<userSecretsId>/secrets.json
• Mac: ~/.microsoft/usersecrets/<userSecretsId>/secrets.json
• The value of userSecretsId comes from the value specified in project.json
.NET Core
Demo
Demo

More Related Content

PDF
.NET Core, ASP.NET Core Course, Session 18
PDF
.NET Core, ASP.NET Core Course, Session 13
PDF
.NET Core, ASP.NET Core Course, Session 19
PDF
.NET Core, ASP.NET Core Course, Session 14
PDF
.NET Core, ASP.NET Core Course, Session 17
PDF
.NET Core, ASP.NET Core Course, Session 10
PDF
.NET Core, ASP.NET Core Course, Session 12
PDF
.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 18
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 19
.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 17
.NET Core, ASP.NET Core Course, Session 10
.NET Core, ASP.NET Core Course, Session 12
.NET Core, ASP.NET Core Course, Session 7

What's hot (20)

PDF
.NET Core, ASP.NET Core Course, Session 11
PDF
.NET Core, ASP.NET Core Course, Session 16
PDF
.NET Core, ASP.NET Core Course, Session 15
PDF
.NET Core, ASP.NET Core Course, Session 6
PDF
.NET Core, ASP.NET Core Course, Session 9
PDF
.NET Core, ASP.NET Core Course, Session 3
PDF
.NET Core, ASP.NET Core Course, Session 2
PDF
Share point review qustions
ODP
Hibernate Developer Reference
DOC
24 collections framework interview questions
PPTX
Spring & hibernate
PPTX
Spring core
PPTX
Mike Taulty OData (NxtGen User Group UK)
PDF
Microsoft Search Server 2008 - Technical Overview
DOCX
Hibernate notes
ODP
Different Types of Containers in Spring
PPS
Java Hibernate Programming with Architecture Diagram and Example
PPTX
Integrating Servlets and JSP (The MVC Architecture)
PPTX
Spring MVC 5 & Hibernate 5 Integration
.NET Core, ASP.NET Core Course, Session 11
.NET Core, ASP.NET Core Course, Session 16
.NET Core, ASP.NET Core Course, Session 15
.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 2
Share point review qustions
Hibernate Developer Reference
24 collections framework interview questions
Spring & hibernate
Spring core
Mike Taulty OData (NxtGen User Group UK)
Microsoft Search Server 2008 - Technical Overview
Hibernate notes
Different Types of Containers in Spring
Java Hibernate Programming with Architecture Diagram and Example
Integrating Servlets and JSP (The MVC Architecture)
Spring MVC 5 & Hibernate 5 Integration
Ad

Similar to .NET Core, ASP.NET Core Course, Session 8 (20)

PPTX
Go (con)figure - Making sense of .NET configuration
PDF
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
PPTX
Quick Interview Preparation Dot Net Core
PPTX
Introducing ASP.NET Core 2.0
PPTX
Academy PRO: ASP .NET Core
PPTX
.NET Core: a new .NET Platform
PDF
Azure App configuration
PDF
(Ebook) C# 6 and .NET Core 1.0: Modern Cross-Platform Development by Mark J. ...
PDF
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
PPTX
ASP.NET Core For The Agile Enterprise
PPTX
Explore asp.net core 3.0 features
PDF
Asp.Net Core MVC , Razor page , Entity Framework Core
PDF
ASP.NET Core in Action (2018).pdf
PDF
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
PDF
C 7 and NET Core Modern Cross Platform Development 2nd Edition Mark J. Price
PPTX
.NET Overview & Roadmap
PPTX
ASP.NET Core 1.0
PPTX
Asp.net core 2.0 security and identity
PPTX
"Project Tye to Tie .NET Microservices", Oleg Karasik
PDF
Architecting ASP.NET Core Applications Carl-Hugo Marcotte
Go (con)figure - Making sense of .NET configuration
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
Quick Interview Preparation Dot Net Core
Introducing ASP.NET Core 2.0
Academy PRO: ASP .NET Core
.NET Core: a new .NET Platform
Azure App configuration
(Ebook) C# 6 and .NET Core 1.0: Modern Cross-Platform Development by Mark J. ...
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
ASP.NET Core For The Agile Enterprise
Explore asp.net core 3.0 features
Asp.Net Core MVC , Razor page , Entity Framework Core
ASP.NET Core in Action (2018).pdf
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
C 7 and NET Core Modern Cross Platform Development 2nd Edition Mark J. Price
.NET Overview & Roadmap
ASP.NET Core 1.0
Asp.net core 2.0 security and identity
"Project Tye to Tie .NET Microservices", Oleg Karasik
Architecting ASP.NET Core Applications Carl-Hugo Marcotte
Ad

More from Amin Mesbahi (10)

PPTX
Software Performance Benchmarking using BenchmarkDotNet Webinar
PPTX
How to choose appropriate technology for product development - Persian Version
PPTX
How to choose appropriate technology for product development
PPTX
Python + Machine Learning Course, Session 2
PPTX
Python + Machine Learning Course, Session 1
PDF
.NET Core, ASP.NET Core Course, Session 5
PDF
.NET Core, ASP.NET Core Course, Session 4
PDF
.NET Core, ASP.NET Core Course, Session 1
PPTX
A comparative study of process templates in team
PPTX
SQL server 2016 New Features
Software Performance Benchmarking using BenchmarkDotNet Webinar
How to choose appropriate technology for product development - Persian Version
How to choose appropriate technology for product development
Python + Machine Learning Course, Session 2
Python + Machine Learning Course, Session 1
.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 1
A comparative study of process templates in team
SQL server 2016 New Features

Recently uploaded (20)

PDF
AI in Product Development-omnex systems
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Introduction to Artificial Intelligence
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
System and Network Administraation Chapter 3
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
history of c programming in notes for students .pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
AI in Product Development-omnex systems
2025 Textile ERP Trends: SAP, Odoo & Oracle
How Creative Agencies Leverage Project Management Software.pdf
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
System and Network Administraation Chapter 3
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
VVF-Customer-Presentation2025-Ver1.9.pptx
Odoo POS Development Services by CandidRoot Solutions
How to Choose the Right IT Partner for Your Business in Malaysia
Odoo Companies in India – Driving Business Transformation.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
history of c programming in notes for students .pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41

.NET Core, ASP.NET Core Course, Session 8

  • 1. .NET Core + ASP.NET Core Training Course Session 8
  • 2. .NET Core What we learned? Session 6, 7 Overview • Overview on HTTP Protocol • Introducing ASP.NET Core applications • Startup Class • Middleware in ASP.NET core • Working with Static Files • Error Handling • Logging
  • 3. .NET Core What we’ll learn today? Session 8 Agenda • Working with Configuration • Working with app secrets
  • 4. .NET Core Configuration in ASP.NET Core Configuration ASP.NET Core supports • JSON • XML • INI • environment variables • command line arguments • in-memory collection formats for storing application configuration data. We can also write our own custom configuration provider
  • 5. .NET Core Getting and setting configuration settings Configuration – Getting Started ASP.NET Core’s configuration system has been re-architected from previous versions of ASP.NET, which relied on System.Configuration and XML configuration files like web.config. The new configuration model provides streamlined access to key/value based settings that can be retrieved from a variety of sources.
  • 6. .NET Core Getting and setting configuration settings Configuration – Getting Started To work with settings, it is recommended to instantiate a Configuration in our application’s Startup class. If a name/value pair is written to Configuration, it is not persisted. We should configure at least one source in order for Configuration to function correctly.
  • 7. .NET Core Getting and setting configuration settings Configuration – Getting Started
  • 8. .NET Core hierarchical configuration settings Configuration – hierarchical It’s not unusual to store configuration values in a hierarchical structure, especially when using external files (e.g. JSON, XML, INI). values can be retrieved using a : separated key, starting from the root of the hierarchy
  • 9. .NET Core Using the built-in sources Configuration – built-in sources We are not limited to using a single configuration source.
  • 10. .NET Core Using the built-in sources Configuration – built-in sources The order in which configuration sources are specified is important, as this establishes the precedence with which settings will be applied if they exist in multiple locations. The IHostingEnvironment service is used to get the current environment. In the Development environment, the highlighted line of code above would look for a file named appsettings.Development.json and use its values, overriding any other values, if it’s present.
  • 11. .NET Core Using the built-in sources Configuration – built-in sources
  • 12. .NET Core reloadOnChange Configuration – reloadOnChange When specifying files as configuration sources, you can optionally specify whether changes to the file should result in the settings being reloaded. This is configured by passing in a true value for the reloadOnChange parameter when calling AddJsonFile or similar file-based extension methods. Warning You should never store passwords or other sensitive data in configuration provider code or in plain text configuration files.
  • 13. .NET Core Configuration – Overriding values Overriding values
  • 14. .NET Core Configuration – reloadOnChange Overriding values
  • 15. .NET Core Using Options and configuration objects Configuration – Using Options The options pattern enables using custom options classes to represent a group of related settings. • A class needs to have a public read-write property for each setting and a constructor that does not take any parameters • Options can be injected into our application using the IOptions<TOptions> accessor service. • setup the IOptions<TOptions> service you call the AddOptions extension method during startup in our ConfigureServices method
  • 16. .NET Core Using Options and configuration objects Configuration – Using Options
  • 17. .NET Core Working with options Configuration – Working with options When you bind options to configuration, each property in your options type is bound to a configuration key of the form property:subproperty:.... For example, the MyOptions.Option1 property is bound to the key Option1, which is read from the option1 property in appsettings.json. Note that configuration keys are case insensitive. Use the AddSingleton<IConfigureOptions<TOptions>> extension method to register a custom IConfigureOptions<TOptions> service to configure options using objects that must be obtained from the service container (for example, to read settings from a database)
  • 18. .NET Core Writing custom providers Configuration – Writing custom providers Implement the IConfigurationSource interface, which exposes a Build method. The build method configures and returns an IConfigurationProvider.
  • 19. .NET Core Writing custom providers Configuration – Writing custom providers
  • 20. .NET Core Writing custom providers Configuration – Writing custom providers By convention you can also add an AddEntityFrameworkConfiguration extension method for adding the configuration source
  • 21. .NET Core Summery Configuration – Summery Required Package(s): • Microsoft.Extensions.Configuration.Json • Microsoft.Extensions.Configuration.Ini • Microsoft.Extensions.Configuration.Xml
  • 22. .NET Core Summery Configuration – Summery Reading configuration keys: var val = Configuration.GetValue<int>("key-name", defaultValue: 10); For binding configuration structure to a class use: Microsoft.Extensions.Configuration.Binder
  • 23. .NET Core Safe storage of app secrets during development Configuration – app secrets • Use the Secret Manager tool to keep secrets out of your code • We should never store passwords or other sensitive data in source code • We shouldn’t use production secrets in development and test mode • The Secret Manager tool helps prevent sensitive data from being checked into source control • The Secret Manager tool is a project tool that can be used to store secrets for a .NET Core project during development • The Secret Manager tool does not encrypt the stored secrets and should not be treated as a trusted store. It is for development purposes only
  • 24. .NET Core Safe storage of app secrets during development Configuration – app secrets • Add SecretManager.Tools to the tools section of the project.json file and run dotnet restore • Test the Secret Manager tool by running the following command: • dotnet user-secrets –h "tools": { "Microsoft.AspNetCore.Razor.Tools": "1.0.0", "Microsoft.Extensions.SecretManager.Tools": "1.0.0" },
  • 25. .NET Core Safe storage of app secrets during development Configuration – app secrets • The Secret Manager tool operates on project specific configuration settings that are stored in your user profile. To use user secrets the project must specify a userSecretsId value in its project.json file. The value of userSecretsId is arbitrary, but is generally unique to the project. • Add a userSecretsId for your project in its project.json file: • "userSecretsId": "aspnet-WebApp1-c23d27a4-eb88-4b18-9b77-2a93f3b15119", • Use the Secret Manager tool to set a secret. For example, in a command window from the project directory enter the following: • dotnet user-secrets set MySecret ValueOfMySecret
  • 26. .NET Core Safe storage of app secrets during development Configuration – app secrets • dotnet user-secrets set MySecret ValueOfMySecret --project c:workWebApp1 Accessing user secrets via configuration You access Secret Manager secrets through the configuration system. Add the Microsoft.Extensions.Configuration.UserSecrets as a dependency in your project.json file and run dotnet restore. "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
  • 27. .NET Core Safe storage of app secrets during development Configuration – app secrets How the Secret Manager tool works The secret manager tool abstracts away the implementation details, such as where and how the values are stored. You can use the tool without knowing these implementation details. In the current version, the values are stored in a JSON configuration file in the user profile directory: • Windows: %APPDATA%microsoftUserSecrets<userSecretsId>secrets.json • Linux: ~/.microsoft/usersecrets/<userSecretsId>/secrets.json • Mac: ~/.microsoft/usersecrets/<userSecretsId>/secrets.json • The value of userSecretsId comes from the value specified in project.json