Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YCompany.Configurations;

namespace CustomConfigurationProvider.Controllers
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controller is not requried in this project. this project will be referrenced in other project which will actually use the configuration provider.

[Route("api/[controller]")]
[ApiController]
public class CustomConfigurationController : ControllerBase
{
private readonly IConfiguration _configuration;
public CustomConfigurationController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet]
public IActionResult Get()
{
var metadata = new SecurityMetaData
{
ApiKey = _configuration["ApiKey"],
ApiSecret = _configuration["ApiSecret"]
};
return Ok(metadata);
}
}
}
16 changes: 16 additions & 0 deletions CustomConfigurationProvider/CustomConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace YCompany.Configurations
{
public static class CustomConfigurationExtensions
{
public static IConfigurationBuilder AddSecurityConfiguration
(this IConfigurationBuilder builder)
{
return builder.Add(new CustomConfigurationSource());
}
}
}
30 changes: 30 additions & 0 deletions CustomConfigurationProvider/CustomConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;

namespace YCompany.Configurations
{
public class CustomConfigurationProvider :ConfigurationProvider
{
public override void Load()
{
var text = File.ReadAllText(@"D:\SecurityMetadata.json");
var options = new JsonSerializerOptions
{ PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var content = JsonSerializer.Deserialize<SecurityMetaData>
(text, options);
if (content != null)
{
var Data = new Dictionary<string, string>
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are more than 1 configurations in the json file?

{"ApiKey", content.ApiKey},
{"ApiSecret", content.ApiSecret}
};
}
}

}
}
13 changes: 13 additions & 0 deletions CustomConfigurationProvider/CustomConfigurationProvider.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configuration project should be a class library. not a web application

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
16 changes: 16 additions & 0 deletions CustomConfigurationProvider/CustomConfigurationSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace YCompany.Configurations
{
public class CustomConfigurationSource:IConfigurationSource

{
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new CustomConfigurationProvider();
}
}
}
29 changes: 29 additions & 0 deletions CustomConfigurationProvider/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using YCompany.Configurations;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Configuration.AddSecurityConfiguration();

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
31 changes: 31 additions & 0 deletions CustomConfigurationProvider/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57907",
"sslPort": 44305
}
},
"profiles": {
"CustomConfigurationProvider": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7007;http://localhost:5261",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
12 changes: 12 additions & 0 deletions CustomConfigurationProvider/SecurityMetaData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace YCompany.Configurations
{
public class SecurityMetaData
{
public string ApiKey { get; set; }
public string ApiSecret { get; set; }
}
}
8 changes: 8 additions & 0 deletions CustomConfigurationProvider/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions CustomConfigurationProvider/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}