Skip to content

Instantly share code, notes, and snippets.

View wellingtonjhn's full-sized avatar

Wellington Nascimento wellingtonjhn

View GitHub Profile
@wellingtonjhn
wellingtonjhn / 5.Post.SettingsValidation.PostConfigureMethod.Startup.cs
Last active August 30, 2018 06:33
Startup PostConfigure Method implementation
public class Startup
{
// ... código omitido
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration);
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
@wellingtonjhn
wellingtonjhn / 3.Post.SettingsValidation.MySettingsValidator.cs
Last active September 8, 2018 03:57
MySettingsValidator PostConfigureOptions implementation
public class MySettingsValidator : IPostConfigureOptions<MySettings>
{
public void PostConfigure(string name, MySettings options)
{
if (options.Validate())
{
return;
}
throw new InvalidOperationException($"Invalid {nameof(MySettings)} options. {string.Join(",", options.ValidationResult)}");
public abstract class Repository
{
protected string ConnectionString { get; }
protected Repository(IConfiguration configuration)
{
ConnectionString = configuration.GetConnectionString("DefaultConnection");
}
// ... código omitido
[Route("api/[controller]")]
public class HomeController : Controller
{
public HomeController(IOptions<MySettings> settings)
{
var applicationName = settings.Value.ApplicationName;
var author = settings.Value.Author;
}
}
public class Startup
{
public IConfiguration Configuration { get; }
// ... código omitido
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration);
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
public class MySettings
{
public string ApplicationName { get; set; }
public string Author { get; set; }
}
@wellingtonjhn
wellingtonjhn / 2.Post.HomeController.cs
Last active July 26, 2018 22:56
IConfiguration Simple Usage
public HomeController(IConfiguration configuration)
{
var applicationName = configuration.GetValue<string>("MySettings:ApplicationName");
var logLevel = configuration.GetValue<string>("Logging:LogLevel:Default");
}
@wellingtonjhn
wellingtonjhn / 1.Post.ConfigurationBuilder.cs
Created July 26, 2018 06:13
ConfigurationBuilder Initialization
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
@wellingtonjhn
wellingtonjhn / 5.Post.RefreshToken.AppSettings.json
Last active July 18, 2018 01:34
AppSettings - Refresh Token
{
"JwtSettings": {
"Issuer": "DemoJwtApi",
"Audience": "demo-jwt-api",
"SigningKey": "kzfSPDKwdx5KnyxtBTlwNW_IoqrpbaGRwaFNdqxQyv-WVIqeLKOGJVLmh4lRd4wUPmolq6CM7Bs4r1NRbAoZQZQui80YbqMGuymdw5NSlnMvoMHNdF2niiydKV5X2esajAZk6t1pu1Jf05TNIxQBO1aI8xnk4ttVIPXRDG47wKlTPwnvqpVX3lh5nwrG_A4fUj7KOslfysPbusORDePIQlnnCqkzURl3qanQzjku02kWxujqpujl3I1VpJ0zKc2ReeyVNoeKNG3WYi2eO8sYsDw8XtbkcY5mJW7dHeXSMYvzrFIWDbbxorb5LP0FtFbsgOfh8IYT4qzSL4BmUV17ag",
"ValidForMinutes": 60,
"RefreshTokenValidForMinutes": 120
}
}
@wellingtonjhn
wellingtonjhn / 4.Post.RefreshToken.AccountsController.cs
Last active July 18, 2018 01:33
Accounts Controller - Refresh Token
[Route("api/[controller]")]
public class AccountsController : Controller
{
private readonly IMediator _mediator;
public AccountsController(IMediator mediator)
{
_mediator = mediator;
}