Skip to content

Instantly share code, notes, and snippets.

View PradeepLoganathan's full-sized avatar

Pradeep Loganathan PradeepLoganathan

View GitHub Profile
@PradeepLoganathan
PradeepLoganathan / appsettings.json
Created July 9, 2020 00:52
Configuring http2 using appsettings
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://localhost:5000"
},
"HttpsInlineCertFile": {
"Url": "https://localhost:5001",
"Protocols": "Http1AndHttp2",
"Certificate": {
"Path": "./certificate.pfx",
@PradeepLoganathan
PradeepLoganathan / Startup.cs
Last active June 30, 2020 09:01
Adding HSTS to the middleware pipeline
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(365);
});
@PradeepLoganathan
PradeepLoganathan / startup.cs
Last active June 30, 2020 07:56
Using HTTPS redirection
public void ConfigureServices(IServiceCollection services)
{
if (!_env.IsDevelopment())
{
services.AddHttpsRedirection(opts => {
opts.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
opts.HttpsPort = 44300;
});
}
else
@PradeepLoganathan
PradeepLoganathan / publiccontroller.cs
Last active June 29, 2020 23:23
asp.net core controller with enable cors attribute
[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
[AllowAnonymous]
[EnableCors(CORSPolicies.PublicApi)]
public class PublicApiControllerBase : ControllerBase
{
[DisableCors]
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
@PradeepLoganathan
PradeepLoganathan / startup.cs
Created June 29, 2020 22:27
using different CORS policies
if (env.IsDevelopment())
{
app.UseCors(CORSPolicies.DevAPI);
}
else
{
app.UseCors(CORSPolicies.PublicAPI);
}
@PradeepLoganathan
PradeepLoganathan / startup.cs
Last active June 29, 2020 23:30
Defining and adding multiple CORS policies to the middleware
readonly string CORSPolicies.PublicAPI = "corspolicy.public";
readonly string CORSPolicies.DevAPI = "corspolicy.dev";
services.AddCors(options =>
{
options.AddPolicy(CORSPolicies.PublicAPI,
builder => builder
.AllowAnyHeader()
.WithMethods("POST", "GET")
.WithOrigins("https://domain1.com", "https://domain2.com"));
@PradeepLoganathan
PradeepLoganathan / startup.cs
Created June 25, 2020 01:10
OAS 3 for .net core 3.1 using swashbuckle 5- Adding a API Key Security definition
services.AddSwaggerGen(c =>
{
var apiinfo = new OpenApiInfo
{
Title = "theta-CandidateAPI",
Version = "v1",
Description = "Candidate API for thetalentbot",
Contact = new OpenApiContact
{ Name = "thetalentbot", Url = new Uri("https://thetalentbot.com/developers/contact") },
License = new OpenApiLicense()
OpenApiSecurityScheme securityScheme = new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("/auth-server/connect/authorize", UriKind.Relative),
TokenUrl = new Uri("/auth-server/connect/token", UriKind.Relative),
Scopes = new Dictionary<string, string>
@PradeepLoganathan
PradeepLoganathan / SecurityDefinition-Bearer.cs
Last active June 25, 2020 01:02
OAS 3 for .net core 3.1 using swashbuckle 5- Adding a bearer Security definition
services.AddSwaggerGen(options =>
{
var apiinfo = new OpenApiInfo
{
Title = "theta-CandidateAPI",
Version = "v1",
Description = "Candidate API for thetalentbot",
Contact = new OpenApiContact
{ Name = "thetalentbot", Url = new Uri("https://thetalentbot.com/developers/contact") },
License = new OpenApiLicense()