Skip to content

Instantly share code, notes, and snippets.

View GeorgDangl's full-sized avatar
😎
Coding

Georg Dangl GeorgDangl

😎
Coding
View GitHub Profile
@GeorgDangl
GeorgDangl / AspNetCoreTestHostSignIn.cs
Last active October 22, 2019 19:27
Perform user sign in via request headers for integration testing with Asp.Net Core TestHost & the Asp.Net Core Identity framework
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
// Seed with integration tests specific data
ServerInitialization.InitializeIntegrationTestsDatabase(app.ApplicationServices).Wait();
}
app.UseStaticFiles();
app.UseIdentity();
// To log in a user for integration tests
@GeorgDangl
GeorgDangl / ApiModelStateValidationFilter.cs
Created December 19, 2016 19:57
Validation filter to automatically return BadRequest respones for invalid models and IsEqualToAttribute for comparing Password and ConfirmPasswords in models
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Filters
{
public class ApiModelStateValidationFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
@GeorgDangl
GeorgDangl / copyGeometryInteropDlls.ps1
Last active February 2, 2017 18:21
Copy xBim Geometry Interop Dlls in the new project format in .Net
$xBimGeometryPackages = Join-Path -Path $env:USERPROFILE -ChildPath "\.nuget\packages\Xbim.Geometry"
$latestxBimGeometryPackage32 = Join-Path -Path ((Get-ChildItem -Path $xBimGeometryPackages | Sort-Object Fullname -Descending)[0].FullName) -ChildPath "build\x86\Xbim.Geometry.Engine32.dll"
$latestxBimGeometryPackage64 = Join-Path -Path ((Get-ChildItem -Path $xBimGeometryPackages | Sort-Object Fullname -Descending)[0].FullName) -ChildPath "build\x64\Xbim.Geometry.Engine64.dll"
if (!(Test-Path "$PSScriptRoot\Dependencies")){
New-Item -ItemType Directory -Path "$PSScriptRoot\Dependencies"
}
Copy-Item -Path $latestxBimGeometryPackage32 -Destination "$PSScriptRoot\Dependencies\Xbim.Geometry.Engine32.dll"
Copy-Item -Path $latestxBimGeometryPackage64 -Destination "$PSScriptRoot\Dependencies\Xbim.Geometry.Engine64.dll"
@GeorgDangl
GeorgDangl / Index.cshtml
Last active February 21, 2017 21:13
Angular 2 in Visual Studio Webpack Configuration and Dependencies
<!DOCTYPE html>
<html>
<head>
<title>NetCoreHeroes</title>
<link rel="icon" type="image/png" href="/favicon.png">
<base href="@ViewData["AngularBase"]" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
@GeorgDangl
GeorgDangl / BasicAuthenticationHeaderValue.cs
Last active June 5, 2019 19:28
Asp.Net Core middleware for Asp.Net Identity projects to support Http Basic authentication, see https://blog.dangl.me/archive/http-basic-authentication-in-aspnet-core-projects
using System;
namespace Project.Middleware
{
public class BasicAuthenticationHeaderValue
{
public BasicAuthenticationHeaderValue(string authenticationHeaderValue)
{
if (!string.IsNullOrWhiteSpace(authenticationHeaderValue))
{
namespace InMemoreCompilation
{
public static class CodeGenerator
{
public static string GenerateCalculator()
{
var calculator = @"namespace Calculator
{
public class Calculator
{
@GeorgDangl
GeorgDangl / ConvertPasswordToSecureString.ps1
Last active April 27, 2017 20:05
Securely storing strings in PowerShell scripts and setting VPN Shared Secrets in Windows Server 2016
param([string]$password)
$encrypted = ConvertTo-SecureString $password -AsPlainText -Force
ConvertFrom-SecureString $encrypted
@GeorgDangl
GeorgDangl / MigrateMSTestToXUnit.ps1
Created May 25, 2017 12:42
Migrating MSTest to xUnit
Param(
[string ]$ProjectDir
)
get-childitem $ProjectDir -recurse -include *.cs |
select -expand fullname |
foreach {
( Get-Content $_ ) -replace '\[TestMethod\]','[Fact]' `
-replace 'Assert.AreEqual' , 'Assert.Equal' `
-replace 'Assert.AreNotEqual' , 'Assert.NotEqual' `
-replace 'Assert.IsTrue' , 'Assert.True' `
@GeorgDangl
GeorgDangl / MockHttpClient.cs
Last active October 6, 2021 13:47
Mock an Asp.Net Core HttpClient with a custom HttpMessageHandler using Moq
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;
namespace Tests
{
public class MockHttpClient
@GeorgDangl
GeorgDangl / copySQLiteDlls.ps1
Created June 29, 2017 10:25
Using Microsoft.EntityFrameworkCore.Sqlite in a full .Net framework class library
$SQLitePackages = Join-Path -Path $env:USERPROFILE -ChildPath "\.nuget\packages\SQLite"
$latestSQLite3PackageX64 = Join-Path -Path ((Get-ChildItem -Path $SQLitePackages | Sort-Object Fullname -Descending)[0].FullName) -ChildPath "runtimes\win7-x64\native\sqlite3.dll"
$latestSQLite3PackageX86 = Join-Path -Path ((Get-ChildItem -Path $SQLitePackages | Sort-Object Fullname -Descending)[0].FullName) -ChildPath "runtimes\win7-x86\native\sqlite3.dll"
if (!(Test-Path "$PSScriptRoot\Dependencies")){
New-Item -ItemType Directory -Path "$PSScriptRoot\Dependencies"
}
if (!(Test-Path "$PSScriptRoot\Dependencies\x86")){
New-Item -ItemType Directory -Path "$PSScriptRoot\Dependencies\x86"
}
if (!(Test-Path "$PSScriptRoot\Dependencies\x64")){