Skip to content

Instantly share code, notes, and snippets.

View vmandic's full-sized avatar
🤠
chillin'

Vedran Mandić vmandic

🤠
chillin'
View GitHub Profile
@vmandic
vmandic / IApplicationBuilderExtensions.cs
Created May 23, 2019 13:53
meds-processor, part/4, snippet #20
using System;
using MedsProcessor.WebAPI.Infrastructure;
using Microsoft.AspNetCore.Builder;
namespace MedsProcessor.WebAPI.Extensions
{
public static class IApplicationBuilderExtensions
{
public static IApplicationBuilder UseBasicAuthentication(
this IApplicationBuilder app,
@vmandic
vmandic / BasicAuthMiddleware.cs
Created May 23, 2019 13:49
meds-processor, part/4, snippet #19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace MedsProcessor.WebAPI.Infrastructure
{
@vmandic
vmandic / Startup.cs, Swagger config
Created May 23, 2019 13:28
meds-processor, part/4, snippet #18
services.AddSwaggerGen(opts =>
{
opts.SwaggerDoc(
"v1-0",
new Info
{
Title = "HZZO meds-processor v1.0",
Version = "1.0"
});
@vmandic
vmandic / AuthController.cs
Created May 17, 2019 12:38
med-processor, part/4, snippet #17
using MedsProcessor.WebAPI.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace MedsProcessor.WebAPI.Controllers
{
[ApiVersionNeutral, Route("api/[controller]")]
public class AuthController : ApiControllerBase
{
@vmandic
vmandic / JwtAuthService.cs
Created May 17, 2019 12:24
meds-processor, part/4, snippet #16
using System;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace MedsProcessor.WebAPI.Infrastructure
{
@vmandic
vmandic / AuthTokenOptions.cs
Created May 17, 2019 12:13
meds-processor, part/4, snippet #15
using System;
using System.Text;
using Microsoft.IdentityModel.Tokens;
namespace MedsProcessor.WebAPI.Infrastructure
{
public class AuthTokenOptions
{
public string Secret { get; set; }
public string Issuer { get; set; }
@vmandic
vmandic / Startu.cs, ConfigureServices()
Last active May 17, 2019 12:11
meds-processor, part/4, snippet #14
services.AddScoped<IJwtAuthService, JwtAuthService>();
var tokenOptsConfig = Configuration.GetSection(nameof(AuthTokenOptions).Replace("Options", ""));
var tokenOpts = tokenOptsConfig.Get<AuthTokenOptions>();
services.Configure<AuthTokenOptions>(tokenOptsConfig);
services.AddAuthentication(opts =>
{
opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opts.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
@vmandic
vmandic / ApiV1ControllerBase.cs
Last active May 15, 2019 21:35
meds-processor, part/4, snippet #13
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace MedsProcessor.WebAPI.Controllers.v1_0
{
[ApiVersion("1.0"), Route("api/v{apiVersion:apiVersion}")]
[ProducesResponseType(401), ProducesResponseType(403)]
public abstract class ApiV1ControllerBase : ApiControllerBase { }
}
@vmandic
vmandic / Startup.cs ConfigureServices()
Last active May 15, 2019 14:09
meds-processor, part/4, snippet #12
services.AddMvc().AddJsonOptions(opts =>
{
opts.SerializerSettings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
opts.SerializerSettings.Formatting = Formatting.Indented;
opts.SerializerSettings.Converters.Add(new StringEnumConverter());
opts.SerializerSettings.DateFormatString = "yyyy-MM-dd";
opts.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
@vmandic
vmandic / DrugsController.cs
Created May 14, 2019 08:50
meds-processor, part/4, snippet #12
using System;
using System.Collections.Generic;
using System.Linq;
using MedsProcessor.Common;
using MedsProcessor.Common.Models;
using MedsProcessor.WebAPI.Core;
using MedsProcessor.WebAPI.Infrastructure;
using Microsoft.AspNetCore.Mvc;
namespace MedsProcessor.WebAPI.Controllers