Skip to content

Instantly share code, notes, and snippets.

@postb99
postb99 / xmlserialization.cs
Created August 8, 2019 12:15
XML serialization of any class you cannot annotate
// For example classes generated by adding a reference to a WCF service.
// IEnumerable and arrays are tricky to serialize to xml, so here is the tip.
// First, I build a dictionary and put xml serialiers instance like this:
// _xmlSerializers.Add(typeof(SomeType), new XmlSerializer(typeof(SomeType), "a namespace"));
// _xmlSerializers.Add(typeof(List<SomeType>), new XmlSerializer(typeof(List<SomeType>), "a namespace"));
// Then te code to serialize a class:
string serialized = null;
bool serializerFound = false;
using (var xwriter = new Utf8StringWriter())
@postb99
postb99 / model binder.cs
Last active April 1, 2019 13:56
Custom model binding to add XmlSerializer deserialization errors to model state (asp .net core mvc)
// Thanks to : https://www.dotnetcurry.com/aspnet-mvc/1261/custom-model-binder-aspnet-mvc
// and https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-2.2
// model binder
public class SmartXmlModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
try
{
@postb99
postb99 / gist:7d1389f90ccf2fbf17ff0f7e1a2f2458
Created January 14, 2019 20:53
VS Code workbench custom colors settings
"workbench.colorCustomizations": {
"statusBar.background": "#666666",
"titleBar.activeBackground": "#252526",
"statusBar.foreground": "#ffffff",
"statusBarItem.hoverBackground": "#dbd805a4",
"editor.lineHighlightBorder": "#363636",
"editor.lineHighlightBackground": "#363636",
"activityBar.background": "#007ACC",
"activityBar.border": "#003c64",
"activityBarBadge.foreground": "#ffffff",
@postb99
postb99 / Startup.cs
Created August 17, 2018 13:50
Multiple auth schemes in .NET Core 2.0
https://github.com/aspnet/Security/issues/1469
JWT token if any in request header, then OpenIdConnect (Azure AD) or anything else.
public void ConfigureServices(IServiceCollection services)
{
// Add CORS
services.AddCors();
// Add authentication before adding MVC
@postb99
postb99 / gist:a2f38c9352790ae0ff0d2f4977cbf23e
Created April 17, 2018 10:49
Add adal token renewal to App startup
export default Vue.extend({
name: 'app',
created: function() {
// Add an utility method to check token on startup
// because the necessary renewal when expired etc isn't handled for now by vue-adal package.
// (see a comment in https://github.com/survirtual/vue-adal/issues/2).
const resource = AuthenticationContext.config.clientId
AuthenticationContext.acquireToken(resource, (err: any, token: any) => {
if (err) {
let errCode = err.split(':')[0]
@postb99
postb99 / gist:9a29f6cac2e6e9d5907412867b42cafe
Created April 6, 2018 12:07
Useful Visual Studio extensions
Better Comments
Viasfora
Whack Whack Terminal
VSColorOutput ou Output enhancer
SQLite/SQL Server Compact Toolbox
JSON Viewer
Vue.js Pack 2017
Trailling White Space
Local History for Visual Studio
Bundler & Minifier
<template>
<div v-if="isAuthenticated" id="app" class="container-fluid">
<div id="mainTitle">
Hello World
</div>
<router-view/>
</div>
</template>
@postb99
postb99 / Mapping for Identity
Created December 17, 2017 21:40
Mapping for Identity
modelBuilder_.Entity<User>() .HasMany(e => e.UserRoles) .WithOne() .HasForeignKey(e => e.UserId) .IsRequired() .OnDelete(DeleteBehavior.Cascade);
modelBuilder_.Entity<User>() .HasMany(e => e.UserRoles) .WithOne() .HasForeignKey(e => e.RoleId) .IsRequired() .OnDelete(DeleteBehavior.Cascade);
modelBuilder_.Entity<Role>() .HasMany(e => e.UserRoles) .WithOne() .HasForeignKey(e => e.RoleId) .IsRequired() .OnDelete(DeleteBehavior.Cascade);
modelBuilder_.Entity<User>() .HasMany(e => e.UserLogins) .WithOne() .HasForeignKey(e => e.UserId) .IsRequired() .OnDelete(DeleteBehavior.Cascade);
modelBuilder_.Entity<User>() .HasMany(e => e.UserTokens) .Wit
@postb99
postb99 / apk.ps1
Created January 8, 2014 16:09
Powershell script to generate APK for Google Play
Param(
[string]$projectPath=$(throw "projectPath is required (full path to .csproj file)"),
[string]$packageName=$(throw "packageName is required"),
[string]$configurationDirName=$(throw "configurationDirName is required"),
[string]$keyAlias=$(throw "keyAlias is required (keystore key alias)")
)
# Parameters are defined at first line of powershell script.
# Thanks to http://docs.xamarin.com/guides/android/deployment%2C_testing%2C_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release for this script
# Parameters :