the overengineer

Feed

Feed Occasional Blog Posts en Unable to run Visual Studio installer or updates /unable-to-run-visual-studio-installer-or-updates <p>Both my laptop and my PC suddenly started crashing when opening Visual Studio projects, and the Visual Studio installer would not properly run on either machine.</p> <p>I was getting this error in my <code class="highlighter-rouge">ActivityLog.xml</code>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>System.InvalidOperationException: Controller terminated before accepting connections. Exit code: -1073741819 </code></pre></div></div> <p>Welp, it turns out it was having the <code class="highlighter-rouge">NODE_OPTIONS</code> environmnent variable set to <code class="highlighter-rouge">--max-old-space-size=4096</code>.</p> <p>I removed this (System) environment variable and suddenly everything worked. Go figure.</p> <p>More info here:</p> <p>https://developercommunity.visualstudio.com/content/problem/287499/systeminvalidoperationexception-controller-termina.html</p> <p>And apparently here:</p> <p>https://github.com/electron/electron/issues/12695</p> <p>Somebody shoot me.</p> 2018-11-26T00:00:00+00:00 SITE NAME /unable-to-run-visual-studio-installer-or-updates Get list of installed programs in Windows 10 /get-list-of-installed-programs-in-windows-10 <p>I have found to get a <em>complete</em> list of installed programs, I need to combine the <code class="highlighter-rouge">WMIC</code> method and the <code class="highlighter-rouge">PowerShell</code> method:</p> <p>From <code class="highlighter-rouge">PowerShell</code>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize &gt; C:\installed-ps.txt </code></pre></div></div> <p>From <code class="highlighter-rouge">cmd</code> with admin priveleges:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>wmic product get name,version &gt; C:\installed-wmic.txt </code></pre></div></div> <p>You can then manually combine those results to give you a complete (I hope) list.</p> 2018-11-26T00:00:00+00:00 SITE NAME /get-list-of-installed-programs-in-windows-10 Fix "style/TextAppearance.Compat.Notification.Info" missing /fix-style-textappearance-compat-notification-info-missing <p>I found suddenly I was unable to compile Android (using NativeScript), and was receiving a number of errors similar to this:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>"style/TextAppearance.Compat.Notification.Info" missing </code></pre></div></div> <p>#Solution</p> <p>Delete <code class="highlighter-rouge">C:\Users\[username]\.gradle\caches</code>. Rebuild. Happy days (at least for me).</p> 2018-11-18T00:00:00+00:00 SITE NAME /fix-style-textappearance-compat-notification-info-missing Getting leaflet-draw to work in Angular 7 /getting-leaflet-draw-to-work-in-angular-7 <h2 id="issue">Issue</h2> <p>The code works fine in development builds, but when published (in my case with AOT, I didn’t test without), I am getting <code class="highlighter-rouge">l.Control.Draw is not a constructor</code>.</p> <h2 id="solution">Solution</h2> <p>Be prepared. This is uglier than Ted Cruz and Papa John’s bastard baby.</p> <h1 id="first">First</h1> <p>Import <code class="highlighter-rouge">leaflet</code> and <code class="highlighter-rouge">leaflet-draw</code> into your equivalent of <code class="highlighter-rouge">vendors.ts</code>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import 'leaflet'; import 'leaflet-draw'; </code></pre></div></div> <h1 id="second">Second</h1> <p><em>Wherever</em> you have used these imports in your code, replace it with:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>declare const L: any; </code></pre></div></div> <p>If you encounter build errors, you can use <code class="highlighter-rouge">@ts-ignore</code>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// @ts-ignore drawControl: L.Control.Draw; </code></pre></div></div> <p>Et voila.</p> <p>F***ing stupid.</p> 2018-11-09T00:00:00+00:00 SITE NAME /getting-leaflet-draw-to-work-in-angular-7 Fixing "Unable to get repository signature information" on "dotnet pack" or "dotnet restore" /fixing-unable-to-get-repository-signature-information-on-dotnet-pack-or-dotnet-restore <p>I ran into an odd problem today whereby all of a sudden the <code class="highlighter-rouge">dotnet restore</code> command (also a problem if you call <code class="highlighter-rouge">dotnet pack</code>) failed with the following errors:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>C:\Program Files\dotnet\sdk\2.1.300-preview2-008533\NuGet.targets(114,5): error : Unable to get repository signature information for source https://www.myget.org/F/[blahblah]/api/v3/repository-signatures/index.json. [D:\...blah.sln] C:\Program Files\dotnet\sdk\2.1.300-preview2-008533\NuGet.targets(114,5): error : Unable to parse allRepositorySigned information from https://www.myget.org/F/[blahblah]/api/v3/index.json. [D:\...blah.sln] </code></pre></div></div> <p>I tried flushing dns cache, resetting network adapters, clearing <code class="highlighter-rouge">NuGet</code> cache, uninstalling and reinstalling <code class="highlighter-rouge">NuGet</code>, everything.</p> <p>This is what worked:</p> <p>I was calling <code class="highlighter-rouge">dotnet restore</code>/<code class="highlighter-rouge">dotnet pack</code> on the solution file. I ran <code class="highlighter-rouge">dotnet pack</code> on each individual project in the solution from the command line, now it works on the solution file. Go figure.</p> <p>You probably don’t need to run it on every project, I imagine one will do, but I’m unable to test that as now everything is working. I have confirmed it is working by clearing my <code class="highlighter-rouge">NuGet</code> cache again, cloning a new version of my repository and running <code class="highlighter-rouge">dotnet pack</code>, and the restore runs fine once again.</p> <p>As an aside, pinging <code class="highlighter-rouge">www.myget.org</code> on the problem machine returns <code class="highlighter-rouge">myget-www-prod-eu-west.cloudapp.net</code>. Not sure if this has anything to do with it (some cross domain restriction, perhaps?). But then again, now pack works and I can see in the logs the restore took place with <code class="highlighter-rouge">dotnet </code></p> 2018-05-07T00:00:00+00:00 SITE NAME /fixing-unable-to-get-repository-signature-information-on-dotnet-pack-or-dotnet-restore Fixing NativeScript's "Xcode is not installed or is not configured properly" on MacOS /fixing-nativescript-s-xcode-is-not-installed-or-is-not-configured-properly-on-macos <h1 id="run-the-following-command">Run the following command:</h1> <p><code class="highlighter-rouge">sudo xcode-select --reset</code></p> <h1 id="why-does-this-fix-it">Why does this fix it?</h1> <p>As <a href="https://github.com/NativeScript/nativescript-cli/issues/1064#issuecomment-275924430">GitHub user @thowerton explains</a>:</p> <blockquote> <p>“The default path for Xcode when installing Xcode command line tools was incorrect. You can confirm if this is your issue by running $ xcodebuild , which will show an error like this if this is your issue:</p> <p>xcode-select: error: tool ‘xcodebuild’ requires Xcode, but active developer directory ‘/Library/Developer/CommandLineTools’ is a command line tools instance”</p> </blockquote> <p>The command above resets this.</p> 2017-08-07T00:00:00+00:00 SITE NAME /fixing-nativescript-s-xcode-is-not-installed-or-is-not-configured-properly-on-macos VS Code: How to set a keyboard shortcut to focus the active file in file explorer /vs-code-how-to-set-a-keyboard-shortcut-to-focus-the-active-file-in-file-explorer <p>Simple.</p> <p>Add this to your <code class="highlighter-rouge">keybindings.json</code>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{ "key": "shift+alt+l", "command": "workbench.files.action.focusFilesExplorer" } </code></pre></div></div> <p>This works just like ReSharper’s <code class="highlighter-rouge">Shift+Alt+L</code> binding in Visual Studio.</p> 2017-02-23T00:00:00+00:00 SITE NAME /vs-code-how-to-set-a-keyboard-shortcut-to-focus-the-active-file-in-file-explorer SqlException: A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 15 - Function not supported) /sqlexception-a-transport-level-error-has-occurred-when-receiving-results-from-the-server-provider-shared-memory-provider-error-15-function-not-supported <p>This error might come after a particular SQL Server Windows Update (KB3197875 and KB3196684, listed as KB3195387 in installed updates).</p> <p>A common suggestion is to install .NET 4.6.2, but for me this didn’t work as the installer simply told me that this version of the framework was alrady installed.</p> <p>The solution for me was to open <em>SQL Server Configuration Manager</em>:</p> <p><img src="/media/2017-02-06 02_31_32-Cortana2.png" alt="Abc" width="350px" /></p> <p>Then navigate to the <em>SQL Server Network Configuration</em> and disable “Shared Memory”, but make sure you leave one of the others on so SQL Server can communicate <strong>somehow</strong>:</p> <p><img src="/media/2017-02-06 03_04_27-Manage your account - Hazception.png" alt="2017-02-06 03_04_27-Manage your account - Hazception.png" /></p> <p>Then just head over to your SQL Server instance and restart it:</p> <p><img src="/media/2017-02-06 03_30_12-the overengineer.png" alt="2017-02-06 03_30_12-the overengineer.png" /></p> <p>Now re-run your app, things should be cushty.</p> 2017-02-06T00:00:00+00:00 SITE NAME /sqlexception-a-transport-level-error-has-occurred-when-receiving-results-from-the-server-provider-shared-memory-provider-error-15-function-not-supported Hacky, quick-fix debugging of .NET Core in Visual Studio /hacky-quick-fix-debugging-of-net-core-in-visual-studio <p>In the latest .NET Core there is currently no compatible Visual Studio tooling for debugging from within VS.</p> <p>This is due to a shift from DNX to CLI, and on-going work.</p> <p>Anyway, that’s dull, let’s cut to the chase. Want some primitive debugging support to your .NET Core stuff?</p> <h2 id="step-1---install-the-latest-cli">Step 1 - Install the latest CLI</h2> <p><a href="https://dotnetcli.blob.core.windows.net/dotnet/beta/Installers/Latest/dotnet-dev-win-x64.latest.exe">https://dotnetcli.blob.core.windows.net/dotnet/beta/Installers/Latest/dotnet-dev-win-x64.latest.exe</a></p> <h2 id="step-2---install-visual-commander-vs-extension">Step 2 - Install Visual Commander VS extension</h2> <p>Install: <a href="https://visualstudiogallery.msdn.microsoft.com/deda8ac1-75e6-4068-89ab-b607cee38f2d">https://visualstudiogallery.msdn.microsoft.com/deda8ac1-75e6-4068-89ab-b607cee38f2d</a></p> <p>Info: <a href="https://vlasovstudio.com/visual-commander/">https://vlasovstudio.com/visual-commander/</a></p> <h2 id="step-3---import-my-debug-and-run-commands">Step 3 - Import my debug and run commands</h2> <p><a href="https://www.dropbox.com/s/tq8bqp00pvja3w6/netcoredebugging.vcmd?dl=0">https://www.dropbox.com/s/tq8bqp00pvja3w6/netcoredebugging.vcmd?dl=0</a></p> <p>The (current at the time of writing) code for the commands is below.</p> <p>The code for both are identical, with the exception that the following line is commented out in the “Run” version:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"> <span class="c1">// Comment out this line if you want just "Run" functionality</span> <span class="n">proc</span><span class="p">.</span><span class="nf">Attach</span><span class="p">();</span></code></pre></figure> <p>This is the line that attaches VS debugging to the process, so without it it’s just <code class="highlighter-rouge">Ctrl+F5</code> type “Run”.</p> <p>The commands expect to find <code class="highlighter-rouge">Newtonsoft.Json.dll</code> somewhere, notably here:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>C:\Program Files (x86)\Common Files\Microsoft Shared\VsHub\1.0.0.0\lib\Newtonsoft.Json.dll </code></pre></div></div> <p>You can edit the commands yourself if you want to change the location of that DLL (or anything else).</p> <p>Finally, if you want to have debugging right from your entry point (e.g. <code class="highlighter-rouge">Program.cs</code>) you need to add this:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public static void Main(string[] args) { args = DotNetRunner.AppStart(args); ... } </code></pre></div></div> <p>Where the <code class="highlighter-rouge">DotNetRunner.cs</code> looks like this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">public</span> <span class="k">static</span> <span class="k">class</span> <span class="nc">DotNetRunner</span> <span class="p">{</span> <span class="k">public</span> <span class="k">static</span> <span class="kt">string</span><span class="p">[]</span> <span class="nf">AppStart</span><span class="p">(</span><span class="k">params</span> <span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">newArgs</span> <span class="p">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;();</span> <span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">Length</span> <span class="p">&gt;</span> <span class="m">0</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">prefix</span> <span class="p">=</span> <span class="s">"wait:"</span><span class="p">;</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">arg</span> <span class="k">in</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">arg</span><span class="p">.</span><span class="nf">StartsWith</span><span class="p">(</span><span class="n">prefix</span><span class="p">))</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">wait</span> <span class="p">=</span> <span class="n">Int32</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="n">arg</span><span class="p">.</span><span class="nf">Substring</span><span class="p">(</span><span class="n">prefix</span><span class="p">.</span><span class="n">Length</span><span class="p">));</span> <span class="n">Thread</span><span class="p">.</span><span class="nf">Sleep</span><span class="p">(</span><span class="n">wait</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">newArgs</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="n">arg</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">newArgs</span><span class="p">.</span><span class="nf">ToArray</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <p>This is a very, VERY hacky hack hack that simply tells the app to sleep for 5 seconds, otherwise we don’t attach debugging in time to jump into the entry point’s code. You can adjust the 5 seconds to someting else as suited by changing the “wait:5000” in the command code to something else if you need. Yes, it’s horrible, truly horrible, but until the Visual Studio CLI tooling is here, it’s fine.</p> <p>So, overall this is VERY primitive, but here’s what it does:</p> <ul> <li>It saves all documents open in VS first, just like a normal build</li> <li>It finds the current startup project</li> <li>It tries to find the <code class="highlighter-rouge">launchSettings.json</code> for the current startup project and looks for a “launchUrl” specifically at the following JSON path: <code class="highlighter-rouge">\profiles\IIS Express\launchUrl</code></li> <li>It publishes the app to: <code class="highlighter-rouge">[project-path]\..\.vcmd\[project-name]</code>, so be sure to add <code class="highlighter-rouge">.vcmd/</code> to your <code class="highlighter-rouge">.gitignore</code></li> <li>It also tries to call an <code class="highlighter-rouge">onbeforerun.bat</code> and an <code class="highlighter-rouge">onrun.bat</code> so you can inject some stuff if you want</li> <li>It calls <code class="highlighter-rouge">dotnet restore</code> and then <code class="highlighter-rouge">dotnet build</code> <em>every</em> time</li> <li>It receives the output from your exe and looks for <code class="highlighter-rouge">Now listening on:</code> as a cue to launch the launch URL</li> </ul> <h2 id="code-for-the-visual-commander-commands">Code for the Visual Commander command(s):</h2> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="nn">EnvDTE</span><span class="p">;</span> <span class="k">using</span> <span class="nn">EnvDTE80</span><span class="p">;</span> <span class="k">using</span> <span class="nn">System</span><span class="p">;</span> <span class="k">using</span> <span class="nn">System.IO</span><span class="p">;</span> <span class="k">using</span> <span class="nn">System.Windows.Forms</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Newtonsoft.Json</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Newtonsoft.Json.Linq</span><span class="p">;</span> <span class="k">public</span> <span class="k">class</span> <span class="nc">C</span> <span class="p">:</span> <span class="n">VisualCommanderExt</span><span class="p">.</span><span class="n">ICommand</span> <span class="p">{</span> <span class="k">public</span> <span class="kt">string</span> <span class="n">ProjectPath</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">Run</span><span class="p">(</span><span class="n">EnvDTE80</span><span class="p">.</span><span class="n">DTE2</span> <span class="n">DTE</span><span class="p">,</span> <span class="n">Microsoft</span><span class="p">.</span><span class="n">VisualStudio</span><span class="p">.</span><span class="n">Shell</span><span class="p">.</span><span class="n">Package</span> <span class="n">package</span><span class="p">)</span> <span class="p">{</span> <span class="n">DTE</span><span class="p">.</span><span class="n">Documents</span><span class="p">.</span><span class="nf">SaveAll</span><span class="p">();</span> <span class="kt">var</span> <span class="n">startupProjectFile</span> <span class="p">=</span> <span class="nf">GetStartupProject</span><span class="p">(</span><span class="n">DTE</span><span class="p">);</span> <span class="n">ProjectPath</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetDirectoryName</span><span class="p">(</span><span class="n">startupProjectFile</span><span class="p">);</span> <span class="kt">var</span> <span class="n">projectName</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetFileName</span><span class="p">(</span><span class="n">ProjectPath</span><span class="p">);</span> <span class="kt">var</span> <span class="n">vcmdFolder</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetFullPath</span><span class="p">(</span><span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">ProjectPath</span><span class="p">,</span> <span class="s">"..\\.vcmd"</span><span class="p">));</span> <span class="nf">SetHidden</span><span class="p">(</span><span class="n">vcmdFolder</span><span class="p">);</span> <span class="kt">var</span> <span class="n">outputFolder</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">vcmdFolder</span><span class="p">,</span> <span class="n">projectName</span><span class="p">);</span> <span class="n">outputFolder</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetFullPath</span><span class="p">(</span><span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">ProjectPath</span><span class="p">,</span> <span class="n">outputFolder</span><span class="p">));</span> <span class="kt">var</span> <span class="n">exe</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">outputFolder</span><span class="p">,</span> <span class="n">projectName</span><span class="p">)</span> <span class="p">+</span> <span class="s">".exe"</span><span class="p">;</span> <span class="nf">TryRun</span><span class="p">(</span><span class="s">"onbeforerun.bat"</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> <span class="kt">var</span> <span class="n">existingProcesses</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="n">Process</span><span class="p">.</span><span class="nf">GetProcessesByName</span><span class="p">(</span><span class="n">projectName</span><span class="p">);</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">existingProcess</span> <span class="k">in</span> <span class="n">existingProcesses</span><span class="p">)</span> <span class="p">{</span> <span class="n">existingProcess</span><span class="p">.</span><span class="nf">Kill</span><span class="p">();</span> <span class="p">}</span> <span class="c1">// Call "dotnet restore"</span> <span class="nf">DotNet</span><span class="p">(</span><span class="s">"restore"</span><span class="p">);</span> <span class="c1">// Call "dotnet build"</span> <span class="nf">DotNet</span><span class="p">(</span><span class="s">"publish -c Debug -o \""</span> <span class="p">+</span> <span class="n">outputFolder</span> <span class="p">+</span> <span class="s">"\""</span><span class="p">);</span> <span class="kt">var</span> <span class="n">process</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="nf">Process</span><span class="p">();</span> <span class="n">process</span><span class="p">.</span><span class="n">OutputDataReceived</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">line</span> <span class="p">=</span> <span class="n">args</span><span class="p">.</span><span class="n">Data</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">line</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="kt">var</span> <span class="n">prefix</span> <span class="p">=</span> <span class="s">"Now listening on: "</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">line</span><span class="p">.</span><span class="nf">Trim</span><span class="p">().</span><span class="nf">StartsWith</span><span class="p">(</span><span class="n">prefix</span><span class="p">))</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">url</span> <span class="p">=</span> <span class="n">line</span><span class="p">.</span><span class="nf">Substring</span><span class="p">(</span><span class="n">prefix</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span> <span class="kt">var</span> <span class="n">launchSettingsPath</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">ProjectPath</span><span class="p">,</span> <span class="s">@"properties\launchSettings.json"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">File</span><span class="p">.</span><span class="nf">Exists</span><span class="p">(</span><span class="n">launchSettingsPath</span><span class="p">))</span> <span class="p">{</span> <span class="n">JObject</span> <span class="n">obj</span> <span class="p">=</span> <span class="n">JObject</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="n">File</span><span class="p">.</span><span class="nf">ReadAllText</span><span class="p">(</span><span class="n">launchSettingsPath</span><span class="p">));</span> <span class="kt">var</span> <span class="n">settingsUrl</span> <span class="p">=</span> <span class="s">""</span><span class="p">;</span> <span class="k">try</span> <span class="p">{</span> <span class="n">JToken</span> <span class="n">token</span> <span class="p">=</span> <span class="n">obj</span><span class="p">[</span><span class="s">"profiles"</span><span class="p">][</span><span class="s">"IIS Express"</span><span class="p">][</span><span class="s">"launchUrl"</span><span class="p">];</span> <span class="n">settingsUrl</span> <span class="p">=</span> <span class="n">token</span><span class="p">.</span><span class="n">Value</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;();</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">NullReferenceException</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(!</span><span class="kt">string</span><span class="p">.</span><span class="nf">IsNullOrWhiteSpace</span><span class="p">(</span><span class="n">settingsUrl</span><span class="p">))</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nf">IsAbsoluteUrl</span><span class="p">(</span><span class="n">settingsUrl</span><span class="p">))</span> <span class="p">{</span> <span class="n">url</span> <span class="p">=</span> <span class="n">settingsUrl</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">url</span> <span class="p">=</span> <span class="n">url</span><span class="p">.</span><span class="nf">TrimEnd</span><span class="p">(</span><span class="sc">'/'</span><span class="p">)</span> <span class="p">+</span> <span class="s">"/"</span> <span class="p">+</span> <span class="n">settingsUrl</span><span class="p">.</span><span class="nf">TrimStart</span><span class="p">(</span><span class="sc">'/'</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="n">url</span> <span class="p">=</span> <span class="n">url</span><span class="p">.</span><span class="nf">Trim</span><span class="p">();</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(!</span><span class="kt">string</span><span class="p">.</span><span class="nf">IsNullOrWhiteSpace</span><span class="p">(</span><span class="n">url</span><span class="p">))</span> <span class="p">{</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="n">Process</span><span class="p">.</span><span class="nf">Start</span><span class="p">(</span><span class="s">"explorer"</span><span class="p">,</span> <span class="s">"\""</span> <span class="p">+</span> <span class="n">url</span> <span class="p">+</span> <span class="s">"\""</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">};</span> <span class="kt">var</span> <span class="n">ps</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="nf">ProcessStartInfo</span><span class="p">(</span><span class="n">exe</span><span class="p">,</span> <span class="s">"wait:5000"</span><span class="p">);</span> <span class="n">ps</span><span class="p">.</span><span class="n">RedirectStandardOutput</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="n">ps</span><span class="p">.</span><span class="n">UseShellExecute</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="n">EnableRaisingEvents</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="n">StartInfo</span> <span class="p">=</span> <span class="n">ps</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="nf">Start</span><span class="p">();</span> <span class="n">process</span><span class="p">.</span><span class="nf">BeginOutputReadLine</span><span class="p">();</span> <span class="nf">TryRun</span><span class="p">(</span><span class="s">"onrun.bat"</span><span class="p">);</span> <span class="k">foreach</span> <span class="p">(</span><span class="n">Process</span> <span class="n">proc</span> <span class="k">in</span> <span class="n">DTE</span><span class="p">.</span><span class="n">Debugger</span><span class="p">.</span><span class="n">LocalProcesses</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">proc</span><span class="p">.</span><span class="n">ProcessID</span> <span class="p">==</span> <span class="n">process</span><span class="p">.</span><span class="n">Id</span><span class="p">)</span> <span class="p">{</span> <span class="n">proc</span><span class="p">.</span><span class="nf">Attach</span><span class="p">();</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">void</span> <span class="nf">SetHidden</span><span class="p">(</span><span class="kt">string</span> <span class="n">folder</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(!</span><span class="n">Directory</span><span class="p">.</span><span class="nf">Exists</span><span class="p">(</span><span class="n">folder</span><span class="p">))</span> <span class="p">{</span> <span class="n">Directory</span><span class="p">.</span><span class="nf">CreateDirectory</span><span class="p">(</span><span class="n">folder</span><span class="p">);</span> <span class="p">}</span> <span class="n">DirectoryInfo</span> <span class="n">di</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">DirectoryInfo</span><span class="p">(</span><span class="n">folder</span><span class="p">);</span> <span class="c1">// See if directory has hidden flag, if not, make hidden</span> <span class="k">if</span> <span class="p">((</span><span class="n">di</span><span class="p">.</span><span class="n">Attributes</span> <span class="p">&amp;</span> <span class="n">FileAttributes</span><span class="p">.</span><span class="n">Hidden</span><span class="p">)</span> <span class="p">!=</span> <span class="n">FileAttributes</span><span class="p">.</span><span class="n">Hidden</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Add Hidden flag </span> <span class="n">di</span><span class="p">.</span><span class="n">Attributes</span> <span class="p">|=</span> <span class="n">FileAttributes</span><span class="p">.</span><span class="n">Hidden</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">bool</span> <span class="nf">IsAbsoluteUrl</span><span class="p">(</span><span class="kt">string</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="n">Uri</span> <span class="n">result</span><span class="p">;</span> <span class="k">return</span> <span class="n">Uri</span><span class="p">.</span><span class="nf">TryCreate</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">UriKind</span><span class="p">.</span><span class="n">Absolute</span><span class="p">,</span> <span class="k">out</span> <span class="n">result</span><span class="p">);</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">TryRun</span><span class="p">(</span><span class="kt">string</span> <span class="n">batch</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">waitForExit</span> <span class="p">=</span> <span class="k">false</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">onrun</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">ProjectPath</span><span class="p">,</span> <span class="n">batch</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">File</span><span class="p">.</span><span class="nf">Exists</span><span class="p">(</span><span class="n">onrun</span><span class="p">))</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">process</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="nf">Process</span><span class="p">();</span> <span class="kt">var</span> <span class="n">psi</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="nf">ProcessStartInfo</span><span class="p">(</span><span class="n">onrun</span><span class="p">);</span> <span class="n">psi</span><span class="p">.</span><span class="n">WorkingDirectory</span> <span class="p">=</span> <span class="n">ProjectPath</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="n">StartInfo</span> <span class="p">=</span> <span class="n">psi</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="nf">Start</span><span class="p">();</span> <span class="k">if</span> <span class="p">(</span><span class="n">waitForExit</span><span class="p">)</span> <span class="p">{</span> <span class="n">process</span><span class="p">.</span><span class="nf">WaitForExit</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">DotNet</span><span class="p">(</span><span class="kt">string</span> <span class="n">command</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">process</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="nf">Process</span><span class="p">();</span> <span class="kt">var</span> <span class="n">ps</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Diagnostics</span><span class="p">.</span><span class="nf">ProcessStartInfo</span><span class="p">(</span><span class="s">"cmd"</span><span class="p">,</span> <span class="s">"/k call dotnet "</span> <span class="p">+</span> <span class="n">command</span> <span class="p">+</span> <span class="s">" &amp; exit"</span><span class="p">);</span> <span class="n">ps</span><span class="p">.</span><span class="n">WorkingDirectory</span> <span class="p">=</span> <span class="n">ProjectPath</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="n">StartInfo</span> <span class="p">=</span> <span class="n">ps</span><span class="p">;</span> <span class="n">process</span><span class="p">.</span><span class="nf">Start</span><span class="p">();</span> <span class="n">process</span><span class="p">.</span><span class="nf">WaitForExit</span><span class="p">();</span> <span class="p">}</span> <span class="k">public</span> <span class="kt">string</span> <span class="nf">GetStartupProject</span><span class="p">(</span><span class="n">EnvDTE80</span><span class="p">.</span><span class="n">DTE2</span> <span class="n">DTE</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">solutionPath</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetDirectoryName</span><span class="p">(</span><span class="n">DTE</span><span class="p">.</span><span class="n">Solution</span><span class="p">.</span><span class="n">FullName</span><span class="p">);</span> <span class="kt">var</span> <span class="n">projectFile</span> <span class="p">=</span> <span class="s">""</span><span class="p">;</span> <span class="k">foreach</span> <span class="p">(</span><span class="n">String</span> <span class="n">s</span> <span class="k">in</span> <span class="p">(</span><span class="n">Array</span><span class="p">)</span><span class="n">DTE</span><span class="p">.</span><span class="n">Solution</span><span class="p">.</span><span class="n">SolutionBuild</span><span class="p">.</span><span class="n">StartupProjects</span><span class="p">)</span> <span class="p">{</span> <span class="n">projectFile</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">solutionPath</span><span class="p">,</span> <span class="n">s</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">projectFile</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> 2016-05-05T00:00:00+00:00 SITE NAME /hacky-quick-fix-debugging-of-net-core-in-visual-studio Fixing nested file names with the node_modules folder in Visual Studio /fixing-nested-file-names-with-the-node-modules-folder-in-visual-studio <p>If you get this:</p> <p><code class="highlighter-rouge">The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.</code></p> <p>Then I recommend following these steps:</p> <h2 id="ensure-you-have-the-latest-nodejs">Ensure you have the latest node.js</h2> <p><a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a></p> <h2 id="ensure-you-have-the-latest-version-of-npm-node-package-manager">Ensure you have the latest version of npm (Node Package Manager)</h2> <p>From cmd:</p> <p><code class="highlighter-rouge">npm install -g npm</code></p> <p>This should ensure the <code class="highlighter-rouge">node_modules</code> folder is now served flat not fat.</p> <h2 id="ensure-visual-studios-preference-for-the-node_modules-folder-is-global-first">Ensure Visual Studio’s preference for the node_modules folder is “global first”</h2> <p><code class="highlighter-rouge">Tools -&gt; Options -&gt; Project and Solutions -&gt; External Web Tools</code> -&gt; move $(PATH) up on level so it is on top of $(DevEnvDir):</p> <p><img src="/media/2016-05-05 15_02_05-Options.png" alt="2016-05-05 15_02_05-Options.png" /></p> <p>That’s it, you should have the problem solved.</p> 2016-05-05T00:00:00+00:00 SITE NAME /fixing-nested-file-names-with-the-node-modules-folder-in-visual-studio Generating new Entity Framework migrations when dotnet-ef is broken /generating-new-entity-framework-migrations-when-dotnet-ef-is-broken <p><code class="highlighter-rouge">dotnet-ef</code> is a handy too to generate Entity Framework migrations and save them to disk.</p> <p>Due to the instability of the .NET Core RC2 feed at the moment, it often doesn’t work.</p> <p>However we can generate migrations manually using the <code class="highlighter-rouge">Microsoft.EntityFrameworkCore.Commands</code> package, which is what <code class="highlighter-rouge">dotnet-ef</code> relies on.</p> <p>I have put such a package together.</p> <p>Right now it only is able to add a migration.</p> <h3 id="source-code-can-be-found-here">Source code can be found here</h3> <p><a href="https://github.com/joshcomley/BrandlessOpen/tree/master/Code/dnx/Brandless.EntityFrameworkCore.Migrations">https://github.com/joshcomley/BrandlessOpen/tree/master/Code/dnx/Brandless.EntityFrameworkCore.Migrations</a></p> <h3 id="nuget-package-can-be-found-here">Nuget package can be found here</h3> <p><a href="https://www.nuget.org/packages/Brandless.EntityFrameworkCore.Migrations">https://www.nuget.org/packages/Brandless.EntityFrameworkCore.Migrations</a></p> <h3 id="usage">Usage</h3> <p>1 - Create a new console application in the same solution as your data context, and reference the project that contains your data context in your new console app.</p> <p>2 - In your <code class="highlighter-rouge">Main()</code> method, add the below code, exchanging the variables to suit your environment:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="kt">var</span> <span class="n">projectPath</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetFullPath</span><span class="p">(</span><span class="n">Path</span><span class="p">.</span><span class="nf">Combine</span><span class="p">(</span><span class="n">AppContext</span><span class="p">.</span><span class="n">BaseDirectory</span><span class="p">,</span> <span class="s">@"..\YourApp.DataContext"</span><span class="p">));</span> <span class="kt">var</span> <span class="n">codeFirstMigrations</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CodeFirstMigrations</span><span class="p">&lt;</span><span class="n">YourDbContext</span><span class="p">&gt;(</span><span class="n">projectPath</span><span class="p">);</span> <span class="n">codeFirstMigrations</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="s">"NameOfTheMigration"</span><span class="p">);</span></code></pre></figure> <p>3 - Run the console app, you should have a screen a bit like this:</p> <p><img src="/media/2016-03-22 20_11_51-C__Windows_system32_cmd.exe.png" alt="2016-03-22 20_11_51-C__Windows_system32_cmd.exe.png" /></p> <p>Feel free to contribute to the open source project with pull requests, adding other tools from the Entity Framework commands as you might need. I am sure I’ll add a few over time.</p> <p>Enjoy!</p> 2016-03-22T00:00:00+00:00 SITE NAME /generating-new-entity-framework-migrations-when-dotnet-ef-is-broken Prevent form validation on ng-click in AngularJS /prevent-form-validation-on-ng-click-in-angularjs <p>With the following HTML, validation on your form will be triggered every time you click on either button:</p> <figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt">&lt;form</span> <span class="na">ng-submit=</span><span class="s">"updateTitle()"</span><span class="nt">&gt;</span> <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">ng-model=</span><span class="s">"title"</span> <span class="na">required</span> <span class="nt">/&gt;</span> <span class="nt">&lt;button&gt;</span>Update<span class="nt">&lt;/button&gt;</span> <span class="nt">&lt;button</span> <span class="na">ng-click=</span><span class="s">"alert('hi!')"</span><span class="nt">&gt;</span>Say hi<span class="nt">&lt;/button&gt;</span> <span class="nt">&lt;/form&gt;</span></code></pre></figure> <p>But we don’t necessarily want that to happen for our “Say hi” button. So how do we stop it?</p> <p>Well, according to the <a href="https://www.w3.org/TR/html401/interact/forms.html#h-17.5">W3 specification on <code class="highlighter-rouge">button</code> elements</a>, if you don’t specify a <code class="highlighter-rouge">type="[something]"</code> for your button, then it will default to <code class="highlighter-rouge">type="submit"</code>:</p> <p><img src="/media/2016-02-09 16_52_30-Forms in HTML documents.png" width="700" /></p> <p>In such a case, any click to this button, no matter what <code class="highlighter-rouge">ng-click</code> says, will submit the form. And submitting the form will trigger validation. The solution? Make sure it’s type isn’t <code class="highlighter-rouge">submit</code>:</p> <figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt">&lt;form</span> <span class="na">ng-submit=</span><span class="s">"updateTitle()"</span><span class="nt">&gt;</span> <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">ng-model=</span><span class="s">"title"</span> <span class="na">required</span> <span class="nt">/&gt;</span> <span class="nt">&lt;button&gt;</span>Update<span class="nt">&lt;/button&gt;</span> <span class="nt">&lt;button</span> <span class="na">type=</span><span class="s">"button"</span> <span class="na">ng-click=</span><span class="s">"alert('hi!')"</span><span class="nt">&gt;</span>Say hi<span class="nt">&lt;/button&gt;</span> <span class="nt">&lt;/form&gt;</span></code></pre></figure> <p>The form is no longer submitted on click, and we merrily say “hi!” to our users without a validation care in the world.</p> <p>Happy coding!</p> 2016-02-09T00:00:00+00:00 SITE NAME /prevent-form-validation-on-ng-click-in-angularjs Creating a simple proxy server middleware in ASP.NET Core /creating-a-simple-proxy-server-middleware-in-asp-net-core <p>Sometimes it is useful, especially for myself under certain development environment conditions, to use a proxy server.</p> <p>Below is an implementation you can adapt to your needs.</p> <p><em>Suppose we have a file at <strong>http://cdn.myapp.com/movie.mp4</strong></em></p> <p><em>Suppose we have a web app at <strong>http://www.myapp.com/</strong></em></p> <p><em>Suppose we want to have the <strong>web app</strong> serve a file from the CDN.</em></p> <p>In this case, this middleware proxy would work by making a request to:</p> <p><strong><em>http://www.myapp.com/proxy?url=http://cdn.myapp.com/movie.mp4</em></strong></p> <p>In my case this was to bypass security restrictions that were only applicable in development environments. As such, this proxy has not been tested thoroughly for performance etc., as I will never use it in production. Feel free to contribute suggestions in the comments.</p> <p>This is very open ended, and could leave your server open to easy DDOS attacks by pointing the proxy endpoint to a huge file anywhere on the internet. If you were to use this in production, the first thing I would recommend would be to change the proxy endpoint to work like this:</p> <p><strong>http://www.myapp.com/proxy/movie.mp4</strong></p> <p>The proxy code itself would then contain the logic to convert the above web app URL to the correct CDN one, rather than just taking absolutely any old URL.</p> <p>Here is the code.</p> <h1 id="projectjson">project.json</h1> <p>This middleware uses <code class="highlighter-rouge">HttpClient</code> that is available in <code class="highlighter-rouge">System.Net.Http</code>, so add this line to your dependencies in your <code class="highlighter-rouge">project.json</code>:</p> <figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="w"> </span><span class="s2">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s2">"System.Net.Http"</span><span class="p">:</span><span class="w"> </span><span class="s2">"4.0.1-*"</span><span class="w"> </span><span class="p">},</span></code></pre></figure> <h1 id="middleware-class-itself">Middleware class itself</h1> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="nn">System</span><span class="p">;</span> <span class="k">using</span> <span class="nn">System.IO</span><span class="p">;</span> <span class="k">using</span> <span class="nn">System.Net.Http</span><span class="p">;</span> <span class="k">using</span> <span class="nn">System.Threading.Tasks</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Microsoft.AspNet.Http</span><span class="p">;</span> <span class="k">namespace</span> <span class="nn">YourWebApp.Middleware.Proxy</span> <span class="p">{</span> <span class="k">public</span> <span class="k">class</span> <span class="nc">ProxyServerMiddleware</span> <span class="p">{</span> <span class="k">private</span> <span class="k">readonly</span> <span class="n">RequestDelegate</span> <span class="n">_next</span><span class="p">;</span> <span class="k">public</span> <span class="nf">ProxyServerMiddleware</span><span class="p">(</span><span class="n">RequestDelegate</span> <span class="n">next</span><span class="p">)</span> <span class="p">{</span> <span class="n">_next</span> <span class="p">=</span> <span class="n">next</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="k">async</span> <span class="n">Task</span> <span class="nf">Invoke</span><span class="p">(</span><span class="n">HttpContext</span> <span class="n">context</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">endRequest</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">context</span><span class="p">.</span><span class="n">Request</span><span class="p">.</span><span class="n">Path</span><span class="p">.</span><span class="n">Value</span><span class="p">.</span><span class="nf">Equals</span><span class="p">(</span><span class="s">"/proxy"</span><span class="p">,</span> <span class="n">StringComparison</span><span class="p">.</span><span class="n">OrdinalIgnoreCase</span><span class="p">))</span> <span class="p">{</span> <span class="k">const</span> <span class="kt">string</span> <span class="n">key</span> <span class="p">=</span> <span class="s">"url"</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">context</span><span class="p">.</span><span class="n">Request</span><span class="p">.</span><span class="n">Query</span><span class="p">.</span><span class="nf">ContainsKey</span><span class="p">(</span><span class="n">key</span><span class="p">))</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">url</span> <span class="p">=</span> <span class="n">context</span><span class="p">.</span><span class="n">Request</span><span class="p">.</span><span class="n">Query</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="m">0</span><span class="p">];</span> <span class="k">await</span> <span class="nf">StreamAsync</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">url</span><span class="p">);</span> <span class="n">endRequest</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(!</span><span class="n">endRequest</span><span class="p">)</span> <span class="p">{</span> <span class="k">await</span> <span class="nf">_next</span><span class="p">(</span><span class="n">context</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="k">private</span> <span class="k">static</span> <span class="k">async</span> <span class="n">Task</span> <span class="nf">StreamAsync</span><span class="p">(</span><span class="n">HttpContext</span> <span class="n">context</span><span class="p">,</span> <span class="kt">string</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">httpClientHandler</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpClientHandler</span> <span class="p">{</span> <span class="n">AllowAutoRedirect</span> <span class="p">=</span> <span class="k">false</span> <span class="p">};</span> <span class="kt">var</span> <span class="n">webRequest</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">HttpClient</span><span class="p">(</span><span class="n">httpClientHandler</span><span class="p">);</span> <span class="kt">var</span> <span class="n">buffer</span> <span class="p">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="p">[</span><span class="m">4</span><span class="p">*</span><span class="m">1024</span><span class="p">];</span> <span class="kt">var</span> <span class="n">localResponse</span> <span class="p">=</span> <span class="n">context</span><span class="p">.</span><span class="n">Response</span><span class="p">;</span> <span class="k">try</span> <span class="p">{</span> <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">remoteStream</span> <span class="p">=</span> <span class="k">await</span> <span class="n">webRequest</span><span class="p">.</span><span class="nf">GetStreamAsync</span><span class="p">(</span><span class="n">url</span><span class="p">))</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">bytesRead</span> <span class="p">=</span> <span class="n">remoteStream</span><span class="p">.</span><span class="nf">Read</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">buffer</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span> <span class="n">localResponse</span><span class="p">.</span><span class="nf">Clear</span><span class="p">();</span> <span class="n">localResponse</span><span class="p">.</span><span class="n">ContentType</span> <span class="p">=</span> <span class="s">"application/octet-stream"</span><span class="p">;</span> <span class="kt">var</span> <span class="n">fileName</span> <span class="p">=</span> <span class="n">Path</span><span class="p">.</span><span class="nf">GetFileName</span><span class="p">(</span><span class="n">url</span><span class="p">);</span> <span class="n">localResponse</span><span class="p">.</span><span class="n">Headers</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="s">"Content-Disposition"</span><span class="p">,</span> <span class="s">"attachment; filename="</span> <span class="p">+</span> <span class="n">fileName</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">remoteStream</span><span class="p">.</span><span class="n">Length</span> <span class="p">!=</span> <span class="p">-</span><span class="m">1</span><span class="p">)</span> <span class="n">localResponse</span><span class="p">.</span><span class="n">ContentLength</span> <span class="p">=</span> <span class="n">remoteStream</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">bytesRead</span> <span class="p">&gt;</span> <span class="m">0</span><span class="p">)</span> <span class="c1">// &amp;&amp; localResponse.IsClientConnected)</span> <span class="p">{</span> <span class="k">await</span> <span class="n">localResponse</span><span class="p">.</span><span class="n">Body</span><span class="p">.</span><span class="nf">WriteAsync</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">bytesRead</span><span class="p">);</span> <span class="n">bytesRead</span> <span class="p">=</span> <span class="n">remoteStream</span><span class="p">.</span><span class="nf">Read</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">buffer</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Do some logging here</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <h1 id="extension-method-for-use-in-startupcs">Extension method for use in <code class="highlighter-rouge">Startup.cs</code></h1> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="nn">Microsoft.AspNet.Builder</span><span class="p">;</span> <span class="k">namespace</span> <span class="nn">YourWebApp.Middleware.Proxy</span> <span class="p">{</span> <span class="k">public</span> <span class="k">static</span> <span class="k">class</span> <span class="nc">ProxyServerMiddlewareExtension</span> <span class="p">{</span> <span class="k">public</span> <span class="k">static</span> <span class="n">IApplicationBuilder</span> <span class="nf">UseProxyServer</span><span class="p">(</span><span class="k">this</span> <span class="n">IApplicationBuilder</span> <span class="n">builder</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">builder</span><span class="p">.</span><span class="nf">Use</span><span class="p">(</span><span class="n">next</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="nf">ProxyServerMiddleware</span><span class="p">(</span><span class="n">next</span><span class="p">).</span><span class="n">Invoke</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <h1 id="startupcs">Startup.cs</h1> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"> <span class="k">public</span> <span class="k">void</span> <span class="nf">Configure</span><span class="p">(</span><span class="n">IApplicationBuilder</span> <span class="n">app</span><span class="p">,</span> <span class="n">IHostingEnvironment</span> <span class="n">env</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Add this to the top of your Configure method</span> <span class="c1">// I am using this dev environment check because I don't recommend enabling</span> <span class="c1">// this in a production environment</span> <span class="k">if</span> <span class="p">(</span><span class="n">env</span><span class="p">.</span><span class="nf">IsDevelopment</span><span class="p">())</span> <span class="p">{</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseProxyServer</span><span class="p">();</span> <span class="p">}</span> <span class="p">...</span> <span class="p">}</span></code></pre></figure> 2016-02-07T00:00:00+00:00 SITE NAME /creating-a-simple-proxy-server-middleware-in-asp-net-core Preventing AngularJS from caching your templates and other files /preventing-angularjs-from-caching-your-templates-and-other-files <p>In development it can be very useful to be able to ensure that your server files aren’t cached.</p> <p>Here is how to achieve this with <code class="highlighter-rouge">AngularJS</code> by using a request <a href="https://docs.angularjs.org/api/ng/service/$http#interceptors">interceptor</a> to intercept requests to the server and inject a no-cache request parameter where required.</p> <p>First of all, let’s create our <code class="highlighter-rouge">AngularJS</code> interceptor:</p> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="s2">"use strict"</span><span class="p">;</span> <span class="cm">/** * Intercepts HTTP requests from Angular and applies a no-cache request parameter * to ensure the request will be unique */</span> <span class="nx">angular</span><span class="p">.</span><span class="nx">module</span><span class="p">(</span><span class="s2">"hazceptionApp"</span><span class="p">)</span> <span class="p">.</span><span class="nx">factory</span><span class="p">(</span><span class="s2">"noCacheInterceptor"</span><span class="p">,</span> <span class="p">[</span> <span class="s2">"$log"</span><span class="p">,</span> <span class="s2">"$injector"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">$log</span><span class="p">,</span> <span class="nx">$injector</span><span class="p">)</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">noCacheFolders</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"views"</span><span class="p">,</span> <span class="s2">"scripts"</span><span class="p">,</span> <span class="s2">"lib"</span><span class="p">];</span> <span class="kd">var</span> <span class="nx">shouldApplyNoCache</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">config</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// The logic in here can be anything you like, filtering on</span> <span class="c1">// the HTTP method, the URL, even the request body etc.</span> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">noCacheFolders</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">folder</span> <span class="o">=</span> <span class="nx">noCacheFolders</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">url</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">folder</span> <span class="o">+</span> <span class="s2">"/"</span><span class="p">)</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">true</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span> <span class="p">};</span> <span class="kd">var</span> <span class="nx">applyNoCache</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">config</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">url</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">"?"</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="nx">config</span><span class="p">.</span><span class="nx">url</span> <span class="o">+=</span> <span class="s2">"&amp;"</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="nx">config</span><span class="p">.</span><span class="nx">url</span> <span class="o">+=</span> <span class="s2">"?"</span><span class="p">;</span> <span class="p">}</span> <span class="nx">config</span><span class="p">.</span><span class="nx">url</span> <span class="o">+=</span> <span class="s2">"nocache="</span> <span class="o">+</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">().</span><span class="nx">getTime</span><span class="p">();</span> <span class="p">};</span> <span class="kd">var</span> <span class="nx">interceptor</span> <span class="o">=</span> <span class="p">{</span> <span class="na">request</span><span class="p">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">config</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nx">shouldApplyNoCache</span><span class="p">(</span><span class="nx">config</span><span class="p">))</span> <span class="p">{</span> <span class="nx">applyNoCache</span><span class="p">(</span><span class="nx">config</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="nx">config</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> <span class="k">return</span> <span class="nx">interceptor</span><span class="p">;</span> <span class="p">}]);</span></code></pre></figure> <p>This interceptor is very simple and can be easily adapted for your needs:</p> <p>1 - Use <code class="highlighter-rouge">shouldApplyNoCache()</code> to check if this particular request should have caching eliminated 2 - If it does, call <code class="highlighter-rouge">applyNoCache()</code> 3 - ….that’s it. But three steps is always nicer, right?</p> <p>I suppose the third step is to add the interceptor to our app configuration:</p> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">angular</span> <span class="p">.</span><span class="nx">module</span><span class="p">(</span><span class="s2">"yourApp"</span><span class="p">)</span> <span class="p">.</span><span class="nx">config</span><span class="p">(</span><span class="kd">function</span> <span class="p">(...,</span> <span class="nx">$httpProvider</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Make sure we only apply this during development</span> <span class="c1">// You can adjust this check as is relevant to your development environment</span> <span class="k">if</span> <span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">host</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="nx">$httpProvider</span><span class="p">.</span><span class="nx">interceptors</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s2">"noCacheTemplateInterceptor"</span><span class="p">);</span> <span class="p">}</span> <span class="p">});</span></code></pre></figure> <p><strong>We really don’t want to apply this interceptor in real-world usage, because it will dramatically increase the traffic on your server. Caching is there for a reason, we only want to override this on our dev boxes!</strong></p> <p>Enjoy, folks!</p> 2016-02-06T00:00:00+00:00 SITE NAME /preventing-angularjs-from-caching-your-templates-and-other-files Fixing "Unable to connect to web server 'IIS Express'." /fixing-unable-to-connect-to-web-server-iis-express <p>Delete your web application’s <code class="highlighter-rouge">.vs\applicationhost.config</code> and try again.</p> <p>Then, in your project settings, change the <code class="highlighter-rouge">App URL</code> port to something new:</p> <p><img src="/media/2016-02-07 12_38_46-AuthorisationServer - Microsoft Visual Studio (Administrator).png" alt="2016-02-07 12_38_46-AuthorisationServer - Microsoft Visual Studio (Administrator).png" /></p> <p>Launch your app. You can change the <code class="highlighter-rouge">App URL</code> back again afterwards, this just ensures VS will regenerate your <code class="highlighter-rouge">applicationhost.config</code>.</p> <p>Enjoy!</p> 2016-02-06T00:00:00+00:00 SITE NAME /fixing-unable-to-connect-to-web-server-iis-express Enabling CORS for HTML5 video element screenshots /enabling-cors-for-html5-video-element-screenshots <p>Or:</p> <p><em>Preventing <strong>Uncaught SecurityError: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported.</strong></em></p> <h1>Scenario</h1> <p>You are trying to take a screenshot of a HTML5 video element, from a video that is on a different domain, and you are getting a security error similar to the above.</p> <p><strong>For example</strong><br /> <em>Your video playing client is here:</em><br /> <a href="http://www.myapp.com/player.html">http://www.myapp.com/player.html</a><br /> <em>Your video is being served from here:</em><br /> <a href="http://cdn.myapp.com/video.mp4">http://cdn.myapp.com/video.mp4</a></p> <h1>The solution</h1> <p>Run through the following check-list to ensure you have absolutely everything configured correctly.</p> <h1>Chrome</h1> <p>This seems to be just in Chrome, but even just <em>loading</em> your HTML5 video will fail if your video is being served from <em>any</em> port other than <em>80</em> <strong><em>and</em></strong> is <strong>not</strong> using <code class="highlighter-rouge">https</code>:</p> <p><strong>THIS WILL FAIL</strong><br /> Client at <strong>http://www.myapp.com/player.html</strong>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;video crossOrigin="anonymous" src="http://cdn.myapp.com:81/video.mp4"&gt;&lt;/video&gt; </code></pre></div></div> <p><strong>THIS WILL SUCCEED</strong><br /> Client at <strong>http://www.myapp.com/player.html</strong>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;video crossOrigin="anonymous" src="https://cdn.myapp.com:81/video.mp4"&gt;&lt;/video&gt; </code></pre></div></div> <p>(the <code class="highlighter-rouge">src</code> is set to <code class="highlighter-rouge">https</code> in the successful example)</p> <h1>Timing</h1> <p><code class="highlighter-rouge">getImageData()</code> and <code class="highlighter-rouge">toDataURL()</code> will be security blocked <em>unless</em> <strong>crossorigin</strong> is set to <code class="highlighter-rouge">anonymous</code> or <code class="highlighter-rouge">use-credentials</code> ([as defined here][1]) <strong>before</strong> the video is loaded. You can achieve this either by adding <code class="highlighter-rouge">crossOrigin</code> directly to the HTML element, or ensuring that javascript sets it <strong>before</strong> the <code class="highlighter-rouge">src</code> is loaded:</p> <p><strong>THIS WILL FAIL</strong><br /></p> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="nx">vid</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s2">"..."</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">load</span><span class="p">();</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">crossOrigin</span> <span class="o">=</span> <span class="s2">"anonymous"</span><span class="p">;</span></code></pre></figure> <p><strong>THIS WILL SUCCEED</strong><br /></p> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="nx">vid</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s2">"..."</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">load</span><span class="p">();</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">crossOrigin</span> <span class="o">=</span> <span class="s2">"anonymous"</span><span class="p">;</span></code></pre></figure> <p><strong>THIS WILL SUCCEED</strong><br /></p> <figure class="highlight"><pre><code class="language-html" data-lang="html"> <span class="nt">&lt;video</span> <span class="na">crossOrigin=</span><span class="s">"anonymous"</span> <span class="na">controls</span><span class="nt">&gt;&lt;/video&gt;</span></code></pre></figure> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="nx">vid</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s2">"..."</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">load</span><span class="p">();</span></code></pre></figure> <h1>Spelling</h1> <p>If you are going to set <code class="highlighter-rouge">crossOrigin</code> in javascript, be sure to use the correct casing for the javascript property: <code class="highlighter-rouge">crossOrigin</code> (NOT <code class="highlighter-rouge">crossorigin</code>) [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes</p> <p><strong>THIS WILL FAIL</strong><br /></p> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="nx">vid</span><span class="p">.</span><span class="nx">crossorigin</span> <span class="o">=</span> <span class="s2">"anonymous"</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s2">"..."</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">load</span><span class="p">();</span></code></pre></figure> <p><strong>THIS WILL SUCCEED</strong><br /></p> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="nx">vid</span><span class="p">.</span><span class="nx">crossOrigin</span> <span class="o">=</span> <span class="s2">"anonymous"</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s2">"..."</span><span class="p">;</span> <span class="nx">vid</span><span class="p">.</span><span class="nx">load</span><span class="p">();</span></code></pre></figure> <h1>Serving of data</h1> <p>You will need your CDN to deliver your video with the <code class="highlighter-rouge">Access-Control-Allow-Origin</code> set to something useful for you. For more details on this header <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">see here</a>, but to test you can use <code class="highlighter-rouge">*</code> as a value (highly dangerous, not recommended for production) otherwise you must specify the domain of your client, in the examples case above the header would look like:</p> <p><code class="highlighter-rouge">Access-Control-Allow-Origin:http://www.myapp.com</code></p> <h1>Everything should now work</h1> <p>Providing you have met all of these requirements, you should be good to go. Happy CORSing!</p> 2016-02-06T00:00:00+00:00 SITE NAME /enabling-cors-for-html5-video-element-screenshots vNext cheat sheet /vnext-cheat-sheet <p>(this tiny blog post will be added to over time, and is initially for my own use as a cheat sheet to remember all the new vNext details, because, well, the only thing that hasn’t changed is the colour of Visual Studio)</p> <h1 id="dnvm">DNVM</h1> <h2 id="dot-net-version-manager">Dot Net Version Manager</h2> <p><a href="https://github.com/aspnet/Home/wiki/Version-Manager">https://github.com/aspnet/Home/wiki/Version-Manager</a></p> <p>###Commands // Upgrades your installed .NET runtime version to the latest STABLE dnvm upgrade</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// Upgrades your installed .NET runtime version to the latest UNSTABLE dnvm upgrade -u // Upgrades your installed .NET core runtime version to the latest STABLE dnvm upgrade -r coreclr // Upgrades your installed .NET core runtime version to the latest UNSTABLE dnvm upgrade -r coreclr -u // Architecture (apply to any of the above) dnvm upgrade [-x86][-x64] </code></pre></div></div> <h1 id="dnx">DNX</h1> <h2 id="dot-net-execution-environment">Dot Net Execution Environment</h2> <p><a href="https://github.com/aspnet/dnx">https://github.com/aspnet/dnx</a> <a href="https://docs.asp.net/en/latest/dnx/overview.html">https://docs.asp.net/en/latest/dnx/overview.html</a></p> <p>###Commands</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// Updates the database with the latest migrations dnx ef database update // Adds a migration with the name "MyMigration" dnx ef migrations add MyMigration </code></pre></div></div> 2016-01-17T00:00:00+00:00 SITE NAME /vnext-cheat-sheet error_insufficient_access_to_site_folder /error-insufficient-access-to-site-folder <p>If you get this whilst trying to publish to Azure, navigate to your app/website in Azure and click “Restart”, then try to publish again.</p> 2016-01-17T00:00:00+00:00 SITE NAME /error-insufficient-access-to-site-folder Gotcha: Unable to load application or execute command 'YourProjectName' /gotcha-unable-to-load-application-or-execute-command-yourprojectname <p>I was recently stuck with a problem whereby neither IIS Express nor Kestrel would load my project.</p> <p>IIS Express would just hang on a spinning loading icon in Chrome, whereas Kestrel would at least deliver me the following error message:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Error: Unable to load application or execute command 'YourProjectName'. Available commands: kestrel, web, ef. </code></pre></div></div> <h1 id="short-answer">Short answer</h1> <p>Your project name in ASP.NET vNext is the <em>folder</em> name that contains your project files, <em>not</em> the project name of your Visual Studio project.</p> <h1 id="long-answer">Long answer</h1> <p><a href="https://github.com/aspnet/Announcements/issues/131">Recent changes</a> to ASP.NET 5 RC2 mean that you must specify the entry point assembly (the assembly that contains your static main method) in your <code class="highlighter-rouge">project.json</code>:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>"commands": { "web": "YourProjectName" }, </code></pre></div></div> <p>The gotcha lies in the fact that unlike traditional project names, in ASP.NET vNext, the project name is abstracted from Visual Studio, and so it is <em>not</em> the name of the project itself as you see it in visual studio:</p> <p><img src="/media/2016-01-15 16_25_57-AuthorisationServer - Microsoft Visual Studio (Administrator).png" alt="2016-01-15 16_25_57-AuthorisationServer - Microsoft Visual Studio (Administrator).png" /></p> <p>It’s this:</p> <p><img src="/media/2016-01-15 16_36_56-src.png" alt="2016-01-15 16_36_56-src.png" /></p> <p>Yep, it’s the folder name. Make the <code class="highlighter-rouge">commands</code> &gt; <code class="highlighter-rouge">web</code> entry in your <code class="highlighter-rouge">project.json</code> match the folder name and you’ll be good to go.</p> 2016-01-15T00:00:00+00:00 SITE NAME /gotcha-unable-to-load-application-or-execute-command-yourprojectname Pretty, extensionless URLs in GitHub Pages using Jekyll /pretty-extensionless-urls-in-github-pages-using-jekyll <p>If you want to have extensionless URLs in GitHub Pages then you might be inclined to add a permalink to the metadata of your page or post:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>permalink: /my-pretty-url </code></pre></div></div> <p>Short answer for TL;DR, you need to put a forward slash at the end of your pretty permalink:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>permalink: /my-pretty-url/ </code></pre></div></div> <p>If you don’t do this, it will <em>kind of</em> work and you’ll still get the expected URL:</p> <p><img src="/media/2015-12-21 15_19_24-the overengineer.png" alt="2015-12-21 15_19_24-the overengineer.png" /></p> <p>However you may well end up with your browser simply prompting you to download the page rather than actually navigate to it:</p> <p><img src="/media/2015-12-21 15_17_32-Cortana.png" alt="2015-12-21 15_17_32-Cortana.png" /></p> <p>The is simply down to how <code class="highlighter-rouge">Jekyll</code> generates pages. Right now you will end up with an extensionless file generated for your page:</p> <p><img src="/media/2015-12-21 15_22_04-_site.png" alt="2015-12-21 15_22_04-_site.png" /></p> <p>What you want is a folder with the pretty name containing your page in an <code class="highlighter-rouge">index.html</code>, like below:</p> <p><img src="/media/2015-12-21 15_24_01-my-pretty-url.png" alt="2015-12-21 15_24_01-my-pretty-url.png" /></p> <p>If you put a forward slash on the end of your <code class="highlighter-rouge">permalink</code> pretty URL, you force <code class="highlighter-rouge">Jekyll</code> to do just this:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>permalink: /my-pretty-url/ </code></pre></div></div> <p>Enjoy!</p> 2015-12-21T00:00:00+00:00 SITE NAME /pretty-extensionless-urls-in-github-pages-using-jekyll HTTP Error 500.19 with ASP.NET 5 after moving a project to a new folder /http-error-500-19-with-asp-net-5-after-moving-a-project-to-a-new-folder <p>If you move your <code class="highlighter-rouge">ASP.NET 5</code> project to a new folder and run it, you might suddenly find yourself with a 500.19 error whilst IIS attempts to read your <code class="highlighter-rouge">web.config</code>. What I noticed was that the folder that IIS was looking in for my <code class="highlighter-rouge">web.config</code> was the old folder:</p> <p><img src="/media/2015-12-21 11_42_42-IIS 10.0 Detailed Error - 500.19 - Internal Server Error - Microsoft Edge.png" alt="2015-12-21 11_42_42-IIS 10.0 Detailed Error - 500.19 - Internal Server Error - Microsoft Edge.png" /></p> <p>To resolve this, just restart Visual Studio <em>even if you’ve only just opened Visual Studio fresh anyway</em>. Don’t waste time fiddling around with a perfectly fine <code class="highlighter-rouge">web.config</code>.</p> <p>I guess it is a teething problem with the latest version of IIS Express.</p> 2015-12-21T00:00:00+00:00 SITE NAME /http-error-500-19-with-asp-net-5-after-moving-a-project-to-a-new-folder Updating a Visual Studio 2015 template ASP.NET 5 web application from RC1 to RC2 /updating-a-visual-studio-2015-template-asp-net-5-web-application-from-rc1-to-rc2 <p>There are a few gotchas when migrating Visual Studio 2015’s ASP.NET 5 web application template from <code class="highlighter-rouge">RC1</code> to <code class="highlighter-rouge">RC2</code>.</p> <p>This quick guide will help you through them.</p> <h1 id="application-insights">Application Insights</h1> <p>First of all, at the time of writing <code class="highlighter-rouge">Applicaiton Insights</code> has no <code class="highlighter-rouge">RC2</code> equivalent. If you try to force the reference in <code class="highlighter-rouge">project.json</code> to <code class="highlighter-rouge">RC2</code>, you’ll see something like this:</p> <p><img src="/media/2015-12-20 12_58_22-Photos.png" alt="2015-12-20 12_58_22-Photos.png" /></p> <p>To this end, remove all references to <code class="highlighter-rouge">Application Insights</code> from your app.</p> <h2 id="projectjson">project.json</h2> <p>First up, remove even the reference to <code class="highlighter-rouge">Application Insights</code> from your <code class="highlighter-rouge">project.json</code>, deleting the line shown below:</p> <figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="w"> </span><span class="s2">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s2">"Microsoft.ApplicationInsights.AspNet"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-rc2-*"</span><span class="p">,</span><span class="w"> </span><span class="p">},</span></code></pre></figure> <h2 id="code">Code</h2> <p>Now compile the app and run through any compilation errors that arise due to the now removed <code class="highlighter-rouge">Application Insights</code> reference.</p> <p>You should find some references in <code class="highlighter-rouge">Startup.cs</code>. Remove all the below, if you find them, and any other references that might be in there (in case the template changed since the time of writing):</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">public</span> <span class="nf">Startup</span><span class="p">(</span><span class="n">IHostingEnvironment</span> <span class="n">env</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="k">if</span> <span class="p">(</span><span class="n">env</span><span class="p">.</span><span class="nf">IsDevelopment</span><span class="p">())</span> <span class="p">{</span> <span class="p">...</span> <span class="c1">// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.</span> <span class="n">builder</span><span class="p">.</span><span class="nf">AddApplicationInsightsSettings</span><span class="p">(</span><span class="n">developerMode</span><span class="p">:</span> <span class="k">true</span><span class="p">);</span> <span class="p">...</span> <span class="p">}</span> <span class="p">...</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">ConfigureServices</span><span class="p">(</span><span class="n">IServiceCollection</span> <span class="n">services</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="n">services</span><span class="p">.</span><span class="nf">AddApplicationInsightsTelemetry</span><span class="p">(</span><span class="n">Configuration</span><span class="p">);</span> <span class="p">...</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">Configure</span><span class="p">(</span><span class="n">IApplicationBuilder</span> <span class="n">app</span><span class="p">,</span> <span class="n">IHostingEnvironment</span> <span class="n">env</span><span class="p">,</span> <span class="n">ILoggerFactory</span> <span class="n">loggerFactory</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseApplicationInsightsRequestTelemetry</span><span class="p">();</span> <span class="p">...</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseApplicationInsightsExceptionTelemetry</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">Configure</span><span class="p">(</span><span class="n">IApplicationBuilder</span> <span class="n">app</span><span class="p">,</span> <span class="n">IHostingEnvironment</span> <span class="n">env</span><span class="p">,</span> <span class="n">ILoggerFactory</span> <span class="n">loggerFactory</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="n">loggerFactory</span><span class="p">.</span><span class="nf">AddConsole</span><span class="p">(</span><span class="n">Configuration</span><span class="p">.</span><span class="nf">GetSection</span><span class="p">(</span><span class="s">"Logging"</span><span class="p">));</span> <span class="p">...</span> <span class="p">}</span></code></pre></figure> <h2 id="cshtml">cshtml</h2> <p>Next up, some sneaky references still reside in some cshtml files. Delete the following lines where <code class="highlighter-rouge">ApplicationInsights</code> is in red:</p> <h3 id="viewsshared_layoutcshtml"><code class="highlighter-rouge">\Views\Shared\_Layout.cshtml</code></h3> <p><img src="/media/2015-12-20 13_12_10-Photos.png" alt="2015-12-20 13_12_10-Photos.png" /></p> <h3 id="views_viewimportscshtml"><code class="highlighter-rouge">\Views\_ViewImports.cshtml</code></h3> <p><img src="/media/2015-12-20 13_13_00-Photos.png" alt="2015-12-20 13_13_00-Photos.png" /></p> <p><code class="highlighter-rouge">_ViewImports.cshtml</code> has one other sneaky change in <code class="highlighter-rouge">RC2</code> that we need to make. The line beginning with <code class="highlighter-rouge">@addTagHelper</code> needs to have the quotes removed from around <code class="highlighter-rouge">*, Microsoft.AspNet.Mvc.TagHelpers</code>, so it ends up looking like this:</p> <p><img src="/media/2015-12-20 14_14_56-Photos.png" alt="2015-12-20 14_14_56-Photos.png" /></p> <p>The red squiggly error line is fine. Ignore it. It just wants to scare you but its behind a screen, it can’t touch you.</p> <h3 id="all-cshtml">All cshtml</h3> <p>Run a find and replace on all cshtml files, replacing:</p> <p><code class="highlighter-rouge">asp-validation-summary="ValidationSummary.All"</code></p> <p>With:</p> <p><code class="highlighter-rouge">asp-validation-summary="All"</code></p> <p>You can quite safely run a <code class="highlighter-rouge">Fine and replace</code> throughout your solution like this:</p> <p><img src="/media/2015-12-21 12_04_11-AuthorisationServer.png" alt="2015-12-21 12_04_11-AuthorisationServer.png" /></p> <h2 id="development-mode">Development mode</h2> <p><code class="highlighter-rouge">ASP.NET 5</code> allows you to write funky code like this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">if</span> <span class="p">(</span><span class="n">env</span><span class="p">.</span><span class="nf">IsDevelopment</span><span class="p">())</span> <span class="p">{</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseBrowserLink</span><span class="p">();</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseDeveloperExceptionPage</span><span class="p">();</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseExceptionHandler</span><span class="p">(</span><span class="s">"/Home/Error"</span><span class="p">);</span> <span class="p">}</span></code></pre></figure> <p>This particular example ensures that in developer environments, you see useful exceptions, whereas elsewhere you see friendly error pages.</p> <p>For this to work in <code class="highlighter-rouge">RC2</code>, you’ll need to add a new environment variable to be sure your <code class="highlighter-rouge">env</code> <em>is</em> <code class="highlighter-rouge">Development</code>. Go to your project properties, click on the <code class="highlighter-rouge">Debug</code> tab and add an <code class="highlighter-rouge">Environment Variable</code> called <code class="highlighter-rouge">ASPNET_ENV</code> with a value of <code class="highlighter-rouge">Development</code>:</p> <p><img src="/media/2015-12-20 16_27_01-Photos.png" alt="2015-12-20 16_27_01-Photos.png" /></p> <h2 id="hostingjson">hosting.json</h2> <p>Finally, RC2 expects a <code class="highlighter-rouge">hosting.json</code> file to be in the root of your project. This file configures how your application will be hosted. Create a file with that name and the following contents:</p> <figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w"> </span><span class="s2">"server"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Microsoft.AspNet.Server.Kestrel"</span><span class="w"> </span><span class="p">}</span></code></pre></figure> <p>This <code class="highlighter-rouge">hosting.json</code> will use Kestrel to serve your app, as you can see.</p> <p>Now your application should run as expected. Enjoy!</p> 2015-12-20T00:00:00+00:00 SITE NAME /updating-a-visual-studio-2015-template-asp-net-5-web-application-from-rc1-to-rc2 Using OpenIddict to easily add token authentication to your .NET web apps /Using-OpenIddict-to-easily-add-token-authentication-to-your-.NET-web-apps <p><a href="https://github.com/openiddict">OpenIddict</a> is a quick and easy way to get your web application talking to an authorisation server using OAuth.</p> <p>This article assumes you already know what it is, so I’m going to dive straight into talking about each step required to get your authorisation server up and running, starting from <code class="highlighter-rouge">File -&gt; New project</code> for both the authorisation server and the client web app.</p> <p>Let’s go.</p> <h1 id="authorisation-server">Authorisation Server</h1> <p><strong>Edit: 19/01/2016</strong> Add this to the <code class="highlighter-rouge">Startup.cs</code> in your OpenIddict server app:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>app.UseOverrideHeaders(x =&gt; { x.ForwardedOptions = ForwardedHeaders.All; }); </code></pre></div></div> <p>This middleware retrieves the transport security protocol from the proxy headers and restores the real scheme.</p> <p>For instance, if you are using IIS and Kestrel, the communication between the two is made without SSL, so the connection is reported as HTTP-only. <br /> <strong>EndEdit: 19/01/2016</strong></p> <p>We will make this first, so as promised, fire up Visual Studio and click <code class="highlighter-rouge">File -&gt; New project</code> and choose <code class="highlighter-rouge">ASP.NET Web Application</code>:</p> <p><img src="/media/2015-12-19 20_41_20-New Project.png" alt="2015-12-19 20_41_20-New Project.png" /></p> <p>Then choose <code class="highlighter-rouge">Web Application</code> from under the <code class="highlighter-rouge">ASP.NE 5 Templates</code> section, with authentication set to <em>Individual User Accounts</em> which should be the default:</p> <p><img src="/media/2015-12-19 20_42_37-New ASP.NET Project - AuthorisationServer.png" alt="2015-12-19 20_42_37-New ASP.NET Project - AuthorisationServer.png" /></p> <p>You should now have a nice, fresh authorisation server web application:</p> <p><img src="/media/2015-12-19 20_47_09-AuthorisationServer - Microsoft Visual Studio (Administrator).png" alt="2015-12-19 20_47_09-AuthorisationServer - Microsoft Visual Studio (Administrator).png" /></p> <p>Hit <code class="highlighter-rouge">Ctrl+F5</code> and you should see your new app running smoothly:</p> <p><img src="/media/2015-12-19 20_50_10-Home Page - AuthorisationServer - Microsoft Edge.png" alt="2015-12-19 20_50_10-Home Page - AuthorisationServer - Microsoft Edge.png" /></p> <p>Now, before we go any further, at the time of typing <em>OpenIddict</em> uses the latest nightly builds of ASP.NET. As such we need to update our own code to use the nightly builds.</p> <p>You can <a href="/moving-your-asp-net-5-project-to-nightly-builds">refer to my previous blog post on how to achieve this</a>. Make sure you follow the tutorial carefully.</p> <p>Lovely. So far, so Microsoft. Now let’s start adding the <em>OpenIddict</em> stuff.</p> <p>Now we need to add <code class="highlighter-rouge">OpenIddict</code> to our <code class="highlighter-rouge">project.json</code>. Add the following two dependencies to your <code class="highlighter-rouge">project.json</code>:</p> <figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="w"> </span><span class="s2">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s2">"AspNet.Security.OAuth.Validation"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-*"</span><span class="p">,</span><span class="w"> </span><span class="s2">"OpenIddict"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-*"</span><span class="w"> </span><span class="p">}</span></code></pre></figure> <p>Great, save that and <code class="highlighter-rouge">OpenIddict</code> will automagically be installed. Finally time to plug in <code class="highlighter-rouge">OpenIddict</code> in the actual code of your new web app.</p> <p>First up, add the following usings to the top of your <code class="highlighter-rouge">Startup.cs</code>:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1 2 </pre></td><td class="code"><pre><span class="k">using</span> <span class="nn">OpenIddict</span><span class="p">;</span> <span class="k">using</span> <span class="nn">OpenIddict.Models</span><span class="p">;</span></pre></td></tr></tbody></table></code></pre></figure> <p>Then, add <code class="highlighter-rouge">.AddOpenIddict()</code> to the end of the <code class="highlighter-rouge">services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()</code> line in <code class="highlighter-rouge">ConfigureServices()</code> method in <code class="highlighter-rouge">Startup.cs</code> (line 9 below):</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1 2 3 4 5 6 7 8 9 10 </pre></td><td class="code"><pre><span class="k">public</span> <span class="k">void</span> <span class="nf">ConfigureServices</span><span class="p">(</span><span class="n">IServiceCollection</span> <span class="n">services</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="n">services</span><span class="p">.</span><span class="n">AddIdentity</span><span class="p">&lt;</span><span class="n">ApplicationUser</span><span class="p">,</span> <span class="n">IdentityRole</span><span class="p">&gt;()</span> <span class="p">.</span><span class="n">AddEntityFrameworkStores</span><span class="p">&lt;</span><span class="n">ApplicationDbContext</span><span class="p">&gt;()</span> <span class="p">.</span><span class="nf">AddDefaultTokenProviders</span><span class="p">()</span> <span class="c1">// Add this line here</span> <span class="p">.</span><span class="nf">AddOpenIddict</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span></pre></td></tr></tbody></table></code></pre></figure> <p>Next up, in your <code class="highlighter-rouge">Configure(..)</code> method add the following code (from and including line 7 below). Crucially, this must be added <em>after</em> <code class="highlighter-rouge">app.UseIdentity()</code> is called:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1 2 3 4 5 6 7 8 9 10 </pre></td><td class="code"><pre><span class="k">public</span> <span class="k">void</span> <span class="nf">Configure</span><span class="p">(</span><span class="n">IApplicationBuilder</span> <span class="n">app</span><span class="p">,</span> <span class="n">IHostingEnvironment</span> <span class="n">env</span><span class="p">,</span> <span class="n">ILoggerFactory</span> <span class="n">loggerFactory</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseIdentity</span><span class="p">();</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseOAuthValidation</span><span class="p">();</span> <span class="c1">// This must be *after* "app.UseIdentity();" above</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseOpenIddict</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span></pre></td></tr></tbody></table></code></pre></figure> <p>In that same method, ensure you replace anything that looks like this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="n">app</span><span class="p">.</span><span class="nf">UseMvc</span><span class="p">(</span><span class="n">routes</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="n">routes</span><span class="p">.</span><span class="nf">MapRoute</span><span class="p">(</span> <span class="n">name</span><span class="p">:</span> <span class="s">"default"</span><span class="p">,</span> <span class="n">template</span><span class="p">:</span> <span class="s">"{controller=Home}/{action=Index}/{id?}"</span><span class="p">);</span> <span class="p">});</span></code></pre></figure> <p>With this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="n">app</span><span class="p">.</span><span class="nf">UseMvcWithDefaultRoute</span><span class="p">();</span></code></pre></figure> <p>Alright, that’s your authentication server configured for <code class="highlighter-rouge">OpenIddict</code>.</p> <p>We haven’t configured any clients in the server yet, we will come back to do that once our client is set up and we know the details we’ll need.</p> <p>Hit <code class="highlighter-rouge">Ctrl+F5</code> again and you should see the same screen as before. Nothing should be different.</p> <p>For more details on configuration options with OpenIddict, see <a href="https://github.com/openiddict/core/wiki/Configuration-and-options">Configuration &amp; Options</a> on the OpenIddict github page.</p> <h2 id="what-exactly-is-this-code-going-to-do-then">What exactly is this code going to do, then?</h2> <p>This code will create the endpoints necessary to receive authentication requests and issues cookies/tokens etc. Think of it as crating a listener in your app that will listen at points like:</p> <p><code class="highlighter-rouge">http://localhost:1212/authenticate</code></p> <p>You will now modify your client app so that it will send requests to these endpoints when it wants to sort things out like logging in, logging out, er, logging back in again and so on.</p> <h1 id="client">Client</h1> <p>The client app is a little more involved.</p> <p>First up, <code class="highlighter-rouge">File -&gt; New project</code> yourself an <code class="highlighter-rouge">ASP.NET 5 web application</code>:</p> <p><img src="/media/2015-12-20 13_38_13-New Project.png" alt="2015-12-20 13_38_13-New Project.png" /></p> <p>This time, however, make sure you click <code class="highlighter-rouge">Change authentication</code> and then select <code class="highlighter-rouge">No authentication</code>:</p> <p><img src="/media/2015-12-20 13_40_10-Change Authentication.png" alt="2015-12-20 13_40_10-Change Authentication.png" /></p> <p><img src="/media/2015-12-20 13_41_32-New ASP.NET Project - OpenIddictClient.png" alt="2015-12-20 13_41_32-New ASP.NET Project - OpenIddictClient.png" /></p> <p>We don’t need the authentication code, because we’ll be “out sourcing” that to our <code class="highlighter-rouge">OpenIddict</code> authentication server.</p> <h2 id="rc2">RC2</h2> <p>At the time of writing, <code class="highlighter-rouge">OpenIddict</code> uses the <code class="highlighter-rouge">RC2</code> nightly builds of <code class="highlighter-rouge">ASP.NET 5</code>, so just as we did with the authentication server follow my guide on <a href="/moving-your-asp-net-5-project-to-nightly-builds/">moving your ASP.NET 5 project to nightly builds</a>.</p> <h2 id="projectjson">project.json</h2> <p>Now we need to add the companion authentication project references to our client web app. OpenIddict uses another project, by the same author, called <code class="highlighter-rouge">Microsoft.AspNet.Authentication.OpenIdConnect</code>. Add the following dependencies to your <code class="highlighter-rouge">project.json</code>:</p> <figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="w"> </span><span class="s2">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s2">"Microsoft.AspNet.Authentication.Cookies"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-rc2-*"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Microsoft.AspNet.Authentication.OpenIdConnect"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-rc2-*"</span><span class="p">,</span><span class="w"> </span><span class="p">}</span></code></pre></figure> <h2 id="startupcs">Startup.cs</h2> <p>Now in your <code class="highlighter-rouge">Startup.cs</code>, add the following using directives at the top:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="nn">Microsoft.AspNet.Authentication</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Microsoft.AspNet.Authentication.Cookies</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Microsoft.AspNet.Http</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Microsoft.IdentityModel.Protocols.OpenIdConnect</span><span class="p">;</span></code></pre></figure> <p>And add the following to each of the below methods, ensuring you configure line <code class="highlighter-rouge">34</code> and <code class="highlighter-rouge">39</code> with your respective application URLs:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 </pre></td><td class="code"><pre><span class="k">public</span> <span class="k">void</span> <span class="nf">ConfigureServices</span><span class="p">(</span><span class="n">IServiceCollection</span> <span class="n">services</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Add the below code to the top of this method</span> <span class="n">services</span><span class="p">.</span><span class="n">Configure</span><span class="p">&lt;</span><span class="n">SharedAuthenticationOptions</span><span class="p">&gt;(</span><span class="n">options</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="n">options</span><span class="p">.</span><span class="n">SignInScheme</span> <span class="p">=</span> <span class="n">CookieAuthenticationDefaults</span><span class="p">.</span><span class="n">AuthenticationScheme</span><span class="p">;</span> <span class="p">});</span> <span class="n">services</span><span class="p">.</span><span class="nf">AddAuthentication</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">Configure</span><span class="p">(</span><span class="n">IApplicationBuilder</span> <span class="n">app</span><span class="p">,</span> <span class="n">IHostingEnvironment</span> <span class="n">env</span><span class="p">,</span> <span class="n">ILoggerFactory</span> <span class="n">loggerFactory</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseIISPlatformHandler</span><span class="p">();</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseStaticFiles</span><span class="p">();</span> <span class="c1">// Add the following code after your calls to "app.UseIISPlatformHandler()" and</span> <span class="c1">// "app.UseStaticFiles()"</span> <span class="c1">// Insert a new cookies middleware in the pipeline to store the user</span> <span class="c1">// identity after he has been redirected from the identity provider.</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseCookieAuthentication</span><span class="p">(</span><span class="n">options</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="n">options</span><span class="p">.</span><span class="n">AutomaticAuthenticate</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">AutomaticChallenge</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">LoginPath</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">PathString</span><span class="p">(</span><span class="s">"/signin"</span><span class="p">);</span> <span class="p">});</span> <span class="n">app</span><span class="p">.</span><span class="nf">UseOpenIdConnectAuthentication</span><span class="p">(</span><span class="n">options</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">idServerUrl</span> <span class="p">=</span> <span class="n">YOUR_AUTHENTICATION_SERVER_URL</span><span class="p">;</span> <span class="c1">// Note: these settings must match the application details</span> <span class="c1">// inserted in the database at the server level.</span> <span class="n">options</span><span class="p">.</span><span class="n">ClientId</span> <span class="p">=</span> <span class="n">YOUR_CLIENT_APP_ID</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">ClientSecret</span> <span class="p">=</span> <span class="n">YOUR_CLIENT_APP_SECRET</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">PostLogoutRedirectUri</span> <span class="p">=</span> <span class="n">YOUR_CLIENT_APP_URL</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">RequireHttpsMetadata</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">GetClaimsFromUserInfoEndpoint</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">SaveTokensAsClaims</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="c1">// Use the authorization code flow.</span> <span class="n">options</span><span class="p">.</span><span class="n">ResponseType</span> <span class="p">=</span> <span class="n">OpenIdConnectResponseTypes</span><span class="p">.</span><span class="n">Code</span><span class="p">;</span> <span class="c1">// Note: setting the Authority allows the OIDC client middleware to automatically</span> <span class="c1">// retrieve the identity provider's configuration and spare you from setting</span> <span class="c1">// the different endpoints URIs or the token validation parameters explicitly.</span> <span class="n">options</span><span class="p">.</span><span class="n">Authority</span> <span class="p">=</span> <span class="n">idServerUrl</span><span class="p">;</span> <span class="n">options</span><span class="p">.</span><span class="n">Scope</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="s">"email"</span><span class="p">);</span> <span class="p">});</span> <span class="p">...</span> <span class="p">}</span></pre></td></tr></tbody></table></code></pre></figure> <p>For the above code to compile, you’ll need to fill in the following variables:</p> <ul> <li>YOUR_CLIENT_APP_ID <ul> <li>This is just the ID of your client, and it <em>cannot contain spaces</em>. For safety, best to just stick to letters. For now you can just use something like “MyClient”, nice and professinal, like.</li> </ul> </li> <li>YOUR_CLIENT_APP_SECRET <ul> <li>Again, we’re just getting this up and running so choose anything, but for production apps ensure this is kept absolutely private and is impossible to guess. Best to just use a random guid.</li> </ul> </li> <li>YOUR_CLIENT_APP_URL <ul> <li>This is just the URL your client app is running at, for now it’s likely some localhost URL</li> </ul> </li> <li>YOUR_AUTHENTICATION_SERVER_URL <ul> <li>This is the URL of your authrozation server we configured earlier, again likely to be some localhost URL</li> </ul> </li> </ul> <h2 id="create-a-login-page">Create a login page</h2> <p>Either replace the contents of your existing <code class="highlighter-rouge">Index.cshtml</code> with the following, or create a new page with the following cshtml code:</p> <figure class="highlight"><pre><code class="language-aspx-cs" data-lang="aspx-cs">@{ ViewData["Title"] = "Home Page"; } @if (User?.Identity?.IsAuthenticated ?? false) { &lt;h1&gt;Welcome, @User.Identity.Name&lt;/h1&gt; &lt;a class="btn btn-lg btn-danger" href="/signout"&gt;Sign out&lt;/a&gt; } else { &lt;a class="btn btn-lg btn-success" href="/home/signin"&gt;Sign in&lt;/a&gt; }</code></pre></figure> <p>Now add the following usings and methods to the controller for the above cshtml file:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="nn">Microsoft.AspNet.Authentication.Cookies</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Microsoft.AspNet.Authentication.OpenIdConnect</span><span class="p">;</span> <span class="k">using</span> <span class="nn">Microsoft.AspNet.Http.Authentication</span><span class="p">;</span> <span class="p">...</span> <span class="k">public</span> <span class="n">IActionResult</span> <span class="nf">SignIn</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">ChallengeResult</span><span class="p">(</span><span class="n">OpenIdConnectDefaults</span><span class="p">.</span><span class="n">AuthenticationScheme</span><span class="p">,</span> <span class="k">new</span> <span class="n">AuthenticationProperties</span> <span class="p">{</span> <span class="n">RedirectUri</span> <span class="p">=</span> <span class="s">"/"</span> <span class="p">});</span> <span class="p">}</span> <span class="na">[HttpGet("~/signout"), HttpPost("~/signout")]</span> <span class="k">public</span> <span class="k">async</span> <span class="n">Task</span> <span class="nf">SignOut</span><span class="p">()</span> <span class="p">{</span> <span class="c1">// Instruct the cookies middleware to delete the local cookie created when the user agent</span> <span class="c1">// is redirected from the identity provider after a successful authorization flow.</span> <span class="k">await</span> <span class="n">HttpContext</span><span class="p">.</span><span class="n">Authentication</span><span class="p">.</span><span class="nf">SignOutAsync</span><span class="p">(</span><span class="n">CookieAuthenticationDefaults</span><span class="p">.</span><span class="n">AuthenticationScheme</span><span class="p">);</span> <span class="c1">// Instruct the OpenID Connect middleware to redirect the user agent to the identity provider to sign out.</span> <span class="k">await</span> <span class="n">HttpContext</span><span class="p">.</span><span class="n">Authentication</span><span class="p">.</span><span class="nf">SignOutAsync</span><span class="p">(</span><span class="n">OpenIdConnectDefaults</span><span class="p">.</span><span class="n">AuthenticationScheme</span><span class="p">);</span> <span class="p">}</span></code></pre></figure> <p>The <code class="highlighter-rouge">SignOut()</code> method above can be in any controller, because we’ve mapped a specific route, which is the relative route <code class="highlighter-rouge">~/signout</code> to always go to this action.</p> <p>Now, hit <code class="highlighter-rouge">Ctrl+F5</code> and you should see a nice big green <code class="highlighter-rouge">Sign in</code> button:</p> <p><img src="/media/2015-12-20 15_00_26-Home Page - OpenIddictClient - Microsoft Edge.png" alt="2015-12-20 15_00_26-Home Page - OpenIddictClient - Microsoft Edge.png" /></p> <p>Before we click it…</p> <h1 id="back-to-the-authorisation-server-project">Back to the authorisation server project</h1> <p>…we need to dip back into our authorisation server to configure our new clientn app.</p> <p>The first thing we need to do is make our data context inherit from <code class="highlighter-rouge">OpenIddictContext</code>. So open up your <code class="highlighter-rouge">ApplicationDbContext.cs</code> and add the following using:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="nn">OpenIddict</span><span class="p">;</span></code></pre></figure> <p>Then change the class definition to this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">public</span> <span class="k">class</span> <span class="nc">ApplicationDbContext</span> <span class="p">:</span> <span class="n">OpenIddictContext</span><span class="p">&lt;</span><span class="n">ApplicationUser</span><span class="p">&gt;</span></code></pre></figure> <p>Then add the following method:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">private</span> <span class="k">static</span> <span class="kt">bool</span> <span class="n">_databaseChecked</span><span class="p">;</span> <span class="c1">// The following code creates the database and schema if they don't exist.</span> <span class="c1">// This is a temporary workaround since deploying database through EF migrations is</span> <span class="c1">// not yet supported in this release.</span> <span class="c1">// Please see this http://go.microsoft.com/fwlink/?LinkID=615859 for more information on how to do deploy the database</span> <span class="c1">// when publishing your application.</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">EnsureDatabaseCreated</span><span class="p">()</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(!</span><span class="n">_databaseChecked</span><span class="p">)</span> <span class="p">{</span> <span class="n">_databaseChecked</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="k">this</span><span class="p">.</span><span class="n">Database</span><span class="p">.</span><span class="nf">EnsureCreated</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <p>Your final class should look something like this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">public</span> <span class="k">class</span> <span class="nc">ApplicationDbContext</span> <span class="p">:</span> <span class="n">OpenIddictContext</span><span class="p">&lt;</span><span class="n">ApplicationUser</span><span class="p">&gt;</span> <span class="p">{</span> <span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnModelCreating</span><span class="p">(</span><span class="n">ModelBuilder</span> <span class="n">builder</span><span class="p">)</span> <span class="p">{</span> <span class="k">base</span><span class="p">.</span><span class="nf">OnModelCreating</span><span class="p">(</span><span class="n">builder</span><span class="p">);</span> <span class="c1">// Customize the ASP.NET Identity model and override the defaults if needed.</span> <span class="c1">// For example, you can rename the ASP.NET Identity table names and more.</span> <span class="c1">// Add your customizations after calling base.OnModelCreating(builder);</span> <span class="p">}</span> <span class="k">private</span> <span class="k">static</span> <span class="kt">bool</span> <span class="n">_databaseChecked</span><span class="p">;</span> <span class="c1">// The following code creates the database and schema if they don't exist.</span> <span class="c1">// This is a temporary workaround since deploying database through EF migrations is</span> <span class="c1">// not yet supported in this release.</span> <span class="c1">// Please see this http://go.microsoft.com/fwlink/?LinkID=615859 for more information on how to do deploy the database</span> <span class="c1">// when publishing your application.</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">EnsureDatabaseCreated</span><span class="p">()</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(!</span><span class="n">_databaseChecked</span><span class="p">)</span> <span class="p">{</span> <span class="n">_databaseChecked</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span> <span class="k">this</span><span class="p">.</span><span class="n">Database</span><span class="p">.</span><span class="nf">EnsureCreated</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <p>Now open up your <code class="highlighter-rouge">AccountController.cs</code>.</p> <p>This next part is a bit of a hack for now, as we need to ensure that the database is created and ready for <code class="highlighter-rouge">OpenIddict</code> to use when account stuff happens.</p> <p>Firstly, we need to pass in our data context into our <code class="highlighter-rouge">AccountController</code> now because we’re going to be looking up clients etc. so add <code class="highlighter-rouge">ApplicationDbContext</code> to your constructor and save a reference to it in a private field named <code class="highlighter-rouge">_applicationDbContext</code>. Your new constructor should look something like the following:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="c1">// Create this private field</span> <span class="k">private</span> <span class="k">readonly</span> <span class="n">ApplicationDbContext</span> <span class="n">_applicationDbContext</span><span class="p">;</span> <span class="k">public</span> <span class="nf">AccountController</span><span class="p">(</span> <span class="n">UserManager</span><span class="p">&lt;</span><span class="n">ApplicationUser</span><span class="p">&gt;</span> <span class="n">userManager</span><span class="p">,</span> <span class="n">SignInManager</span><span class="p">&lt;</span><span class="n">ApplicationUser</span><span class="p">&gt;</span> <span class="n">signInManager</span><span class="p">,</span> <span class="n">IEmailSender</span> <span class="n">emailSender</span><span class="p">,</span> <span class="n">ISmsSender</span> <span class="n">smsSender</span><span class="p">,</span> <span class="n">ILoggerFactory</span> <span class="n">loggerFactory</span><span class="p">,</span> <span class="c1">// Add this in</span> <span class="n">ApplicationDbContext</span> <span class="n">applicationDbContext</span><span class="p">)</span> <span class="p">{</span> <span class="n">_userManager</span> <span class="p">=</span> <span class="n">userManager</span><span class="p">;</span> <span class="n">_signInManager</span> <span class="p">=</span> <span class="n">signInManager</span><span class="p">;</span> <span class="n">_emailSender</span> <span class="p">=</span> <span class="n">emailSender</span><span class="p">;</span> <span class="n">_smsSender</span> <span class="p">=</span> <span class="n">smsSender</span><span class="p">;</span> <span class="n">_logger</span> <span class="p">=</span> <span class="n">loggerFactory</span><span class="p">.</span><span class="n">CreateLogger</span><span class="p">&lt;</span><span class="n">AccountController</span><span class="p">&gt;();</span> <span class="c1">// Save a reference to it</span> <span class="n">_applicationDbContext</span> <span class="p">=</span> <span class="n">applicationDbContext</span><span class="p">;</span> <span class="p">}</span></code></pre></figure> <p>Now add the following line to the top of each of the below three methods:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="c1">//</span> <span class="c1">// POST: /Account/Register</span> <span class="na">[HttpPost]</span> <span class="na">[AllowAnonymous]</span> <span class="na">[ValidateAntiForgeryToken]</span> <span class="k">public</span> <span class="k">async</span> <span class="n">Task</span><span class="p">&lt;</span><span class="n">IActionResult</span><span class="p">&gt;</span> <span class="nf">Register</span><span class="p">(</span><span class="n">RegisterViewModel</span> <span class="n">model</span><span class="p">)</span> <span class="p">{</span> <span class="n">_applicationDbContext</span><span class="p">.</span><span class="nf">EnsureDatabaseCreated</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span> <span class="c1">//</span> <span class="c1">// POST: /Account/ExternalLogin</span> <span class="na">[HttpPost]</span> <span class="na">[AllowAnonymous]</span> <span class="na">[ValidateAntiForgeryToken]</span> <span class="k">public</span> <span class="n">IActionResult</span> <span class="nf">ExternalLogin</span><span class="p">(</span><span class="kt">string</span> <span class="n">provider</span><span class="p">,</span> <span class="kt">string</span> <span class="n">returnUrl</span> <span class="p">=</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span> <span class="n">_applicationDbContext</span><span class="p">.</span><span class="nf">EnsureDatabaseCreated</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span> <span class="c1">//</span> <span class="c1">// POST: /Account/Login</span> <span class="na">[HttpPost]</span> <span class="na">[AllowAnonymous]</span> <span class="na">[ValidateAntiForgeryToken]</span> <span class="k">public</span> <span class="k">async</span> <span class="n">Task</span><span class="p">&lt;</span><span class="n">IActionResult</span><span class="p">&gt;</span> <span class="nf">Login</span><span class="p">(</span><span class="n">LoginViewModel</span> <span class="n">model</span><span class="p">,</span> <span class="kt">string</span> <span class="n">returnUrl</span> <span class="p">=</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span> <span class="n">_applicationDbContext</span><span class="p">.</span><span class="nf">EnsureDatabaseCreated</span><span class="p">();</span> <span class="p">...</span> <span class="p">}</span></code></pre></figure> <p>Now, open up your authorisation server’s <code class="highlighter-rouge">Startup.cs</code> again and add the following code <em>beneath</em> <code class="highlighter-rouge">app.UseOpenIddict()</code>:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">context</span> <span class="p">=</span> <span class="n">app</span><span class="p">.</span><span class="n">ApplicationServices</span><span class="p">.</span><span class="n">GetRequiredService</span><span class="p">&lt;</span><span class="n">ApplicationDbContext</span><span class="p">&gt;())</span> <span class="p">{</span> <span class="n">context</span><span class="p">.</span><span class="n">Database</span><span class="p">.</span><span class="nf">EnsureCreated</span><span class="p">();</span> <span class="c1">// Add Mvc.Client to the known applications.</span> <span class="k">if</span> <span class="p">(!</span><span class="n">context</span><span class="p">.</span><span class="n">Applications</span><span class="p">.</span><span class="nf">Any</span><span class="p">())</span> <span class="p">{</span> <span class="c1">// Note: when using the introspection middleware, your resource server</span> <span class="c1">// MUST be registered as an OAuth2 client and have valid credentials.</span> <span class="c1">// </span> <span class="c1">// context.Applications.Add(new Application {</span> <span class="c1">// Id = "resource_server",</span> <span class="c1">// DisplayName = "Main resource server",</span> <span class="c1">// Secret = "875sqd4s5d748z78z7ds1ff8zz8814ff88ed8ea4z4zzd"</span> <span class="c1">// });</span> <span class="n">context</span><span class="p">.</span><span class="n">Applications</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="k">new</span> <span class="n">Application</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="n">YOUR_CLIENT_APP_ID</span><span class="p">,</span> <span class="n">DisplayName</span> <span class="p">=</span> <span class="s">"My client application"</span><span class="p">,</span> <span class="n">RedirectUri</span> <span class="p">=</span> <span class="n">YOUR_CLIENT_APP_URL</span> <span class="p">+</span> <span class="s">"/signin-oidc"</span><span class="p">,</span> <span class="n">LogoutRedirectUri</span> <span class="p">=</span> <span class="n">YOUR_CLIENT_APP_URL</span><span class="p">,</span> <span class="n">Secret</span> <span class="p">=</span> <span class="n">Crypto</span><span class="p">.</span><span class="nf">HashPassword</span><span class="p">(</span><span class="n">YOUR_CLIENT_APP_SECRET</span><span class="p">),</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">OpenIddictConstants</span><span class="p">.</span><span class="n">ApplicationTypes</span><span class="p">.</span><span class="n">Confidential</span> <span class="p">});</span> <span class="n">context</span><span class="p">.</span><span class="nf">SaveChanges</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <p>Again, you’ll need to fill in the variables for <code class="highlighter-rouge">YOUR_CLIENT_APP_URL</code>, <code class="highlighter-rouge">YOUR_CLIENT_APP_ID</code> and <code class="highlighter-rouge">YOUR_CLIENT_APP_SECRET</code> exactly as before.</p> <p>For now, we’re hardcoding seeding our client app into our authorisation server’s database. Later you might want to refactor this out.</p> <p>Finally, we’ll need to run migrations on our database to get the <code class="highlighter-rouge">OpenIddict</code> tables in there.</p> <p>Open up a command prompt at your authorisation server’s project folder and run the following command:</p> <p><code class="highlighter-rouge">dnx ef migrations add OpenIddict</code></p> <p>You will see this error, we’ll deal with this in a moment:</p> <p><img src="/media/2015-12-20 16_04_58-C__Windows_System32_cmd.exe.png" alt="2015-12-20 16_04_58-C__Windows_System32_cmd.exe.png" /></p> <p>You should now see a new file in your solution explorer:</p> <p><img src="/media/2015-12-20 16_05_32-Photos.png" alt="2015-12-20 16_05_32-Photos.png" /></p> <p>We need to fix up the auto-generated migration a little.</p> <p>There will be lots of entries like this:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="n">migrationBuilder</span><span class="p">.</span><span class="nf">DropForeignKey</span><span class="p">(</span> <span class="n">name</span><span class="p">:</span> <span class="s">"FK_AspNetUserRoles_AspNetUsers_UserId"</span><span class="p">,</span> <span class="n">table</span><span class="p">:</span> <span class="s">"AspNetUserRoles"</span><span class="p">);</span></code></pre></figure> <p>Delete from this migration’s <code class="highlighter-rouge">Up(..)</code> and <code class="highlighter-rouge">Down(..)</code> methods any entries that reference any <code class="highlighter-rouge">AspNetXXX</code> tables.</p> <p>You should end up with a migration class that looks more or less like this, but it might vary as <code class="highlighter-rouge">OpenIddict</code> changes:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">public</span> <span class="k">partial</span> <span class="k">class</span> <span class="nc">OpenIddict</span> <span class="p">:</span> <span class="n">Migration</span> <span class="p">{</span> <span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Up</span><span class="p">(</span><span class="n">MigrationBuilder</span> <span class="n">migrationBuilder</span><span class="p">)</span> <span class="p">{</span> <span class="n">migrationBuilder</span><span class="p">.</span><span class="nf">CreateTable</span><span class="p">(</span> <span class="n">name</span><span class="p">:</span> <span class="s">"Application"</span><span class="p">,</span> <span class="n">columns</span><span class="p">:</span> <span class="n">table</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="n">table</span><span class="p">.</span><span class="n">Column</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="n">nullable</span><span class="p">:</span> <span class="k">false</span><span class="p">),</span> <span class="n">DisplayName</span> <span class="p">=</span> <span class="n">table</span><span class="p">.</span><span class="n">Column</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="n">nullable</span><span class="p">:</span> <span class="k">true</span><span class="p">),</span> <span class="n">LogoutRedirectUri</span> <span class="p">=</span> <span class="n">table</span><span class="p">.</span><span class="n">Column</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="n">nullable</span><span class="p">:</span> <span class="k">true</span><span class="p">),</span> <span class="n">RedirectUri</span> <span class="p">=</span> <span class="n">table</span><span class="p">.</span><span class="n">Column</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="n">nullable</span><span class="p">:</span> <span class="k">true</span><span class="p">),</span> <span class="n">Secret</span> <span class="p">=</span> <span class="n">table</span><span class="p">.</span><span class="n">Column</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="n">nullable</span><span class="p">:</span> <span class="k">true</span><span class="p">),</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">table</span><span class="p">.</span><span class="n">Column</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="n">nullable</span><span class="p">:</span> <span class="k">true</span><span class="p">)</span> <span class="p">},</span> <span class="n">constraints</span><span class="p">:</span> <span class="n">table</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="n">table</span><span class="p">.</span><span class="nf">PrimaryKey</span><span class="p">(</span><span class="s">"PK_Application"</span><span class="p">,</span> <span class="n">x</span> <span class="p">=&gt;</span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span><span class="p">);</span> <span class="p">});</span> <span class="p">}</span> <span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Down</span><span class="p">(</span><span class="n">MigrationBuilder</span> <span class="n">migrationBuilder</span><span class="p">)</span> <span class="p">{</span> <span class="n">migrationBuilder</span><span class="p">.</span><span class="nf">DropTable</span><span class="p">(</span> <span class="n">name</span><span class="p">:</span> <span class="s">"Application"</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span></code></pre></figure> <p>Now <em>be sure to recompile your whole project before the next step</em>, because we’re going to run the migrations, but the migration running code doesn’t compile your project for you, so if you forget you’ll be using the old migrations.</p> <p>Now, go back to command prompt at your authorisation server’s project folder and run the following command:</p> <p><code class="highlighter-rouge">dnx ef database update</code></p> <p>You should see something like this:</p> <p><img src="/media/2015-12-20 16_13_20-C__Windows_System32_cmd.exe.png" alt="2015-12-20 16_13_20-C__Windows_System32_cmd.exe.png" /></p> <p>Now go back to Visual Studio, hit <code class="highlighter-rouge">Ctrl+F5</code> and your authorisation server should run just like before:</p> <p><img src="/media/2015-12-20 16_14_36-Home Page - AuthorisationServer - Microsoft Edge.png" alt="2015-12-20 16_14_36-Home Page - AuthorisationServer - Microsoft Edge.png" /></p> <h1 id="almost-done">Almost done…</h1> <p>We just need to tweak a few things in the authorisation server’s views.</p> <p>The login screen that comes with the <code class="highlighter-rouge">Individual Accounts</code> template contains a <code class="highlighter-rouge">Register as a new user?</code> link:</p> <p><img src="/media/2015-12-21 12_26_47-Log in - AuthorisationServer - Microsoft Edge.png" alt="2015-12-21 12_26_47-Log in - AuthorisationServer - Microsoft Edge.png" /></p> <p>By the time we get to this screen, we are in the <code class="highlighter-rouge">Authorisation App</code> and all the details about the client that would like to login are contained within URL parameters, like this:</p> <p><code class="highlighter-rouge">http://localhost:51962/Account/Login?ReturnUrl=/connect/authorize?unique_id=zA6nvD8tPm59uHkpeDmCruYWTlEd-NoiaMhTkWpJbTc</code></p> <p>As such, as we navigate around the <code class="highlighter-rouge">Authorisation App</code> we need to maintain these parameters so that <code class="highlighter-rouge">OpenIddict</code> knows what to do when we’re logged in and where to redirect back to.</p> <p>For example, if our user would like to create a new account, they’ll click the <code class="highlighter-rouge">Register as a new user?</code> link and be redirected to:</p> <p><code class="highlighter-rouge">http://localhost:51962/Account/Register</code></p> <p>This is no good, because now <code class="highlighter-rouge">OpenIddict</code> has lost all the information it needs.</p> <p>Thankfully, this is very simple.</p> <p>I have also submitted an issue to the ASP.NET templates project to add support for this: <a href="https://github.com/aspnet/Templates/issues/359">https://github.com/aspnet/Templates/issues/359</a></p> <p>For now, in the <code class="highlighter-rouge">Authorisation App</code>, open up the following files and update the code as described.</p> <h2 id="logincshtml">Login.cshtml</h2> <p>Add the <code class="highlighter-rouge">asp-route-returnUrl="@ViewData["ReturnUrl"]"</code> to the <code class="highlighter-rouge">Register as a new user?</code> link:</p> <figure class="highlight"><pre><code class="language-aspx-cs" data-lang="aspx-cs">&lt;a asp-action="Register" asp-route-returnUrl="@ViewData["returnUrl"]"&gt;Register as a new user?&lt;/a&gt;</code></pre></figure> <h2 id="registercshtml">Register.cshtml</h2> <p>Add the <code class="highlighter-rouge">asp-route-returnUrl="@ViewData["ReturnUrl"]"</code> to the form declaration:</p> <figure class="highlight"><pre><code class="language-aspx-cs" data-lang="aspx-cs">&lt;form asp-controller="Account" asp-action="Register" asp-route-returnUrl="@ViewData["returnUrl"]" method="post" class="form-horizontal" role="form"&gt;</code></pre></figure> <h2 id="accountcontrollercs">AccountController.cs</h2> <p>Update your <code class="highlighter-rouge">Register</code> GET and POST methods to accept the <code class="highlighter-rouge">returnUrl</code> parameter and assign the return URL to the view state:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="c1">//</span> <span class="c1">// GET: /Account/Register</span> <span class="na">[HttpGet]</span> <span class="na">[AllowAnonymous]</span> <span class="k">public</span> <span class="n">IActionResult</span> <span class="nf">Register</span><span class="p">(</span> <span class="c1">// Add this parameter</span> <span class="kt">string</span> <span class="n">returnUrl</span> <span class="p">=</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Add this assignment</span> <span class="n">ViewData</span><span class="p">[</span><span class="s">"ReturnUrl"</span><span class="p">]</span> <span class="p">=</span> <span class="n">returnUrl</span><span class="p">;</span> <span class="k">return</span> <span class="nf">View</span><span class="p">();</span> <span class="p">}</span></code></pre></figure> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="c1">//</span> <span class="c1">// POST: /Account/Register</span> <span class="na">[HttpPost]</span> <span class="na">[AllowAnonymous]</span> <span class="na">[ValidateAntiForgeryToken]</span> <span class="k">public</span> <span class="k">async</span> <span class="n">Task</span><span class="p">&lt;</span><span class="n">IActionResult</span><span class="p">&gt;</span> <span class="nf">Register</span><span class="p">(</span><span class="n">RegisterViewModel</span> <span class="n">model</span><span class="p">,</span> <span class="c1">// Add this parameter</span> <span class="kt">string</span> <span class="n">returnUrl</span> <span class="p">=</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Add this assignment</span> <span class="n">ViewData</span><span class="p">[</span><span class="s">"ReturnUrl"</span><span class="p">]</span> <span class="p">=</span> <span class="n">returnUrl</span><span class="p">;</span> <span class="p">...</span> <span class="p">}</span></code></pre></figure> <p>Later in your <code class="highlighter-rouge">Register</code> POST method, change the return from:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">return</span> <span class="nf">RedirectToAction</span><span class="p">(</span><span class="k">nameof</span><span class="p">(</span><span class="n">HomeController</span><span class="p">.</span><span class="n">Index</span><span class="p">),</span> <span class="s">"Home"</span><span class="p">);</span></code></pre></figure> <p>To:</p> <figure class="highlight"><pre><code class="language-c#" data-lang="c#"><span class="k">return</span> <span class="nf">RedirectToLocal</span><span class="p">(</span><span class="n">returnUrl</span><span class="p">);</span></code></pre></figure> <p>Now everything is set up, you should be able to go to your client app, run it, click <code class="highlighter-rouge">Sign in</code>, go through the motions of signing in.</p> <p>The sample projects from this code can be found on GitHub here: <a href="https://github.com/Brandless/DayZero/tree/master/Demos/Overengineer/OpenIddict%20Demo%201">https://github.com/Brandless/DayZero/tree/master/Demos/Overengineer/OpenIddict%20Demo%201</a></p> 2015-12-19T20:33:00+00:00 SITE NAME /Using-OpenIddict-to-easily-add-token-authentication-to-your-.NET-web-apps Moving your ASP.NET 5 project to nightly builds /moving-your-asp-net-5-project-to-nightly-builds <h1 id="moving-your-aspnet-5-project-to-nightly-builds">Moving your ASP.NET 5 project to nightly builds</h1> <p><strong>Note: At the time of writing, <code class="highlighter-rouge">ASP.NET 5</code> is on <code class="highlighter-rouge">RC1</code>, and the nightly builds are for <code class="highlighter-rouge">RC2</code>, so although there is some <code class="highlighter-rouge">RC1</code>/<code class="highlighter-rouge">RC2</code> wording things below, this should work whatever revision the public/nightly set up is on.</strong></p> <p>So first we need to install the latest nightly builds on your machine. Fire up <code class="highlighter-rouge">Command Prompt</code> anywhere you like and type in:</p> <p><code class="highlighter-rouge">dnvm upgrade -u</code></p> <p><a href="http://stackoverflow.com/questions/30131118/what-is-the-dnvm">DNVM is the command line operated .NET Version Manager</a></p> <p>You should see something similar to this:</p> <p><img src="/media/2015-12-19 21_01_43-C__WINDOWS_system32_cmd.exe - dnvm upgrade -u.png" alt="2015-12-19 21_01_43-C__WINDOWS_system32_cmd.exe - dnvm upgrade -u.png" /></p> <p>Great, now your system has the latest builds.</p> <p>Now, you’re going to need to pull the latest and “greatest” nightly nuget packages into your project, so you’ll need to create a custom <code class="highlighter-rouge">NuGet.config</code> file and put it in the <em>same folder as your solution file</em>:</p> <figure class="highlight"><pre><code class="language-xml" data-lang="xml"><span class="cp">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span> <span class="nt">&lt;configuration&gt;</span> <span class="nt">&lt;packageSources&gt;</span> <span class="nt">&lt;add</span> <span class="na">key=</span><span class="s">"aspnet-contrib"</span> <span class="na">value=</span><span class="s">"https://www.myget.org/F/aspnet-contrib/api/v2"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;add</span> <span class="na">key=</span><span class="s">"AspNetVNext"</span> <span class="na">value=</span><span class="s">"https://www.myget.org/F/aspnetvnext/api/v2"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;add</span> <span class="na">key=</span><span class="s">"WebStack Nightly"</span> <span class="na">value=</span><span class="s">"http://www.myget.org/F/aspnetwebstacknightly/"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;add</span> <span class="na">key=</span><span class="s">"Roslyn"</span> <span class="na">value=</span><span class="s">"https://www.myget.org/F/roslyn-nightly"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;add</span> <span class="na">key=</span><span class="s">"DotNetCore"</span> <span class="na">value=</span><span class="s">"https://www.myget.org/F/dotnet-core/"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;add</span> <span class="na">key=</span><span class="s">"NuGet"</span> <span class="na">value=</span><span class="s">"https://api.nuget.org/v3/index.json"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;/packageSources&gt;</span> <span class="nt">&lt;/configuration&gt;</span></code></pre></figure> <p>Now you’ll need to refresh your packages to the latest version using this new <code class="highlighter-rouge">NuGet.config</code>, so open up <code class="highlighter-rouge">Command Prompt</code> <em>at the folder that contains your authentication project’s solution file</em> and type:</p> <p><code class="highlighter-rouge">dnu restore --no-cache</code></p> <p>(Note: we’re doing the <code class="highlighter-rouge">no-cache</code> option just to be on the safe side)</p> <p>You should see some lovely things like this happening:</p> <p><img src="/media/2015-12-19 21_16_49-C__Windows_System32_cmd.exe - dnu restore --no-cache.png" alt="2015-12-19 21_16_49-C__Windows_System32_cmd.exe - dnu restore --no-cache.png" /></p> <p>Now, as it stands, your authentication project probably still has the “normal” (at the time of writing, <code class="highlighter-rouge">RC1</code>) references:</p> <p><img src="/media/2015-12-19 21_20_23-OneNote.png" alt="2015-12-19 21_20_23-OneNote.png" /></p> <p>In <code class="highlighter-rouge">Visual Studio</code>, open up your project’s properties and in the <code class="highlighter-rouge">Application</code> tab, change <code class="highlighter-rouge">Solution DNX SDK version</code> to <code class="highlighter-rouge">RC2</code> (the latest at the time of writing):</p> <p><img src="/media/2015-12-19 21_23_02-.png" alt="2015-12-19 21_23_02-.png" /></p> <p>Next up, open up your <code class="highlighter-rouge">project.json</code> and find and replace all occurences of <code class="highlighter-rouge">-rc1-final</code> with <code class="highlighter-rouge">-rc2-*</code> (or whatever relevant version you want to target at the time you run through this):</p> <p><img src="/media/2015-12-19 21_28_40-AuthorisationServer - Microsoft Visual Studio (Administrator).png" alt="2015-12-19 21_28_40-AuthorisationServer - Microsoft Visual Studio (Administrator).png" /></p> <p>As soon as you save, it’ll start downloading the new packages:</p> <p><img src="/media/2015-12-19 21_30_57-OneNote.png" alt="2015-12-19 21_30_57-OneNote.png" /></p> <p><img src="/media/2015-12-19 21_32_36-AuthorisationServer - Microsoft Visual Studio (Administrator).png" alt="2015-12-19 21_32_36-AuthorisationServer - Microsoft Visual Studio (Administrator).png" /></p> <p>When it’s done, you should have all new <code class="highlighter-rouge">RC2</code> references:</p> <p><img src="/media/2015-12-19 21_33_15-OneNote.png" alt="2015-12-19 21_33_15-OneNote.png" /></p> <h2 id="rc1---rc2-problems">RC1 -&gt; RC2 problems</h2> <p>If you are using the Visual Studio 2015 template for an <code class="highlighter-rouge">ASP.NET 5</code> web application, then it is essentialy you also <a href="/updating-a-visual-studio-2015-template-asp-net-5-web-application-from-rc1-to-rc2">follow my article on how to fix some gotchas when migrating to RC2</a>.</p> 2015-12-18T00:00:00+00:00 SITE NAME /moving-your-asp-net-5-project-to-nightly-builds