<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Brian Vander Plaats</title>
    <description>Personal website and blog of Brian Vander Plaats. Focus on software development articles.</description>
    <link>http://brianvanderplaats.com/</link>
    <atom:link href="http://brianvanderplaats.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 25 Apr 2017 14:20:26 +0000</pubDate>
    <lastBuildDate>Tue, 25 Apr 2017 14:20:26 +0000</lastBuildDate>
    <generator>Jekyll v3.4.3</generator>
    
      <item>
        <title>Generate Queryable List of Names from Excel</title>
        <description>&lt;p&gt;Here’s a handy excel formula to remember:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;=CONCATENATE(CHAR(39),A2, &quot; &quot;, B2, CHAR(39), CHAR(44))
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/list_of_names_excel.png&quot; alt=&quot;list of names excel&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For quickly generating a list of queryable list of names:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;Select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dbo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Bill Gates'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Steve Jobs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Larry Ellison'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Steve Case'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Jon Carmack'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Elon Musk'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'Sergey Brin'&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This can be handy if you get spreadsheets from people and want to quickly filter results in the database.   I did this today for someone who wanted a list of user names for about 40 people, far too many to spend half an hour typing these in!&lt;/p&gt;
</description>
        <pubDate>Mon, 30 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2017/01/30/generate-queryable-list-of-names-from-excel/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2017/01/30/generate-queryable-list-of-names-from-excel/</guid>
        
        <category>excel</category>
        
        <category>sql</category>
        
        
        <category>tips</category>
        
      </item>
    
      <item>
        <title>ADFS Authentication - Adding to Existing Site</title>
        <description>&lt;p&gt;The Visual Studio new project template is a handy wizard for setting up AD FS on a new site, but the wizard isn’t available for existing sites.  Additionally, the wizard forces you to include ASP.NET MVC components, even if you just need to set up a Web API back-end.  This post shows you how to do this manually.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Make sure SSL is enabled in your project.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Set The SSL URL to the one provided by your AD FS administrator, or configure AD FS to use your website url/port. e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;https://localhost:44300/&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;Add required packages (search for these on NuGet)
 &lt;img src=&quot;/assets/manual_adfs_nuget.png&quot; alt=&quot;manual adfs nuget&quot; /&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Owin&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Owin.Security&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Owin.Security.Cookies&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Owin.Security.WsFederation&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Owin.Host.SystemWeb&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Add AD FS Metadata to &lt;code class=&quot;highlighter-rouge&quot;&gt;Web.config&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;appSettings&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;key=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ida:ADFSMetadata&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://example.com/federationmetadata/2007-06/federationmetadata.xml&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;key=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ida:Wtrealm&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://localhost:44300/&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/appSettings&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Add a &lt;code class=&quot;highlighter-rouge&quot;&gt;Startup.cs&lt;/code&gt; file/class if it does not already exist&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Add configuration hooks to &lt;code class=&quot;highlighter-rouge&quot;&gt;Startup.cs&lt;/code&gt;. Note that if you have not added &lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Owin.Host.SystemWeb&lt;/code&gt; to your packages &lt;code class=&quot;highlighter-rouge&quot;&gt;Startup.Configuration()&lt;/code&gt; will not fire.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IAppBuilder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;ConfigureAuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConfigureAuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IAppBuilder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;SetDefaultSignInAsAuthenticationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CookieAuthenticationDefaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AuthenticationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;UseCookieAuthentication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CookieAuthenticationOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;UseWsFederationAuthentication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WsFederationAuthenticationOptions&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Wtrealm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ConfigurationManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AppSettings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ida:Wtrealm&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;MetadataAddress&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ConfigurationManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AppSettings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ida:ADFSMetadata&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
           
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Add Authorization to controllers / routes.
    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;Method 1: Set global filter&lt;/p&gt;

        &lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpConfiguration&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Authorize all api routes
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AuthorizeAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

      &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
        &lt;/div&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;Method 2: Add Authorize Attribute to controller&lt;/p&gt;

        &lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Authorize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PersonController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ApiController&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Build and Run your app.  Your routes requiring authorization should now redirect you to the AD FS login page&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/assets/manual_adfs_signin.png&quot; alt=&quot;manual adfs signin&quot; /&gt;&lt;/p&gt;

</description>
        <pubDate>Fri, 27 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2017/01/27/ADFS-Authentication-Adding-to-Existing-Site/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2017/01/27/ADFS-Authentication-Adding-to-Existing-Site/</guid>
        
        <category>ADFS</category>
        
        <category>ASP.NET Identity</category>
        
        <category>Owin</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>ADFS Authentication Adding Custom Claims</title>
        <description>&lt;p&gt;While user authentication is a key component of AD FS, the returned user claims are powerful tools for client applications.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Common user information (Name, login, email) can be retrieved without code duplication in multiple applications&lt;/li&gt;
  &lt;li&gt;Security roles can be added to the claims token, reducing roundtrips or querying security information from a database during application lifetimes&lt;/li&gt;
  &lt;li&gt;Unique system fields / tokens / identifiers that would normally be stored in some global variable or hidden fields&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two ways to add to the available claims:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Configure AD FS to send additional claims - this will be shown in a future post&lt;/li&gt;
  &lt;li&gt;Dynamically add claims during user sign-on&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before going into details here, a few high level points to keep in mind:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Claim tokens are shared between all sites in a subdomain e.g &lt;code class=&quot;highlighter-rouge&quot;&gt;https://apps.example.com&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Claim tokens can expire (based on AD FS settings), or be removed by the user logging out.&lt;/li&gt;
  &lt;li&gt;Claims from the AD FS server can be removed at any time.&lt;/li&gt;
  &lt;li&gt;Changes made to the claims will not affect users that have a current claims token.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thus, your application should never assume that a claim exists.&lt;/p&gt;

&lt;h2 id=&quot;adding-roles-to-claims&quot;&gt;Adding Roles to claims&lt;/h2&gt;

&lt;p&gt;An excellent usage of claims information is populating the application security roles the user has access to.  This is information that will be checked frequently during the applications lifetime, and querying a security database on each access is inefficient.&lt;/p&gt;

&lt;p&gt;To begin, you will need a security database with users/roles/groups etc.  At my company we use the venerable &lt;code class=&quot;highlighter-rouge&quot;&gt;ASPNETDB&lt;/code&gt; database that has been part of the &lt;code class=&quot;highlighter-rouge&quot;&gt;asp.net membership&lt;/code&gt; system for a long time.&lt;/p&gt;

&lt;p&gt;Adding the claims is done in the &lt;code class=&quot;highlighter-rouge&quot;&gt;ConfigureAuth()&lt;/code&gt; method. In a vanilla configuration, this looks as follows:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;SetDefaultSignInAsAuthenticationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CookieAuthenticationDefaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AuthenticationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;UseCookieAuthentication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CookieAuthenticationOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;UseWsFederationAuthentication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WsFederationAuthenticationOptions&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Wtrealm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;MetadataAddress&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adfsMetadata&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Adding claims is done by specifying additional items in &lt;code class=&quot;highlighter-rouge&quot;&gt;WsFederationAuthenticationOptions&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;UseWsFederationAuthentication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WsFederationAuthenticationOptions&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Wtrealm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MetadataAddress&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adfsMetadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Notifications&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WsFederationAuthenticationNotifications&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;SecurityTokenValidated&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accountName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AuthenticationTicket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClaimTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Upn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;accountName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;//Query roles for user from security database
&lt;/span&gt;            &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringRoles&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TestRole1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TestRole2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TestRole3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TestRole4&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roles&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringRoles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RoleName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;//loop through returned roles, and add
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AuthenticationTicket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddClaim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                                                    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClaimTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RoleName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FromResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Basically we specify an event handler to fire as soon as the claims token exists.  Note while this is configured in &lt;code class=&quot;highlighter-rouge&quot;&gt;startup.auth.cs&lt;/code&gt;, this code only runs when the user signs in. This means that if you change which roles the user has in the database, or in Active Directory, the user will not see these roles until they logout, and signs in again.&lt;/p&gt;

&lt;p&gt;The first step is getting the user identity we will use to retreive user roles - the user name is a good option here, but you could use a number of claims (again, based on what the AD FS adminstrator configured)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;nameidentfier - &lt;code class=&quot;highlighter-rouge&quot;&gt;brianvp&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;upn - &lt;code class=&quot;highlighter-rouge&quot;&gt;brianvp@example.com&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;emailaddress - &lt;code class=&quot;highlighter-rouge&quot;&gt;brian.vanderplaats@example.com&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, you will take your chosen identifier and query your security database.&lt;/p&gt;

&lt;p&gt;Then, iterate through these roles, and add to the claims collection using the &lt;code class=&quot;highlighter-rouge&quot;&gt;AddClaim()&lt;/code&gt; method.  Make sure to specify &lt;code class=&quot;highlighter-rouge&quot;&gt;ClaimTypes.Role&lt;/code&gt;. The actual role name is simply a string. Note that if you specify a role twice, it will be added twice.  This should not be a problem with multiple applications, as again, this handler does not fire on app startup, only during the sign-in process.  Once the user is signed in to an application on a domain, they will not need to sign in to other applications on the domain.&lt;/p&gt;

&lt;h2 id=&quot;using-roles&quot;&gt;Using Roles&lt;/h2&gt;

&lt;p&gt;Now that the roles are added, you need to configure your application to use them.  The simplest way is to combine roles into &lt;code class=&quot;highlighter-rouge&quot;&gt;[Authorize]&lt;/code&gt; tags on your controllers.  For example, you could lock down your editor pages by placing the role over create/edit controller actions, but not over the view actions:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Details&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;[HttpPost]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;[ValidateAntiForgeryToken]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;[Authorize(Roles = &quot;ModelEditorRole&quot;)]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;[Authorize(Roles = &quot;ModelEditorRole&quot;)]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Edit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Another way is to look at the user’s current roles while performing an action inside a method. This can be done by iterating through the user’s claims:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inTestRole4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claims&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClaimsIdentity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CurrentPrincipal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClaimTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Role&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TestRole4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;inTestRole4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Or, even easier, you can use the simple check:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if(User.IsInRole(&quot;TestRole4&quot;))
{

}

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;adding-custom-claim-types&quot;&gt;Adding custom claim types&lt;/h2&gt;

&lt;p&gt;Adding a custom claim is basically the same as adding a role claim.  Instead of specifying &lt;code class=&quot;highlighter-rouge&quot;&gt;ClaimTypes.Role&lt;/code&gt;, you give it your own name / value:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;SecurityTokenValidated&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accountName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AuthenticationTicket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClaimTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Upn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FirstOrDefault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//do something to retrieve appropriate value for current user
&lt;/span&gt;    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CompanyUserID&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4000333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AuthenticationTicket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddClaim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CompanyUserID&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CompanyUserID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FromResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Again, a good candidate for these claims are broad, static bits of information that will not change, or at least the frequency of change should be much longer than the typical claims token expiration period (as the new value would not be received until next user login).&lt;/p&gt;
</description>
        <pubDate>Thu, 19 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2017/01/19/ADFS-Authentication-Adding-Custom-Claims/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2017/01/19/ADFS-Authentication-Adding-Custom-Claims/</guid>
        
        <category>ADFS</category>
        
        <category>ASP.NET Identity</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>ADFS Authentication Using Visual Studio Wizard</title>
        <description>&lt;p&gt;This post demonstrates how to set up a &lt;em&gt;new&lt;/em&gt; ASP.NET MVC project using AD FS. Before you can do this, you need to have an AD FS Server up and running.  In my testing, I used an on-network AD FS Server, but a &lt;a href=&quot;https://docs.microsoft.com/en-us/azure/active-directory/connect/active-directory-aadconnect-azure-adfs&quot;&gt;cloud / azure AD FS&lt;/a&gt; option exists as well (but I haven’t worked with at this point).&lt;/p&gt;

&lt;p&gt;You will need a few pieces of information from your AD FS administrator before proceeding:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Metadata document URL e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;https://example.com/federationmetadata/2007-06/federationmetadata.xml&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;URI to identify your application.  For local development you can use &lt;code class=&quot;highlighter-rouge&quot;&gt;https://localhost:44300/&lt;/code&gt;, but this &lt;em&gt;must&lt;/em&gt; be configured on the AD FS side as well.  If you specify something else it will not work!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;setting-up-the-project&quot;&gt;Setting up the Project&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Create a new project using ASP.NET Web Application template&lt;/li&gt;
  &lt;li&gt;Check the MVC Template, and optionally click on the Web API checkbox
&lt;img src=&quot;/assets/visual_studio_new_project_templates_adfs.png&quot; alt=&quot;visual studio new project templates adfs&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;Click the “Change Authentication” box.&lt;/li&gt;
  &lt;li&gt;Change the options to “Work and School Accounts”&lt;/li&gt;
  &lt;li&gt;Select On-Premises and enter the metadata from your adminstrator and click OK
&lt;img src=&quot;/assets/visual_studio_new_project_adfs_metadata.png&quot; alt=&quot;visual studio new project adfs metadata&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;Click OK to create the rest of your Project&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Open up your project settings, and change the SSL URL to match the one from your AD FS administrator
&lt;img src=&quot;/assets/visual_studio_project_url.png&quot; alt=&quot;visual studio project url&quot; /&gt;&lt;/p&gt;

&lt;p&gt;By default, your entire project requires authentication, so on your first build &amp;amp; run, you will immediately see the login screen.  To see the home page before logging in, remove the &lt;code class=&quot;highlighter-rouge&quot;&gt;[Authorize]&lt;/code&gt; filter from &lt;code class=&quot;highlighter-rouge&quot;&gt;HomeController&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;trying-out-the-ad-fs-components&quot;&gt;Trying out the AD FS components&lt;/h2&gt;

&lt;p&gt;Build and run your project. You should see the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/new_adfs_project_sign_in.png&quot; alt=&quot;new adfs project sign in&quot; /&gt;&lt;/p&gt;

&lt;p&gt;click sign in - you will be redirected to your AD FS Server’s login page.  Note that this is &lt;em&gt;not&lt;/em&gt; your website&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/new_adfs_project_adfs_login.png&quot; alt=&quot;new adfs project adfs login&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once you sign in, you will automatically redirect back to your sample website. Note that if any metadata is misconfigured (on the AD FS Server or in your project), you will start to see problems here.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/new_adfs_project_signed_in.png&quot; alt=&quot;new adfs project signed in&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You are now signed in until you explicitly sign out, or your claims token expires (claim expiration is controlled by your server administrator). The claim token itself is stored in a cookie, and sent with each request to the server&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/new_adfs_project_cookie.png&quot; alt=&quot;new adfs project cookie&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Click Sign Out - you will be directed to the sign out page on your AD FS Server, and then immediately redirect back to your website.  This does a few things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;invalidates the claims token on the AD FS Server by calling: &lt;code class=&quot;highlighter-rouge&quot;&gt;https://adfs.example.com/adfs/ls/?wtrealm=https%3a%2f%2flocalhost%3a44300%2f&amp;amp;wa=wsignout1.0&amp;amp;wreply=https%3a%2f%2flocalhost%3a44300%2fAccount%2fSignOutCallback&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Removes the ASP.NET Cookie from your browser&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/assets/new_adfs_project_signed_out.png&quot; alt=&quot;new adfs project signed out&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;accessing-the-claims-token&quot;&gt;Accessing the Claims Token&lt;/h2&gt;

&lt;p&gt;You can access your claims token by casting &lt;code class=&quot;highlighter-rouge&quot;&gt;Thread.CurrentPrincipal.Identity&lt;/code&gt; to a &lt;code class=&quot;highlighter-rouge&quot;&gt;ClaimsIdentity&lt;/code&gt; and then accessing the &lt;code class=&quot;highlighter-rouge&quot;&gt;Claims&lt;/code&gt; collection.&lt;/p&gt;

&lt;p&gt;HomeController.cs&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ViewBag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClaimsIdentity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CurrentPrincipal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;index.cshtml&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Logged&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&quot;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;table&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;condensed&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;gt;
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claim&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claim&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Subject&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Issuer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;@foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Security&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claim&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;claim&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ViewBag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClaimsIdentity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Claims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ValueType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Subject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Issuer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/new_adfs_project_user_claims.png&quot; alt=&quot;new adfs project user claims&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;That’s it! You now have a site that you can lock down to authenticated users only. Simply add &lt;code class=&quot;highlighter-rouge&quot;&gt;[Authorize]&lt;/code&gt; tags to the necessary feature controllers, and don’t forget to secure all your api paths! To add role-based security for additional granularity, you can add roles to the claims token itself, which I will go over in a future post.&lt;/p&gt;
</description>
        <pubDate>Wed, 18 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2017/01/18/ADFS-Authentication-Using-Visual-Studio-Wizard/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2017/01/18/ADFS-Authentication-Using-Visual-Studio-Wizard/</guid>
        
        <category>ADFS</category>
        
        <category>ASP.NET Identity</category>
        
        <category>Visual Studio</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>Website Authentication using ADFS Overview</title>
        <description>&lt;p&gt;This is the first in a series of posts going over the use of Active Directory Federation Services (AD FS).  For roughly the past year, I’ve been exploring authentication options for an external website project at work. Previously we focused on using Windows Authentication, but that only really works for Intranet applications, and not so well or at all on mobile / tablet environments.  Since this application is only for employees, I wanted to use existing Active Directory resources.  I briefly explored oAuth as an option, but since we would not be using google/facebook etc for credentials, setting up our own oAuth provider seemed to be as much or more work than utilizing AD FS.  Specifically my design goals were:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Implement Single Sign-on.  Users should not have to log into multiple applications / minimize number of logins&lt;/li&gt;
  &lt;li&gt;Use existing domain accounts - Users do not need another login to manage&lt;/li&gt;
  &lt;li&gt;Do not allow the application to access or store user credentials / secrets - If you are going to use a common credential between websites, &lt;em&gt;no&lt;/em&gt; individual website should have access to these credentials directly. At best, each site would need to re-implement security features, and at worst, one site failing to implement security properly exposes all the sites.&lt;/li&gt;
  &lt;li&gt;Work well for Internet and Intranet applications.  While Windows Authentication worked for us in the past, it doesn’t work well in a post-windows pc world.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Early last year, I created two demo projects, one using oAuth, and the other using AD FS.  Both were successful, but the oAuth solution is using google, so after reviewing with my other developers we decided to use AD FS.  At present, we have two live applications using AD FS, and so far has worked out really well.  Below are the demo projects for reference:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/brianvp/authentication-examples/tree/master/ModelManagerOAuthIndividual&quot;&gt;oAuth Demo Project&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/brianvp/authentication-examples/tree/master/ModelManagerADFS&quot;&gt;AD FS Demo Project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest of this post describes key points of the AD FS architecture&lt;/p&gt;

&lt;h2 id=&quot;ad-fs-overview&quot;&gt;AD FS Overview&lt;/h2&gt;

&lt;p&gt;Active Directory Federation Services is a service that allows sharing identity information between “trusted” partners, called a “federation”. At a high level, it allows a website to delegate authentication to a trusted service, and accept a “claim” from this service on the user’s behalf to make authorization decisions.  For example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;John Doe wants to access the corporate payroll site&lt;/li&gt;
  &lt;li&gt;The payroll site requires users to login in (obviously)&lt;/li&gt;
  &lt;li&gt;When John hits the payroll site, he is not authenticated, so the payroll site redirects John to the AD FS login page&lt;/li&gt;
  &lt;li&gt;John enters his credentials (user name and password) to the AD FS login site.&lt;/li&gt;
  &lt;li&gt;The AD FS site verifies the credentials - if valid, it generates a “claims token”, containing certain information about John. This typically includes his username (johnd), full name, “John Doe”, email, “john.doe@company.com”, and any other property the AD FS service is configured to send.&lt;/li&gt;
  &lt;li&gt;AD FS redirects the user back to the payroll site, sending along the claims token&lt;/li&gt;
  &lt;li&gt;The payroll site now assumes that the original user &lt;em&gt;is&lt;/em&gt; John Doe, and presents resources John Doe is authorized to use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, the claims token is typically stored in a cookie, so if John closes his browser and reloads the payroll site, it can use the existing claims token without requring John to re-log in to AD FS.&lt;/p&gt;

&lt;h2 id=&quot;terminology&quot;&gt;Terminology&lt;/h2&gt;

&lt;p&gt;Some key terminology:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Federation - A group of two or more independant sites / services, that are in a special trust relationship.  With the proper configuration, theoretically any service could be added to the exising AD Federation.  Much of the work required to set up AD FS is managaging this federation, both on the AD FS service, and the website.&lt;/li&gt;
  &lt;li&gt;Account Federation Server - Issues the claims tokens to users based on authenticating that user, basically the AD FS server&lt;/li&gt;
  &lt;li&gt;Relying Party - the website using the claim for authenticating &amp;amp; authorizing the user&lt;/li&gt;
  &lt;li&gt;Single Sign-on / SSO - a user authentication mechanism that allows a user to use one login to access multiple applications&lt;/li&gt;
  &lt;li&gt;Claim - A statement about the user.  There are three types of claims - Identity Claims, Group Claims, and Custom Claims
    &lt;ul&gt;
      &lt;li&gt;An identity claim is basic information about the user e.g. username&lt;/li&gt;
      &lt;li&gt;A group claim is information about which domain groups the user belongs to e.g. PayrollUsers&lt;/li&gt;
      &lt;li&gt;a custom claim is a key/value pair that can be used by applications.  For example, we use a corporate user identity code that is standard between all web applications.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;components&quot;&gt;Components&lt;/h2&gt;

&lt;p&gt;Below is a simplified view of what my current AD FS system looks like&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/adfs_architecture.png&quot; alt=&quot;ADFS Architecture&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The AD FS Federation server is a Windows 2012 R2 Instance responsible for:
    &lt;ul&gt;
      &lt;li&gt;Registering each Relying Party / Web Server / Website&lt;/li&gt;
      &lt;li&gt;defining what claims metdata is in in the claims token&lt;/li&gt;
      &lt;li&gt;Provides a login endpoint for users to enter credentials into&lt;/li&gt;
      &lt;li&gt;Provides a signout endpoint to clear the claims token&lt;/li&gt;
      &lt;li&gt;Initially we used a 2008 R2 Instance, but upgraded to a 2012 R2 instance.  The configurations are largely the same, but one key improvement is the ability customize the user login page (the 2008 R2 page looks terrible on mobile devices)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;The Web Servers are Windows Server (2008 R2 - 2012 R2) instances which host one or more websites configured to used AD FS
    &lt;ul&gt;
      &lt;li&gt;The websites are built with ASP.NET / MVC / Web API&lt;/li&gt;
      &lt;li&gt;use Owin middleware for communicating with AD FS
        &lt;ul&gt;
          &lt;li&gt;redirecting the user to the AD FS Server to login&lt;/li&gt;
          &lt;li&gt;redirecting the user to the AD FS Server to sign out&lt;/li&gt;
          &lt;li&gt;Processing the Claims Token / making the claim available to the web application&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;contain AD FS endpoint / configuration metadata&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;The User Instances (PCs / tablets / mobile) access the websites
    &lt;ul&gt;
      &lt;li&gt;Upon hitting the Web Server instance, user is redirected to the AD FS Server&lt;/li&gt;
      &lt;li&gt;Upon successful login, the claims token is stored in a cookie in the user’s browser.&lt;/li&gt;
      &lt;li&gt;The cookie is read by the website after the AD FS Server redirects the user back to the website.  One of the most important configurations in AD FS is specifing the proper redirect endpoints.&lt;/li&gt;
      &lt;li&gt;The website will continue to use the browser cookie until the user signs out, or the cookie expires.&lt;/li&gt;
      &lt;li&gt;Currently we have a cookie expiration of 24 hours, which will force the user to log in at most once per day.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb897402.aspx&quot;&gt;Microsoft Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://serverfault.com/questions/708669/what-is-adfs-active-directory-federation-services&quot;&gt;Excellent ServerFault Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://technet.microsoft.com/en-us/library/cc754236(v=ws.11).aspx&quot;&gt;Detailed AD FS Terminology&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Tue, 17 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2017/01/17/Website-Authentication-Using-ADFS-Overview/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2017/01/17/Website-Authentication-Using-ADFS-Overview/</guid>
        
        <category>ADFS</category>
        
        <category>ASP.NET Identity</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>Understanding Relative Urls</title>
        <description>&lt;p&gt;Something you use all the time, but don’t always realize, is how a web browser will request resources in html files with relative url’s.&lt;/p&gt;

&lt;p&gt;for example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &amp;lt;script src=&quot;libs/jquery.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;will be served from example.com as &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/libs/jquery.min.js&lt;/code&gt; - provided the website is sitting in the web server root e.g &lt;code class=&quot;highlighter-rouge&quot;&gt;wwwroot&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;/var/www/html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If, instead, the app is sitting in a sub-folder / virtual directory, the above code will still work, even thought it may now come from &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/salesapp/libs/jquery.min.js/&lt;/code&gt;.  This is great, as it lets us decouple our application structure from where it is deployed.&lt;/p&gt;

&lt;p&gt;What if we changed the URL a bit?  say to:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &amp;lt;script src=&quot;/libs/jquery.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;With this change, the leading &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; indicates the root of the website.  It is still a relative path, but it is “relative” to the root of the website.  &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/libs/jquery.min.js&lt;/code&gt; will still work, but &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/salesapp/libs/jquery.min.js&lt;/code&gt; will not.  This can be a subtle error, as most applications run locally are sitting at &lt;code class=&quot;highlighter-rouge&quot;&gt;http://localhost:12345&lt;/code&gt; or whatever, but many are not deployed to the web root.&lt;/p&gt;

&lt;p&gt;Using the leading slash removes a lot of future flexibility.   So why would you want it?  Well, consider this scenario.  You have a script file that has a jquery dependency, which lives at &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/salesapp/scripts/accounts.js&lt;/code&gt;.  This reference will no longer work:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &amp;lt;script src=&quot;libs/jquery.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It will return as &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/salesapp/scripts/libs/jquery.min.js&lt;/code&gt; oops.&lt;/p&gt;

&lt;p&gt;The reason this doesn’t work is that the relative path is build based on your current window location, which will change with different url’s:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://example.com
http://example.com/salesapp
http://example.com/salesapp/accounts
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It will have to be one of these:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &amp;lt;script src=&quot;/libs/jquery.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src=&quot;../libs/jquery.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src=&quot;http://example.com/libs/jquery.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Out of the three, the 2nd is probably the most flexible, but is cumbersome to read and write (especially if your source trees are deep).&lt;/p&gt;

&lt;p&gt;What you really want to do is set your url relative to the path of the application.  This can be  using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag.  According to &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base&quot;&gt;mdn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“The HTML &lt;base /&gt; element specifies the base URL to use for all relative URLs contained within a document.”&lt;/p&gt;

&lt;p&gt;For example, if you set up the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag as follows:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;base href=&quot;/&quot;&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This effecively sets all relative paths relative to the root of the website
&lt;code class=&quot;highlighter-rouge&quot;&gt;libs/jquery.min.js&lt;/code&gt; will be read as &lt;code class=&quot;highlighter-rouge&quot;&gt;/libs/jquery.min.js&lt;/code&gt;, and thus &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/libs/jquery.min.js/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To set an application specific path, you could use:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;base href=&quot;/salesapp/&quot;&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;libs/jquery.min.js&lt;/code&gt; will be read as &lt;code class=&quot;highlighter-rouge&quot;&gt;/salesapp/libs/jquery.min.js&lt;/code&gt;, and thus &lt;code class=&quot;highlighter-rouge&quot;&gt;http://example.com/salesapp/libs/jquery.min.js/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this works well when deploying to different endpoints as only the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;base&amp;gt;&lt;/code&gt; reference needs to change, which can be easily handled via a build system.&lt;/p&gt;

&lt;p&gt;Be careful though, specifing relative paths with the leading slash will &lt;em&gt;ignore&lt;/em&gt; the specified &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;base&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lastly, you can also specify an empty/current directory base path&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;base href=&quot;&quot;&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;base href=&quot;./&quot;&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Which is effectively the same as specifying no &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;base&amp;gt;&lt;/code&gt;.  You might use this for local development.&lt;/p&gt;

&lt;p&gt;Additionally, you can dynamically write the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;base&amp;gt;&lt;/code&gt; with a script, but doesn’t appear to be a &lt;a href=&quot;http://stackoverflow.com/questions/3494954/how-do-i-set-a-pages-base-href-in-javascript&quot;&gt;recommended&lt;/a&gt; approach. It seems better to leave this up to your build / depolyment system.&lt;/p&gt;

&lt;p&gt;The good news is that broken paths are generally easy to detect when your browser is in development mode - just look for the red requests in the network tab, and check the path.  You’ll know something is off when the path is partially correct, but contains additional levels or missing levels altogether.&lt;/p&gt;

</description>
        <pubDate>Mon, 16 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2017/01/16/understanding-relative-urls/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2017/01/16/understanding-relative-urls/</guid>
        
        <category>html</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>Default Startup Project in Visual Studio</title>
        <description>&lt;p&gt;Had an interesting error the other day.  While reviewing an interns test project, I noticed that on a fresh clone of his repo, his website errored out on startup.  For our projects we typically have a project for the website, and another for the business classes.  For some reason, Visual Studio was trying to run the business project as the website.&lt;/p&gt;

&lt;p&gt;This is easily fixed right-clicking the web project and selecting &lt;code class=&quot;highlighter-rouge&quot;&gt;set as startup project&lt;/code&gt;, but this setting is stored in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.suo&lt;/code&gt; file which is &lt;em&gt;not&lt;/em&gt; checked in.  So why does this work in our other projects?&lt;/p&gt;

&lt;p&gt;Doing some googling, came across this &lt;a href=&quot;http://stackoverflow.com/a/1808352/24892&quot;&gt;stackoverflow answer&lt;/a&gt;.  It turns out that Visual Studio looks at the order of the projects in the solution file.  Somehow, my intern added the business project first - I don’t think anyone has done that before.&lt;/p&gt;

&lt;p&gt;All you need to do is manually edit the &lt;code class=&quot;highlighter-rouge&quot;&gt;.sln&lt;/code&gt; file in a text editor, and move the “startup” project to the top of t he &lt;code class=&quot;highlighter-rouge&quot;&gt;Project&lt;/code&gt; / &lt;code class=&quot;highlighter-rouge&quot;&gt;EndProject&lt;/code&gt; blocks&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project(&quot;{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&quot;) = &quot;MySiteApp&quot;, &quot;MySite.App\MySiteApp.csproj&quot;, &quot;{F5C8B11C-FF25-45B6-8F25-B1C779D4A053}&quot;
EndProject
Project(&quot;{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&quot;) = &quot;MySiteBusiness&quot;, &quot;MySite.Business\MySiteBusiness.csproj&quot;, &quot;{E9561B89-36A0-43C2-B9BA-970843316D23}&quot;
EndProject
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;On a related note, it seems like Microsoft can’t pick a file format and stay with it.  the &lt;code class=&quot;highlighter-rouge&quot;&gt;.suo&lt;/code&gt; file is binary, &lt;code class=&quot;highlighter-rouge&quot;&gt;.config&lt;/code&gt; files are xml, and the &lt;code class=&quot;highlighter-rouge&quot;&gt;.sln&lt;/code&gt; file is basically just simple keys and values.&lt;/p&gt;
</description>
        <pubDate>Wed, 07 Dec 2016 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2016/12/07/Default-Startup-Project-in-Visual-Studio/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2016/12/07/Default-Startup-Project-in-Visual-Studio/</guid>
        
        <category>Visual Studio</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>Converting JSON Types</title>
        <description>&lt;p&gt;In a &lt;a href=&quot;/2015/10/08/generating-json-from-csv-using-powershell/&quot;&gt;post&lt;/a&gt; last year, I found a simple csv to JSON technique that worked well - but today I noticed a significant limitation - all numbers were converted to strings, which isn’t very useful for setting up mock data for &lt;code class=&quot;highlighter-rouge&quot;&gt;TypeScript&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I created the simple &lt;code class=&quot;highlighter-rouge&quot;&gt;node.js&lt;/code&gt; file below to convert an existing JSON file (using the technique above), by specifying one or more values to convert in the file.  It’s not especially robust, but it works for my current needs.  A few comments:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;input file must be &lt;code class=&quot;highlighter-rouge&quot;&gt;utf-8&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;null values converted to string type get set to blank values.&lt;/li&gt;
  &lt;li&gt;strings of &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;null&quot;&lt;/code&gt; are converted to &lt;code class=&quot;highlighter-rouge&quot;&gt;null&lt;/code&gt; for int or float types.&lt;/li&gt;
  &lt;li&gt;you may specify any number of inputs fields.&lt;/li&gt;
  &lt;li&gt;Only supports simple file structures, no nested arrays inside the JSON objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gist&quot;&gt;gist&lt;/h3&gt;

&lt;script src=&quot;https://gist.github.com/brianvp/3fdc794b6cc4330fad5c3705dfc5537d.js&quot;&gt;&lt;/script&gt;

</description>
        <pubDate>Mon, 17 Oct 2016 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2016/10/17/Converting-JSON-Types/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2016/10/17/Converting-JSON-Types/</guid>
        
        <category>csv</category>
        
        <category>JSON</category>
        
        <category>Powershell</category>
        
        
        <category>General</category>
        
      </item>
    
      <item>
        <title>Angular2 Testing Introduction</title>
        <description>&lt;p&gt;Looking at unit testing in Angular2, following the offical &lt;a href=&quot;https://angular.io/docs/ts/latest/guide/testing.html&quot;&gt;docs&lt;/a&gt;.   Unit Testing is a large topic, so this is just an initial look and summary of what I’ve learned so far.&lt;/p&gt;

&lt;h2 id=&quot;why-unit-testing&quot;&gt;Why Unit Testing&lt;/h2&gt;

&lt;p&gt;According to the documentation, Unit Testing as three main purposes:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Guard against regressions in code.  Changing code in an established application is frequently a source of bugs, and having a battery of tests available can alert the developer to issues quickly (assuming there is a test that covers that particular issue)&lt;/li&gt;
  &lt;li&gt;Describe how services, pipes, and components should operate.   Even if you are good at reading code, it is not always easy to visualize what inputs and outputs should look like.&lt;/li&gt;
  &lt;li&gt;Expose design flaws in component design.  If it is hard to write a test for a component, it is a strong sign that the component has design flaws, such as doing too many things, containing unnecessary dependencies, etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;jasmine&quot;&gt;Jasmine&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://jasmine.github.io/&quot;&gt;Jasmine&lt;/a&gt; is a unit testing framework for JavaScript that the Angular team recommends using.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;In Jasmine, a test is known as a “spec”.&lt;/li&gt;
  &lt;li&gt;Specs are stored in a &lt;code class=&quot;highlighter-rouge&quot;&gt;.spec.ts&lt;/code&gt; file.  Each component/service/pipe that has tests should have their own spec file, e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;App.component.spec.ts&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;specs are defined with the global Jasmine function &lt;code class=&quot;highlighter-rouge&quot;&gt;it()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Multiple specs for the same component are known as a “suite”.  A suite is defined with the global jasmine function &lt;code class=&quot;highlighter-rouge&quot;&gt;describe()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The purpose of a spec is to define an assertion about what a component should do, or be.  An assertion is either true or false.  for example, an assertion may be: When we pass 2 to the function square(), the result will be 4&lt;/li&gt;
  &lt;li&gt;To create an assertion in Jasmine, use the &lt;code class=&quot;highlighter-rouge&quot;&gt;expect()&lt;/code&gt; global function, inside an &lt;code class=&quot;highlighter-rouge&quot;&gt;it()&lt;/code&gt; function.&lt;/li&gt;
  &lt;li&gt;Jasmine defines a large number of “Matchers” to be used with &lt;code class=&quot;highlighter-rouge&quot;&gt;expect()&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;toBe()&lt;/li&gt;
      &lt;li&gt;toEqual()&lt;/li&gt;
      &lt;li&gt;toBeUndefined()&lt;/li&gt;
      &lt;li&gt;toBeTruthy()&lt;/li&gt;
      &lt;li&gt;toContain()&lt;/li&gt;
      &lt;li&gt;etc.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-test&quot;&gt;Example Test&lt;/h3&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Math Tests'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Math.pow(2,32) should be 4294967296'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toBe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4294967296&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Math.sqrt(64) should be 8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toBe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;karma&quot;&gt;Karma&lt;/h2&gt;

&lt;p&gt;Jasmine tests need to be run to see if they succeed or fail.  &lt;a href=&quot;https://karma-runner.github.io/1.0/index.html&quot;&gt;Karma&lt;/a&gt; is a unit test runner the Angular team recommends.  Karma runs via the node command: &lt;code class=&quot;highlighter-rouge&quot;&gt;npm test&lt;/code&gt;.  It launches a browser instance that hosts the tests, and can be configured to run all tests automatically after application code is updated.&lt;/p&gt;

&lt;p&gt;After each test run, the results will output in a command window (not the browser).  While a bit verbose, you will be able to clearly see if any tests have failed.&lt;/p&gt;

&lt;p&gt;I’m not using karma for the demo code below, but it is a part of the &lt;a href=&quot;https://cli.angular.io/&quot;&gt;angular-cli&lt;/a&gt; environment&lt;/p&gt;

&lt;h2 id=&quot;jasmine--angular&quot;&gt;Jasmine + Angular&lt;/h2&gt;

&lt;p&gt;By itself, Jasmine knows nothing about an Angular application, so we need to use an Angular test infrastructure to make useful unit tests.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Need to import all components/services that the test/spec(s) will be using&lt;/li&gt;
  &lt;li&gt;use the global Jasmine function &lt;code class=&quot;highlighter-rouge&quot;&gt;beforeEach()&lt;/code&gt; to initialize components.  &lt;code class=&quot;highlighter-rouge&quot;&gt;beforeEach()&lt;/code&gt; runs before every individual &lt;code class=&quot;highlighter-rouge&quot;&gt;it()&lt;/code&gt; execution.&lt;/li&gt;
  &lt;li&gt;some items such as pipes and services do not depend on the angular environment running, and can be tested with little setup in Jasmine, besides instantiating the class.&lt;/li&gt;
  &lt;li&gt;the primary class for interacting with the Angular environment is the &lt;code class=&quot;highlighter-rouge&quot;&gt;TestBed&lt;/code&gt; class. This class is responsible for:
    &lt;ul&gt;
      &lt;li&gt;Instantiating the component that will be tested via &lt;code class=&quot;highlighter-rouge&quot;&gt;TestBed.createComponent()&lt;/code&gt;. this will return a special &lt;code class=&quot;highlighter-rouge&quot;&gt;ComponentFixture&lt;/code&gt; class that contains the component instance as well as a few other testing properties&lt;/li&gt;
      &lt;li&gt;configuring the angular environment via &lt;code class=&quot;highlighter-rouge&quot;&gt;TestBed.configureTestingModule()&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;querying the components template + rendered DOM via &lt;code class=&quot;highlighter-rouge&quot;&gt;debugElement&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;nativeElement&lt;/code&gt; properties of the &lt;code class=&quot;highlighter-rouge&quot;&gt;ComponentFixture&lt;/code&gt;. Note that these elements are hierarchical i.e. &lt;code class=&quot;highlighter-rouge&quot;&gt;debugElement&lt;/code&gt; can contain one or more child &lt;code class=&quot;highlighter-rouge&quot;&gt;debugElement&lt;/code&gt;s&lt;/li&gt;
      &lt;li&gt;provide access to the component’s properties &amp;amp; methods via &lt;code class=&quot;highlighter-rouge&quot;&gt;fixture.componentInstance&lt;/code&gt;.  Note that the properties will not be defined until &lt;code class=&quot;highlighter-rouge&quot;&gt;fixture.detectChanges()&lt;/code&gt; is run.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-component-test&quot;&gt;Example Component Test&lt;/h3&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TestBed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ComponentFixture&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'@angular/core/testing'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;By&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;           &lt;span class=&quot;nx&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'@angular/platform-browser'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DebugElement&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'@angular/core'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SampleComponent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'./sample.component'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;comp&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;    &lt;span class=&quot;nx&quot;&gt;SampleComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fixture&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ComponentFixture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SampleComponnent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'SampleComponent'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
  &lt;span class=&quot;nx&quot;&gt;beforeEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;TestBed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;configureTestingModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;declarations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;SampleComponent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;fixture&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TestBed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SampleComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;comp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fixture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;componentInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Should create the component'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fixture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;debugElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;componentInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toBeTruthy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'H1 title should be: Sample Component'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;de&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fixture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;debugElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;By&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;css&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'h1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;nativeElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;textContent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toEqual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Sample Component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'component property should be 25'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;fixture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;detectChanges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// the component does not initialize until detect changes happens&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;comp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;componentProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toEqual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'component component property should be undefined before initialization'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;comp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;componentProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toBeFalsy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;demo&quot;&gt;Demo&lt;/h3&gt;

&lt;iframe src=&quot;https://embed.plnkr.co/DzLg9W8OyVgNGA2M3qWq/&quot; width=&quot;100%&quot; height=&quot;800&quot;&gt;&lt;/iframe&gt;
</description>
        <pubDate>Tue, 11 Oct 2016 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2016/10/11/Angular2-Testing-Introduction/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2016/10/11/Angular2-Testing-Introduction/</guid>
        
        <category>Angular2</category>
        
        <category>Jasmine</category>
        
        <category>Unit Testing</category>
        
        
        <category>Development</category>
        
        <category>Projects</category>
        
      </item>
    
      <item>
        <title>An Initial Look at RxJS Observables</title>
        <description>&lt;p&gt;A major change in Angular2 from Angular1 is a transition from using &lt;code class=&quot;highlighter-rouge&quot;&gt;promises&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;observables&lt;/code&gt; in the implementation of the new &lt;code class=&quot;highlighter-rouge&quot;&gt;Http&lt;/code&gt; angular module.  You can listen to angular team member Rob Wormald discuss this change in an &lt;a href=&quot;https://youtu.be/JLsR75sf9OY&quot;&gt;April 2016 talk&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Observables are part of &lt;a href=&quot;http://reactivex.io/&quot;&gt;ReactiveX&lt;/a&gt;.  This is a major cross-platform set of libraries designed to work with several languages. The version Angular uses is called &lt;code class=&quot;highlighter-rouge&quot;&gt;RxJS&lt;/code&gt;, also known as &lt;a href=&quot;https://github.com/Reactive-Extensions/RxJS&quot;&gt;Reactive Extensions for JavasScript&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;angular-1-httpget-using-promises&quot;&gt;Angular 1 $http.get() using Promises&lt;/h3&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$http&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'GET'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'http://example.com/api/getStuff'&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;successCallback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;//do stuff&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;angular-2-httpget-using-observables&quot;&gt;Angular 2 http.get() using Observables&lt;/h3&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Observable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;observable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'http://example.com/api/getStuff'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                  &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;observer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;observable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subscribe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//do stuff&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Both Promises and Observables are mechanisms for responding to asynchronous events. Promises are designed to retrieve data exactly once.  Once the &lt;code class=&quot;highlighter-rouge&quot;&gt;.then()&lt;/code&gt; fires, we’re done.  Observables are different.  They have the ability to send data multiple times.  Notice in the above example we “subscribe” to the observable.  This is the equivalent of the &lt;code class=&quot;highlighter-rouge&quot;&gt;.then()&lt;/code&gt; handler in a promise. However, the observable can call this handler &lt;em&gt;multiple&lt;/em&gt; times!&lt;/p&gt;

&lt;p&gt;Here’s an example of how this works:&lt;/p&gt;

&lt;p&gt;First, we create an &lt;code class=&quot;highlighter-rouge&quot;&gt;Observable&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Rx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Observable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;observer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;itemsToEmit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;itemsEmitted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;itemsEmitted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;itemsToEmit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;observer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;itemsEmitted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;observer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onCompleted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The observable is where the async operations happen.  At some point, the Observable needs to pass along some information to the &lt;code class=&quot;highlighter-rouge&quot;&gt;observer&lt;/code&gt;.  This is done with the &lt;code class=&quot;highlighter-rouge&quot;&gt;.onNext()&lt;/code&gt; function.  Notice that this is called multiple times. When this happens we say that we are &lt;strong&gt;emitting&lt;/strong&gt; data to the observer.  It’s the observer’s job to decide what to do with this data, which is handled in the subscription:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   &lt;span class=&quot;nx&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subscribe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emittedValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'subscriptionOneOutput'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;emittedValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;', '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Each time &lt;code class=&quot;highlighter-rouge&quot;&gt;.onNext()&lt;/code&gt; is called, the &lt;code class=&quot;highlighter-rouge&quot;&gt;function(emittedValue)&lt;/code&gt; handler is called.  The observer defines three event handlers for &lt;code class=&quot;highlighter-rouge&quot;&gt;.onNext()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;onError()&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;onCompleted()&lt;/code&gt;.  We can pass these inline to a &lt;code class=&quot;highlighter-rouge&quot;&gt;.subscribe()&lt;/code&gt;, or create the observer directly:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Rx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Observer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emittedValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'subscriptionTwoOutput'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;emittedValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;', '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// onNext()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// onError()&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'subscriptionTwoOutput'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'... done!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// onCompleted()&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subscribe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;observer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;.onError()&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;.onCompleted()&lt;/code&gt; both pass information to the observer, but once they fire the Observable does not emit any more data.  The observer does not need to define handlers for these events.&lt;/p&gt;

&lt;p&gt;You can try this out in this fiddle:&lt;/p&gt;

&lt;h3 id=&quot;jsfiddlenetbrnvndreakrb004&quot;&gt;&lt;a href=&quot;https://jsfiddle.net/brnvndr/eakrb004/&quot;&gt;jsfiddle.net/brnvndr/eakrb004/&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Observables can be created automatically from many sources:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Rx.Observable.from()&lt;/code&gt; - from an array or collection&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Rx.Observable.fromEvent(&amp;lt;target control&amp;gt;, &amp;lt;event name&amp;gt;)&lt;/code&gt; - from a DOM event&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Rx.Observable.fromPromise()&lt;/code&gt; - even from promises&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, Observables define methods for manipulating the data returned to the observer:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;.map( x=&amp;gt; 10 * x)&lt;/code&gt; - transform data.  this is very useful for parsing HTTP response to get the data payload&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;.filter(x =&amp;gt; x &amp;gt; 2)&lt;/code&gt; - limit data returned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good place to start reviewing Observables is in this &lt;a href=&quot;https://xgrommx.github.io/rx-book/&quot;&gt;online book&lt;/a&gt;, but be warned, this is a very deep topic.  You don’t need to understand all of this to use Observables in Angular, but understanding Observables outside the context of Angular will enable you to use them more effectively.&lt;/p&gt;

</description>
        <pubDate>Tue, 04 Oct 2016 00:00:00 +0000</pubDate>
        <link>http://brianvanderplaats.com/2016/10/04/An-Initial-Look-at-RxJS-Observables/</link>
        <guid isPermaLink="true">http://brianvanderplaats.com/2016/10/04/An-Initial-Look-at-RxJS-Observables/</guid>
        
        <category>Angular2</category>
        
        <category>RxJS</category>
        
        <category>Observables</category>
        
        <category>ReactiveX</category>
        
        
        <category>Research</category>
        
      </item>
    
  </channel>
</rss>
