<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="http://weliwita.com/blog/feed.xml" rel="self" type="application/atom+xml" /><link href="http://weliwita.com/blog/" rel="alternate" type="text/html" /><updated>2023-06-23T00:23:14+00:00</updated><id>http://weliwita.com/blog/feed.xml</id><title type="html">Inside{CurlyBraces}</title><subtitle>I read, learn, code and blog</subtitle><author><name>Rasika Weliwita</name><email>weliwita@gmail.com</email></author><entry><title type="html">The Controversial Practice of SSL Inspection - A Betrayal of Trust?</title><link href="http://weliwita.com/blog/2022/06/22/ssl-inspection-a-betrayal-of-trust.html" rel="alternate" type="text/html" title="The Controversial Practice of SSL Inspection - A Betrayal of Trust?" /><published>2022-06-22T16:47:00+00:00</published><updated>2022-06-22T16:47:00+00:00</updated><id>http://weliwita.com/blog/2022/06/22/ssl-inspection-a-betrayal-of-trust</id><content type="html" xml:base="http://weliwita.com/blog/2022/06/22/ssl-inspection-a-betrayal-of-trust.html">&lt;p&gt;There’s a thin line between safeguarding corporate assets and invading personal privacy. One of the practices that continually straddle this line is SSL Inspection, a controversial technique utilized by organizations in the name of security. While it might seem like a fair move to protect proprietary information, let’s dive into why this technique is often seen as intrusive and potentially damaging.&lt;/p&gt;

&lt;p&gt;SSL inspection, or SSL/TLS interception as it’s also known, involves the decryption, inspection, and re-encryption of supposedly secure (HTTPS) internet traffic. In simple terms, it’s akin to intercepting sealed letters, reading their contents, and then resealing them before they reach their final destination.&lt;/p&gt;

&lt;p&gt;This measure, while painted as a security protocol, is more akin to a betrayal of trust. It raises severe privacy concerns and challenges our perception of secure browsing. SSL certificates were initially created to ensure secure communication between two parties, offering a sanctuary of privacy in the chaotic and vast digital world. To manipulate this secure channel for inspection directly contradicts the very essence of its creation.&lt;/p&gt;

&lt;p&gt;Companies may argue that SSL inspection is a necessary evil, a method for blocking malware or preventing data leakage. While these are valid concerns, the crux of the matter lies in the sheer scope of the information a company can access through this method. Personal communications, sensitive data, login credentials, even financial information – all can be read and re-encrypted without the user’s knowledge.&lt;/p&gt;

&lt;p&gt;This practice not only infringes on privacy rights, but it also raises several technical issues. Firstly, it can actually make users more susceptible to certain cyber-attacks. SSL inspection requires the installation of an additional trusted root certificate on the user’s device. If this certificate were to fall into the wrong hands, it could be used to conduct more sophisticated attacks.&lt;/p&gt;

&lt;p&gt;Moreover, it can degrade performance. The inspection process places an extra burden on network hardware, potentially leading to slower connections. Ironically, this creates a paradoxical situation where a security measure ends up degrading the user experience, an outcome that could lead to even more significant security risks if users seek ways to bypass the system altogether.&lt;/p&gt;

&lt;p&gt;It’s essential for companies to maintain a balance between security and privacy. While the intent behind SSL inspection may be well-meaning, it’s important to question whether it’s worth the potential damage to trust, privacy, and even the security it promises to uphold.&lt;/p&gt;

&lt;p&gt;Instead of resorting to such intrusive measures, organizations could invest in user education and promote good cyber hygiene. Perhaps the key to a more secure digital landscape lies not in policing every move, but in fostering a culture of responsibility and understanding.&lt;/p&gt;

&lt;p&gt;Let’s ensure that the SSL in HTTPS stands as a beacon of security, not an illusion of it.&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="IAM" /><category term="security-fundamentals" /><summary type="html">There’s a thin line between safeguarding corporate assets and invading personal privacy. One of the practices that continually straddle this line is SSL Inspection, a controversial technique utilized by organizations in the name of security. While it might seem like a fair move to protect proprietary information, let’s dive into why this technique is often seen as intrusive and potentially damaging.</summary></entry><entry><title type="html">Attaching Windbg to Managed Application</title><link href="http://weliwita.com/blog/2017/04/10/attaching-windbg-to-managed-application.html" rel="alternate" type="text/html" title="Attaching Windbg to Managed Application" /><published>2017-04-10T16:47:00+00:00</published><updated>2017-04-10T16:47:00+00:00</updated><id>http://weliwita.com/blog/2017/04/10/attaching-windbg-to-managed-application</id><content type="html" xml:base="http://weliwita.com/blog/2017/04/10/attaching-windbg-to-managed-application.html">&lt;p&gt;You may want time to time dig into the implementation detail of the code you are working with(e.g. how they are represented in RAM). Windbg with the help of SOS extension gives you lot of those implementation detail. But when the initial breakpoint is hit the CLR has not yet been loaded and Windbg doesn’t allow you to load SOS. What you can do here is that temporary set a &lt;code&gt;Console.ReadLine()&lt;/code&gt; in your code and hit Ctrl + Break to break the debugger at your desired location.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/weliwita/5b57ce9859b8fd140c09002f991ce395.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Launch the executable from WinDbg, and then input “g” command to let debuggee process to continue until readLine statement. press &lt;code&gt;Ctrl + Break&lt;/code&gt; or use &lt;code&gt;menu-&amp;gt;Debug-&amp;gt;Break&lt;/code&gt; and then input command &lt;code&gt;.loadby sos clr&lt;/code&gt; to load the SOS extension.&lt;/p&gt;
&lt;h4&gt;The Infinite Loop Technique&lt;/h4&gt;
&lt;p&gt;In case you are not working with a console app and you still need to break into a specific location you can use infinite loop technique. In this technique, you are injecting temporary infinite loop, based on the value of a global Boolean flag, into your code.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/weliwita/e40e988b4dd2a53ee6e5cea35622de00.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;When you hit &lt;code&gt;g&lt;/code&gt; in the debugger it will stop at the infinite loop. You can then break into the loop and set the boolean flag to true in memory window. To track the location of the local variable in the stack you can use &lt;code&gt; !clrstack -a &lt;/code&gt; command. After setting the variable to true you can continue the execution by hitting &lt;code&gt;g&lt;/code&gt; command to get out of the infinite loop.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/blog/assets/img/posts/20170410-clrstack.png&quot;&gt;&lt;img src=&quot;/blog/assets/img/posts/20170410-clrstack.png&quot; alt=&quot;Clr stack&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="windbg" /><summary type="html">You may want time to time dig into the implementation detail of the code you are working with(e.g. how they are represented in RAM). Windbg with the help of SOS extension gives you lot of those implementation detail. But when the initial breakpoint is hit the CLR has not yet been loaded and Windbg doesn’t allow you to load SOS. What you can do here is that temporary set a Console.ReadLine() in your code and hit Ctrl + Break to break the debugger at your desired location.</summary></entry><entry><title type="html">VS Code Integrated Terminal with Visual Studio Command Prompt</title><link href="http://weliwita.com/blog/2017/01/21/visual-studio-code-integrated-terminal-with-visual-studio-command-prompt.html" rel="alternate" type="text/html" title="VS Code Integrated Terminal with Visual Studio Command Prompt" /><published>2017-01-21T00:43:00+00:00</published><updated>2017-01-21T00:43:00+00:00</updated><id>http://weliwita.com/blog/2017/01/21/visual-studio-code-integrated-terminal-with-visual-studio-command-prompt</id><content type="html" xml:base="http://weliwita.com/blog/2017/01/21/visual-studio-code-integrated-terminal-with-visual-studio-command-prompt.html">&lt;p&gt;It is quite convenient to use visual studio developer command prompt from VS Code integrated terminal.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;You need to find the correct command line argument to start the visual studio command prompt. You can do that by checking properties of the shortcut icon.
  &lt;img src=&quot;/blog/assets/img/posts/20170121-Shortcut.png&quot; alt=&quot;Configuring shortcut&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Copy the shortcut link text&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;    &lt;span class=&quot;nv&quot;&gt;%comspec%&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/k &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;C&lt;/span&gt;:\Program &lt;span class=&quot;kd&quot;&gt;Files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;x86&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;\Microsoft &lt;span class=&quot;kd&quot;&gt;Visual&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;Studio&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;12&lt;/span&gt;.0\Common7\Tools\VsDevCmd.bat&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;
    &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;Then go to user preferences of VS Code and update the ShellArgs property with correct parameters you copied from the step 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;terminal.integrated.shellArgs.windows&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/k&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;C:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Program Files (x86)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Microsoft Visual Studio 12.0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Common7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Tools&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;VsDevCmd.bat&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;If everything goes ok you should be able to use developer command prompt within VS Code integrated shell as follows
  &lt;img src=&quot;/blog/assets/img/posts/20170121-Compiling.png&quot; alt=&quot;Using command prompt&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;troubleshooting&quot;&gt;Troubleshooting&lt;/h3&gt;

&lt;p&gt;If you have trouble executing commands you can check if correct command parameters are passed into the integrated terminal by using the process explorer.
&lt;a href=&quot;/blog/assets/img/posts/20170121-CommandLine.png&quot;&gt;&lt;img src=&quot;/blog/assets/img/posts/20170121-CommandLine.png&quot; alt=&quot;Using process explorer&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are running an older version of VS Code you may need to upgrade to the latest version due to this &lt;a href=&quot;https://github.com/Microsoft/vscode/issues/7266&quot;&gt;bug&lt;/a&gt;&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="vscode" /><summary type="html">It is quite convenient to use visual studio developer command prompt from VS Code integrated terminal.</summary></entry><entry><title type="html">Transport Layer Security (TLS)</title><link href="http://weliwita.com/blog/2015/05/13/transport-layer-security-tls.html" rel="alternate" type="text/html" title="Transport Layer Security (TLS)" /><published>2015-05-13T05:42:00+00:00</published><updated>2015-05-13T05:42:00+00:00</updated><id>http://weliwita.com/blog/2015/05/13/transport-layer-security-tls</id><content type="html" xml:base="http://weliwita.com/blog/2015/05/13/transport-layer-security-tls.html">&lt;h4&gt;What is Transport Layer&lt;/h4&gt;
&lt;p&gt;In the Open Systems Interconnection(OSI) model the transport layer is most often referred to as Layer 4. It resides between network layer and session layer. Almost all modern authentication protocols rely heavily on transport layer security.&lt;/p&gt;
&lt;h3&gt;What is Transport Layer Security (TLS)&lt;/h3&gt;
&lt;p&gt;TLS is a protocol designed to provide communications security over a computer network. It is the successor of the SSL. The protocol is defined &lt;a href=&quot;http://tools.ietf.org/html/rfc5246&quot;&gt;here&lt;/a&gt;.
It can provide privacy and data integrity between two communicating applications.&lt;/p&gt;
&lt;h4&gt;Why it is important&lt;/h4&gt;
&lt;p&gt;It allows unprotected protocols like HTTP to travel across a secure tunnel and adds following features to the HTTP&lt;/p&gt;
&lt;ol&gt;

&lt;li&gt;
&lt;h5&gt;Server Authentication&lt;/h5&gt;
Make sure that we are talking to the server that we are expecting to call. Without TLS we have no guarantee that the server at the other end of the channel is the same server that we intended to call.This is achieved by using x.509 certificates
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;Integrity Protection&lt;/h5&gt;
Make sure that middleman between client and server can't modify the content. This is also achieved by using x.509 certificates with hashed content.
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;Replay protection&lt;/h5&gt;
Make sure same request can't be repeatedly send to the server.&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;confidentiality&lt;/h5&gt;
Make sure the messages client send to the server are properly encrypted so that nobody in the middle cannot see the content on the wire.
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In modern web applications TLS is a must ensuring Confidentiality and Integrity without affecting Availability.&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="IAM" /><category term="security-fundamentals" /><summary type="html">What is Transport Layer In the Open Systems Interconnection(OSI) model the transport layer is most often referred to as Layer 4. It resides between network layer and session layer. Almost all modern authentication protocols rely heavily on transport layer security. What is Transport Layer Security (TLS) TLS is a protocol designed to provide communications security over a computer network. It is the successor of the SSL. The protocol is defined here. It can provide privacy and data integrity between two communicating applications. Why it is important It allows unprotected protocols like HTTP to travel across a secure tunnel and adds following features to the HTTP</summary></entry><entry><title type="html">Security Fundamentals 1 - What is CIA</title><link href="http://weliwita.com/blog/2015/04/07/security-fundamentals-1-what-is-cia.html" rel="alternate" type="text/html" title="Security Fundamentals 1 - What is CIA" /><published>2015-04-07T16:36:00+00:00</published><updated>2015-04-07T16:36:00+00:00</updated><id>http://weliwita.com/blog/2015/04/07/security-fundamentals-1-what-is-cia</id><content type="html" xml:base="http://weliwita.com/blog/2015/04/07/security-fundamentals-1-what-is-cia.html">&lt;p&gt;In computer security CIA is the abbreviation for following terms.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Confidentiality&lt;/li&gt;
	&lt;li&gt;Integrity&lt;/li&gt;
	&lt;li&gt;Availability&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Confidentiality&lt;/h3&gt;
&lt;p&gt;This means keeping the information secure from unauthorized access.
The technologies involved are encryption, authentication, and authorization(access control)&lt;/p&gt;

&lt;h3&gt;Integrity&lt;/h3&gt;
&lt;p&gt;This means information should be consistent, accurate, and valid.
This is achieved by using Hashing algorithms.&lt;/p&gt;

&lt;h3&gt;Availability&lt;/h3&gt;
&lt;p&gt;This means information should be available when required. 
This is achieved by using correct SLAs and preventing DOS attacks&lt;/p&gt;

&lt;h4&gt;Related Links:&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;http://channel9.msdn.com/Series/Security-Fundamentals&quot;&gt;Security-Fundamentals&lt;/a&gt;&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="IAM" /><category term="security-fundamentals" /><summary type="html">In computer security CIA is the abbreviation for following terms. Confidentiality Integrity Availability Confidentiality This means keeping the information secure from unauthorized access. The technologies involved are encryption, authentication, and authorization(access control)</summary></entry><entry><title type="html">How to Issue the Claims About the User</title><link href="http://weliwita.com/blog/2015/03/30/how-to-issue-the-claims-about-the-user.html" rel="alternate" type="text/html" title="How to Issue the Claims About the User" /><published>2015-03-30T11:43:00+00:00</published><updated>2015-03-30T11:43:00+00:00</updated><id>http://weliwita.com/blog/2015/03/30/how-to-issue-the-claims-about-the-user</id><content type="html" xml:base="http://weliwita.com/blog/2015/03/30/how-to-issue-the-claims-about-the-user.html">&lt;p&gt;This post is part of few introductory blog posts about the identity in DotNet Application. In this post, let’s talk about the process of storing and issuing claims about the user.&lt;/p&gt;

&lt;p&gt;The claims are basically key-value pairs that say something about the authenticated user. These key-value pairs should be stored somewhere against the user’s identity. As we are going through the default ASP.NET template, Let’s see how claims are stored in the DB.&lt;/p&gt;

&lt;p&gt;You can see following connection string in the web.config file&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;DefaultConnection&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;connectionString=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-AuthenticationExamples-20171022124844.mdf;Initial Catalog=aspnet-AuthenticationExamples-20171022124844;Integrated Security=True&quot;&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;providerName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Data.SqlClient&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Let’s use either server explorer or SQL server management studio to see the DB.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/blog/assets/img/posts/20170330-IdentityDB.png&quot;&gt;&lt;img src=&quot;/blog/assets/img/posts/20170330-IdentityDB.png&quot; alt=&quot;IdentityDB&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASPNETUserClaims&lt;/code&gt; table store the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClaimType&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClaimValue&lt;/code&gt; pairs against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserId&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let’s see how we these values are stored and retrieved using code.&lt;/p&gt;

&lt;p&gt;Let’s create simple console app and use the default connection string(see above) used in asp.net default template.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PM&amp;gt; Install-package Microsoft.AspNet.Identity.Core -Version 2.2.1
PM&amp;gt; Install-package Microsoft.AspNet.Identity.EntityFramework -Version 2.2.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The SignInManager uses the UserManager class internally to validate user password. UserManager is the main service that store and retrieve users and claims from the data store.&lt;/p&gt;

&lt;p&gt;So using our console app we can add a claim for an existing user as follows&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userStore&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;UserStore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IdentityUser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userManager&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;UserManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IdentityUser&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;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userStore&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;user&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FindByEmail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;weliwita@gmail.com&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;claim&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;IdentityUserClaim&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;ClaimType&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;claim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClaimValue&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Administrator&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;user&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;Add&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;userManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;/blog/assets/img/posts/20170330-ClaimsInCode.png&quot;&gt;&lt;img src=&quot;/blog/assets/img/posts/20170330-ClaimsInCode.png&quot; alt=&quot;IdentityDB&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, we can retrieve claims from user manager as follows&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userStore&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;UserStore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IdentityUser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userManager&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;UserManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IdentityUser&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;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userStore&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;user&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FindByEmail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;weliwita@gmail.com&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;claims&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&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;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;/blog/assets/img/posts/20170330-ClaimsListInCode.png&quot;&gt;&lt;img src=&quot;/blog/assets/img/posts/20170330-ClaimsListInCode.png&quot; alt=&quot;IdentityDB&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;UserManager is capable of performing many tasks related user. e.g account lockout, two-factor authentication using SMS service etc… See the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dn613290(v=vs.108).aspx&quot;&gt;documentation&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;Now we know how claims are stored and retrieved using the ASP.NET default template. The template uses libraries(see Nuget Packages above) belong to ASP.NET Identity. There are other similar libraries to manage users and claims. Membership reboot is one such library(Update-It is deprecated as of 2017).&lt;/p&gt;

&lt;p&gt;In the default template, UserManagement is part of the application itself. As you can see in the template itself, it is a lot of code and learning curve for an application developer. So nowadays applications tend not to handle user management.&lt;/p&gt;

&lt;p&gt;If an application doesn’t want to handle user management who can? Let’s discuss it in another post.&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="IAM" /><category term="authentication" /><category term="claims" /><summary type="html">This post is part of few introductory blog posts about the identity in DotNet Application. In this post, let’s talk about the process of storing and issuing claims about the user.</summary></entry><entry><title type="html">HTML Helper to Display the Logged In User’s DisplayName</title><link href="http://weliwita.com/blog/2015/03/22/html-helper-to-display-the-current-logged-in-users-displayname.html" rel="alternate" type="text/html" title="HTML Helper to Display the Logged In User’s DisplayName" /><published>2015-03-22T07:01:00+00:00</published><updated>2015-03-22T07:01:00+00:00</updated><id>http://weliwita.com/blog/2015/03/22/html-helper-to-display-the-current-logged-in-users-displayname</id><content type="html" xml:base="http://weliwita.com/blog/2015/03/22/html-helper-to-display-the-current-logged-in-users-displayname.html">&lt;p&gt;It is pretty common to display the logged-in user in various places in a website. We can create an HTML helper to display the logged-in user’s name.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&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;n&quot;&gt;MvcHtmlString&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UserDisplayName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HtmlHelper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlHelper&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;htmlHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ViewContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAuthenticated&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;ClaimsPrincipal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;principal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ViewContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClaimsPrincipal&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;principal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&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;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidCastException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The current principal is not a claims principal.&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;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nameClaim&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;principal&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;FirstOrDefault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&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;s&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//use any claim that need to be displayed as UserDisplayName&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;nameClaim&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&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;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MvcHtmlString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nameClaim&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MvcHtmlString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Anonymous&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;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then the helper can be used on the navigation bar or any other UI widget that need to display user name.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;nav navbar-nav navbar-right&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;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dropdown navbar-text&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        Hi, @Html.UserDisplayName()
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="IAM" /><category term="claims" /><summary type="html">It is pretty common to display the logged-in user in various places in a website. We can create an HTML helper to display the logged-in user’s name.</summary></entry><entry><title type="html">List All the Claims of the Current User</title><link href="http://weliwita.com/blog/2015/03/09/list-all-the-claims-current-user-has.html" rel="alternate" type="text/html" title="List All the Claims of the Current User" /><published>2015-03-09T05:02:00+00:00</published><updated>2015-03-09T05:02:00+00:00</updated><id>http://weliwita.com/blog/2015/03/09/list-all-the-claims-current-user-has</id><content type="html" xml:base="http://weliwita.com/blog/2015/03/09/list-all-the-claims-current-user-has.html">&lt;p&gt;The easiest way to display all the claims that was issued to the current user is to query the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.identity(v=vs.110).aspx&quot; title=&quot;Identity&quot;&gt;Identity&lt;/a&gt; property of the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal(v=vs.110).aspx&quot; title=&quot;IPrincipal&quot;&gt;IPrincipal &lt;/a&gt;interface.&lt;/p&gt;

&lt;p&gt;Here is the sample code in razor view.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;err&quot;&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;identity&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;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;ClaimsIdentity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&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;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;ul&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;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&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;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;li&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;@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;-&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;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;li&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;ul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="IAM" /><category term="claims" /><summary type="html">The easiest way to display all the claims that was issued to the current user is to query the Identity property of the IPrincipal interface.</summary></entry><entry><title type="html">Running web application locally as a predefined user using OWIN Middleware</title><link href="http://weliwita.com/blog/2015/03/01/running-web-application-locally-as-a-predefined-user-using-owin-middleware.html" rel="alternate" type="text/html" title="Running web application locally as a predefined user using OWIN Middleware" /><published>2015-03-01T09:29:00+00:00</published><updated>2015-03-01T09:29:00+00:00</updated><id>http://weliwita.com/blog/2015/03/01/running-web-application-locally-as-a-predefined-user-using-owin-middleware</id><content type="html" xml:base="http://weliwita.com/blog/2015/03/01/running-web-application-locally-as-a-predefined-user-using-owin-middleware.html">&lt;p&gt;It is quite common that we sometimes need to run the application without all the identity server redirection dance at development time. It is possible to assign common identity to all the users running the application locally as follows.&lt;/p&gt;

&lt;p&gt;Create a custom authentication middleware as follows.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&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;LocalAuthenticationMiddleware&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OwinMiddleware&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;nf&quot;&gt;LocalAuthenticationMiddleware2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OwinMiddleware&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&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;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&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;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IOwinContext&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;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&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;Response&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;request&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;Request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;OnSendingHeaders&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&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;resp&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;OwinResponse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&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;resp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StatusCode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;401&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;resp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Headers&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;s&quot;&gt;&quot;WWW-Authenticate&quot;&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;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Basic&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;resp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StatusCode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;403&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;resp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReasonPhrase&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Forbidden&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;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// use this identity only if running locally&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;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Uri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsLoopback&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;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;k&quot;&gt;new&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;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;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Administrator&quot;&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;NameIdentifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0001&quot;&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;s&quot;&gt;&quot;SiteAdministrator&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;kt&quot;&gt;var&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;new&lt;/span&gt; &lt;span class=&quot;nf&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;s&quot;&gt;&quot;Basic&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&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;ClaimsPrincipal&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;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Invoke&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;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then use the custom authentication middleware in the application startup&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&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;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LocalAuthenticationMiddleware&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Access the claims via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Controller.User&lt;/code&gt; API.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nameClaim&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;User&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClaimsPrincipal&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;FirstOrDefault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&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;s&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://lbadri.wordpress.com/2013/07/13/basic-authentication-with-asp-net-web-api-using-owin-middleware/&quot;&gt;basic authentication&lt;/a&gt;&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="IAM" /><category term="authentication" /><category term="claims" /><summary type="html">It is quite common that we sometimes need to run the application without all the identity server redirection dance at development time. It is possible to assign common identity to all the users running the application locally as follows.</summary></entry><entry><title type="html">How to use Identity in a .NET Web Application - Part 1</title><link href="http://weliwita.com/blog/2015/02/15/how-to-use-identity-in-a-net-web-application-part1.html" rel="alternate" type="text/html" title="How to use Identity in a .NET Web Application - Part 1" /><published>2015-02-15T08:57:00+00:00</published><updated>2015-02-15T08:57:00+00:00</updated><id>http://weliwita.com/blog/2015/02/15/how-to-use-identity-in-a-net-web-application-part1</id><content type="html" xml:base="http://weliwita.com/blog/2015/02/15/how-to-use-identity-in-a-net-web-application-part1.html">&lt;p&gt;In a previous &lt;a href=&quot;/blog/2015/02/12/how-to-use-currentprincipal.html&quot;&gt;post&lt;/a&gt;, I described how does a user is represented inside a DotNet application using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread.CurentPrinciple&lt;/code&gt; property. In this post and next, I will go through the default visual studio 2013 MVC template to see how the identity is set for an MVC application.&lt;/p&gt;

&lt;p&gt;Let’s say in the default template you put authorize attribute in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;About&lt;/code&gt; action in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Home&lt;/code&gt; controller like follows.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&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;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;About&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;Message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Your application description page.&quot;&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;/figure&gt;

&lt;p&gt;If you now try to navigate to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/Home/About&lt;/code&gt; you will receive is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;302&lt;/code&gt; redirect to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/Account/Login?ReturnUrl=%2FHome%2FAbout&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s see what is happening inside the OWIN pipeline.&lt;/p&gt;

&lt;p&gt;To get details within OWIN pipeline we can inject some additional logging components as follows. Here we have injected two middleware components before and after cookie authentication middleware.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&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;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&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;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1 ==&amp;gt;request, before cookie auth&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1 ==&amp;gt;context.response=&quot;&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;Response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StatusCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;4 &amp;lt;==response, after cookie auth&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;4 ==&amp;gt;context.response=&quot;&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;Response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StatusCode&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;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;n&quot;&gt;CookieAuthenticationOptions&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;DefaultAuthenticationTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ApplicationCookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LoginPath&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;PathString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/Account/Login&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;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;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&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;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;2 ==&amp;gt;context.response=&quot;&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;Response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StatusCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;2 ==&amp;gt;request, before controller action&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;3 &amp;lt;==response, after controller action&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;3 ==&amp;gt;context.response=&quot;&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;Response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StatusCode&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;/figure&gt;

&lt;p&gt;If you now issue a request to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/Home/About&lt;/code&gt; page you get following debug output.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1 ==&amp;gt;request, before cookie auth
1 ==&amp;gt;context.response=200
2 ==&amp;gt;context.response=200
2 ==&amp;gt;request, before controller action
3 &amp;lt;==response, after controller action
3 &amp;lt;==context.response=401
4 &amp;lt;==response, after cookie auth
4 &amp;lt;==context.response=302
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;See how response changed from 200 to 401 after the controller action. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Authorize&lt;/code&gt; attribute has detected that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread.CurentPrinciple&lt;/code&gt; doesn’t contain a valid identity and has issued a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;401 Unauthorized&lt;/code&gt; response.&lt;/p&gt;

&lt;p&gt;This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;401&lt;/code&gt; response then go through the cookie authentication middleware and it then changes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;401 Unauthorized&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;302 Redirect&lt;/code&gt;. The cookie authentication middleware knows where user should be redirected to get authenticated by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LoginPath&lt;/code&gt;
property. It also knows the URL user originally requested. So it can easily construct the redirect URL to the login page as follows&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/Account/Login?ReturnUrl=%2FHome%2FAbout&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s see how login process takes place in the next post.&lt;/p&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;
&lt;p&gt;Brock Allen has this great &lt;a href=&quot;http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/&quot; title=&quot;article&quot;&gt;article &lt;/a&gt;about the cookie authentication middleware.&lt;/p&gt;</content><author><name>weliwita@gmail.com</name></author><category term="Visual Studio" /><category term="IAM" /><category term="authentication" /><category term="claims" /><summary type="html">In a previous post, I described how does a user is represented inside a DotNet application using Thread.CurentPrinciple property. In this post and next, I will go through the default visual studio 2013 MVC template to see how the identity is set for an MVC application.</summary></entry></feed>