Skip to content

Instantly share code, notes, and snippets.

View lurumad's full-sized avatar
🏠
Working from home

Luis Ruiz Pavon lurumad

🏠
Working from home
View GitHub Profile
@lurumad
lurumad / Startup.cs
Created September 17, 2020 05:22
Add authentication to SwaggerUI
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
@lurumad
lurumad / CachePipelineBehavior.cs
Last active August 3, 2020 07:09
Adding cache to MediatR
public class CachePipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly IDistributedCache cache;
private readonly ILogger<CachePipelineBehavior> logger;
public CachePipelineBehavior(
IDistributedCache cache,
ILogger<CachePipelineBehavior> logger)
{
Ensure.Argument.NotNull(cache, nameof(cache));
@lurumad
lurumad / CachePipelineBehavior.cs
Created August 3, 2020 07:09
Adding cache to MediatR
public class CachePipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly IDistributedCache cache;
private readonly ILogger<SGUKAspNetCore> logger;
public CachePipelineBehavior(
IDistributedCache cache,
ILogger<SGUKAspNetCore> logger)
{
Ensure.Argument.NotNull(cache, nameof(cache));
@lurumad
lurumad / apimwebhook.json
Created September 25, 2018 16:54
Handling errors
<send-one-way-request mode="new">
<set-url>https://hooks.slack.com/services/xxxxxxx/xxxxxxx/xxxxxxxxxx</set-url>
<set-method>POST</set-method>
<set-body>@{
return new JObject(
new JProperty("text", context.LastError.Message)
).ToString();
}</set-body>
</send-one-way-request>
@lurumad
lurumad / openid.md
Last active September 25, 2018 12:13
Protecting APIM with OpenId Connect

Add OpenID Connect server

https://idsrv/.well-known/openid-configuration

Client credentials:

  • implicit
  • https://{service}.portal.azure-api.net/docs/services/idsvr/console/openidconnect/authorizationcode/callback
  • https://{service}.portal.azure-api.net/docs/services/idsvr/console/openidconnect/implicit/callback
@lurumad
lurumad / monitoring.md
Created September 25, 2018 07:27
Monitoring APIM

Create an alert rule

Alert criteria: (Whenever the Unauthorized Gateway Requests is Greater than 1 count)

  • Unauthorized Gateway Requests
  • Greater than Total > 1
  • Over the last 5 minutes - Every minute

Alert details:

  • Unauthorized access
  • Unauthorized alert to the APIM
@lurumad
lurumad / logger.md
Created September 25, 2018 06:52
APIM Logger

Create an API Management logger

https://{your service}.management.azure-api.net/loggers/{new logger name}?api-version=2017-03-01

Content-Type : application/json Authorization : SharedAccessSignature 58...

{
  "loggerType" : "AzureEventHub",
@lurumad
lurumad / retry.xml
Created September 25, 2018 06:33
Retry policy APIM
<retry condition="@(context.Response.StatusCode >= 500)" count="2" interval="3" first-fast-retry="false">
<forward-request />
</retry>
@lurumad
lurumad / azure-alerts.json
Last active September 25, 2018 07:19
Azure alerts payload
{
"status": "Activated",
"context": {
"timestamp": "2015-08-14T22:26:41.9975398Z",
"id": "/subscriptions/s1/resourceGroups/useast/providers/microsoft.insights/alertrules/ruleName1",
"name": "ruleName1",
"description": "some description",
"conditionType": "Metric",
"condition": {
"metricName": "Requests",
# Install IIS
Install-WindowsFeature Web-Server,Web-Asp-Net45,NET-Framework-Features
# Install the .NET Core SDK
Invoke-WebRequest https://download.microsoft.com/download/1/1/5/115B762D-2B41-4AF3-9A63-92D9680B9409/dotnet-sdk-2.1.4-win-x64.exe -outfile $env:temp\dotnet-sdk-2.1.4-win-x64.exe
Start-Process $env:temp\dotnet-sdk-2.1.4-win-x64.exe -ArgumentList '/quiet' -Wait
# Install the .NET Core Windows Server Hosting bundle
Invoke-WebRequest https://go.microsoft.com/fwlink/?LinkId=817246 -outfile $env:temp\DotNetCore.WindowsHosting.exe
Start-Process $env:temp\DotNetCore.WindowsHosting.exe -ArgumentList '/quiet' -Wait