﻿<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://developers.de/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Armin Kalajdzija Posts</title><subtitle type="html">&lt;div style="font-size:small;position:relative;top:-10px;"&gt;Blog about Life, the Universe and .Net :)&lt;/div&gt;</subtitle><id>http://developers.de/blogs/armin_kalajdzija/atom.aspx</id><link rel="alternate" type="text/html" href="http://developers.de/blogs/armin_kalajdzija/default.aspx" /><link rel="self" type="application/atom+xml" href="http://developers.de/blogs/armin_kalajdzija/atom.aspx" /><generator uri="http://communityserver.org" version="4.0.30619.63">Community Server</generator><updated>2013-01-16T14:35:39Z</updated><entry><title>ReSharper Ultimate – System.ApplicationException: Error loading settings file fix!</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2017/01/16/resharper-ultimate-system-applicationexception-error-loading-settings-file-fix.aspx" /><id>/blogs/armin_kalajdzija/archive/2017/01/16/resharper-ultimate-system-applicationexception-error-loading-settings-file-fix.aspx</id><published>2017-01-16T12:22:31Z</published><updated>2017-01-16T12:22:31Z</updated><content type="html">&lt;p&gt;Something really strange happened today while I was running my Unit Tests or to be exact while I was making a Coverage analysis of my Project. In the middle of the Execution my Laptop stopped responding (bad Docking Station Drivers under Windows 10), so I had to do a Hard Reset.
&lt;/p&gt;&lt;p&gt;After that, every time I start Unit Testing Session under ReSharper I get following Error Message:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/011617_5F00_1222_5F00_ReSharperUl1.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;So, if you get this kind of Error Message, all you have to do is the delete the corrupted Settings file of a NUnit Testing Engine that is located under: &lt;strong&gt;C:\Users\&amp;lt;username&amp;gt;\AppData\Local\NUnit&lt;/strong&gt;. File is called &lt;strong&gt;Nunit30Settings.xml&lt;/strong&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1805075" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Visual Studio" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Visual+Studio/default.aspx"></category><category term="Unit Testing" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Unit+Testing/default.aspx"></category><category term="ReSharper" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ReSharper/default.aspx"></category></entry><entry><title>Implementing WebSockets in ASP.NET 4.6 using WebSocketHandler</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/12/06/implementing-websockets-in-asp-net-4-6-using-websockethandler.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/12/06/implementing-websockets-in-asp-net-4-6-using-websockethandler.aspx</id><published>2016-12-06T11:17:52Z</published><updated>2016-12-06T11:17:52Z</updated><content type="html">&lt;p&gt;This time I want to share with you a short example of how to implement WebSockets in your ASP.NET 4.6 Web API / AngularJS Application by using WebSocketHandler from Microsoft.WebSockets nuget Package.
&lt;/p&gt;&lt;h3&gt;Installing a Microsoft.WebSockets Nuget package
&lt;/h3&gt;&lt;p&gt;First of all you need to install the Microsoft.WebSockets from Nuget using Package Manager Console by typing: 
&lt;/p&gt;&lt;p&gt;&lt;em&gt;PM&amp;gt; Install-Package Microsoft.WebSockets&lt;/em&gt;
	&lt;/p&gt;&lt;p&gt;Note that this Package is almost one year old now and not actively developed (says official Nuget package description, current version 0.2.3.1) but it is the best implementation I found so far if you are using it with ASP.NET 4.6. 
&lt;/p&gt;&lt;p&gt;You can also find a .NET Framework implementation of WebSockets in System.Web.WebSockets Namespace but I find it little bit impractical and if you want to implement it to be stable and multiclient, you will actually end up implementing the WebSocketHandler from Microsoft.WebSockets package.
&lt;/p&gt;&lt;h3&gt;Defining your Message Object
&lt;/h3&gt;&lt;p&gt;Here is a simple model of a Message Object that we will be sending to Client:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;SimpleMessage&lt;span style="color:black;"&gt;
							&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;JsonProperty&lt;span style="color:black;"&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;span style="color:black;"&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;long&lt;span style="color:black;"&gt; Id { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;JsonProperty&lt;span style="color:black;"&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;body&amp;quot;&lt;span style="color:black;"&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;dynamic&lt;span style="color:black;"&gt; Body { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;JsonProperty&lt;span style="color:black;"&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;sessionId&amp;quot;&lt;span style="color:black;"&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;Guid&lt;span style="color:black;"&gt; SessionId { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}&lt;/span&gt;
	&lt;/p&gt;&lt;h3&gt;Implementing your WebSocketHandler
&lt;/h3&gt;&lt;p&gt;Now we need to implement our own WebSocketHandler. I called it for purpose of this Post &amp;quot;SimpleWebSocketHandler&amp;quot; and it looks like this:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;SimpleWebSocketHandler&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;WebSocketHandler&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;private&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;static&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;WebSocketCollection&lt;span style="color:black;"&gt; _wsClients = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
										&lt;span style="color:#2b91af;"&gt;WebSocketCollection&lt;span style="color:black;"&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;override&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; OnOpen()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;_wsClients.Add(&lt;span style="color:blue;"&gt;this&lt;span style="color:black;"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;base&lt;span style="color:black;"&gt;.OnOpen(); 
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;override&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; OnClose()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;wsClients.Remove(&lt;span style="color:blue;"&gt;this&lt;span style="color:black;"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;base&lt;span style="color:black;"&gt;.OnClose();
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; SendMessage(&lt;span style="color:#2b91af;"&gt;SimpleMessage&lt;span style="color:black;"&gt; message)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;.IsNullOrEmpty(message.SessionId))
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;SendBroadcastMessage(message);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;else&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;SendMessage(message, message.SessionId);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; SendMessage(&lt;span style="color:#2b91af;"&gt;SimpleMessage&lt;span style="color:black;"&gt; message, &lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; sessionId)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; webSockets = _wsClients.Where(s =&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; httpCookie = s.WebSocketContext.Cookies[&lt;span style="color:#a31515;"&gt;&amp;quot;SessionId&amp;quot;&lt;span style="color:black;"&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt; httpCookie != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt; &amp;amp;&amp;amp; httpCookie.Value == sessionId;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;foreach&lt;span style="color:black;"&gt; (&lt;span style="color:blue;"&gt;var&lt;span style="color:black;"&gt; socket &lt;span style="color:blue;"&gt;in&lt;span style="color:black;"&gt; webSockets)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;socket.Send(&lt;span style="color:#2b91af;"&gt;JsonConvert&lt;span style="color:black;"&gt;.SerializeObject(message));
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; SendBroadcastMessage(&lt;span style="color:#2b91af;"&gt;SimpleMessage&lt;span style="color:black;"&gt; message)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;_wsClients.Broadcast(&lt;span style="color:#2b91af;"&gt;JsonConvert&lt;span style="color:black;"&gt;.SerializeObject(message));
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;As you can see, I am also using a WebSocketCollection object for storing my opened WebSocket connections. That way I can simply send a Broadcast message to all WebSocket connections or filter the one that has my Session ID as a cookie (that will be place upon first web request in Startup.cs below) and send the message only to that client.
&lt;/p&gt;&lt;h3&gt;Changes in Application Startup
&lt;/h3&gt;&lt;p&gt;Now we have to make sure that we initialize our &amp;quot;SimpleWebSocketHandler&amp;quot; upon application start. If you are using an OWIN Middleware and Unity Container (and you should), your Startup.cs should look something like this:
&lt;/p&gt;&lt;p&gt;&lt;em&gt;Startup.cs
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;partial&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
								&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Configuration(&lt;span style="color:#2b91af;"&gt;IAppBuilder&lt;span style="color:black;"&gt; app)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;    &lt;span style="color:blue;"&gt;var&lt;span style="color:black;"&gt; container = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
							&lt;span style="color:#2b91af;"&gt;UnityContainer&lt;span style="color:black;"&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;    container
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;        .RegisterType&amp;lt;&lt;span style="color:#2b91af;"&gt;SimpleWebSocketHandler&lt;span style="color:black;"&gt;&amp;gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:#2b91af;font-family:Consolas;font-size:9pt;"&gt;DependencyResolver&lt;span style="color:black;"&gt;.SetResolver(&lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;UnityDependencyResolver&lt;span style="color:black;"&gt;(container));
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// register dependency resolver for WebAPI&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;configuration.DependencyResolver = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt; Unity.WebApi.&lt;span style="color:#2b91af;"&gt;UnityDependencyResolver&lt;span style="color:black;"&gt;(container);    
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;    &lt;span style="color:#2b91af;"&gt;UnityConfig&lt;span style="color:black;"&gt;.Initialise(&lt;span style="color:#2b91af;"&gt;GlobalConfiguration&lt;span style="color:black;"&gt;.Configuration);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ConfigureAuth(app);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ConfigureWebSocketListeners();
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.Use(new Func&amp;lt;AppFunc, AppFunc&amp;gt;(next =&amp;gt; (async env =&amp;gt;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (context.Request.Cookies[&lt;span style="color:#a31515;"&gt;&amp;quot;SessionId&amp;quot;&lt;span style="color:black;"&gt;] == &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;context.Response.Cookies.Append(&lt;span style="color:#a31515;"&gt;&amp;quot;SessionId&amp;quot;&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;Guid&lt;span style="color:black;"&gt;.NewGuid().ToString(), &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
									&lt;span style="color:#2b91af;"&gt;CookieOptions&lt;span style="color:black;"&gt;() { HttpOnly = &lt;span style="color:blue;"&gt;false&lt;span style="color:black;"&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;            
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;            await next.Invoke(env);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;})));
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;Startup.WebSockets.cs
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;partial&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
								&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; ConfigureWebSocketListeners(&lt;span style="color:#2b91af;"&gt;SimpleWebSocketHandler&lt;span style="color:black;"&gt; _webSocket)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ListenForSystemMessages(_webSocket);
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;private&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; ListenForSystemMessages(&lt;span style="color:#2b91af;"&gt;SimpleWebSocketHandler&lt;span style="color:black;"&gt; webSocket)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// Here you can register any event that should send the messages to users.
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// It can be for example ServiceBus.OnMessage or anything else.
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// queueClient.OnMessage(receivedMessage =&amp;gt;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// {
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//    var msg = JsonConvert.DeserializeObject&amp;lt;SimpleMessage&amp;gt;(receivedMessage.GetBody&amp;lt;string&amp;gt;());
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//    webSocket.SendMessage(msg);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//    queueClient.Complete(receivedMessage.LockToken);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// });&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Note that I am injecting my SimpleWebSocketHandler over Unity Container that we configured in step above.
&lt;/p&gt;&lt;h3&gt;Implementing your Web API Controller 
&lt;/h3&gt;&lt;p&gt;So now what we need to do is the register one WebAPI route/method (&amp;quot;GetMessages&amp;quot;) that will be used as our WebSocket connection endpoint and will be open for whole user session. 
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;RoutePrefix&lt;span style="color:black;"&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;api/messages&amp;quot;&lt;span style="color:black;"&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;MessageController&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;ApiController&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;private&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;readonly&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;SimpleWebSocketHandler&lt;span style="color:black;"&gt; _webSocketHandler;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt; FrontendSystemMessageController(&lt;span style="color:#2b91af;"&gt;SimpleWebSocketHandler&lt;span style="color:black;"&gt; webSocketHandler)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;_webSocketHandler = webSocketHandler;
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;HttpGet&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Route&lt;span style="color:black;"&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;GetMessages&amp;quot;&lt;span style="color:black;"&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;async&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;Task&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;HttpResponseMessage&lt;span style="color:black;"&gt;&amp;gt; Messages()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; currentContext = &lt;span style="color:#2b91af;"&gt;HttpContext&lt;span style="color:black;"&gt;.Current;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;await&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;Task&lt;span style="color:black;"&gt;.Run(() =&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (currentContext.IsWebSocketRequest || currentContext.IsWebSocketRequestUpgrading)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;currentContext.AcceptWebSocketRequest(ProcessWebSocketRequest);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt; Request.CreateResponse(&lt;span style="color:#2b91af;"&gt;HttpStatusCode&lt;span style="color:black;"&gt;.SwitchingProtocols);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt; Request.CreateResponse(&lt;span style="color:#2b91af;"&gt;HttpStatusCode&lt;span style="color:black;"&gt;.BadRequest);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;private&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;async&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;Task&lt;span style="color:black;"&gt; ProcessWebSocketRequest(&lt;span style="color:#2b91af;"&gt;AspNetWebSocketContext&lt;span style="color:black;"&gt; context)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; sessionCookie = context.Cookies[&lt;span style="color:#a31515;"&gt;&amp;quot;SessionId&amp;quot;&lt;span style="color:black;"&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (sessionCookie != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;await&lt;span style="color:black;"&gt; _webSocketHandler.ProcessWebSocketRequestAsync(context);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}&lt;/span&gt;
	&lt;/p&gt;&lt;p&gt;Note that I am injecting my SimpleWebSocketHandler over Unity Container that we configured in step above.
&lt;/p&gt;&lt;h3&gt;AngularJS &amp;quot;WebSocketPoller&amp;quot; Factory
&lt;/h3&gt;&lt;p&gt;For this purpose I wrote a simple AngularJS factory that creates a WebSocket connection upon the app start and raises the event on every message that has been received.
&lt;/p&gt;&lt;p&gt;&lt;span style="color:#a31515;font-family:Consolas;font-size:9pt;"&gt;&amp;#39;use strict&amp;#39;&lt;span style="color:black;"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.factory(&lt;span style="color:#a31515;"&gt;&amp;#39;webSocketPoller&amp;#39;&lt;span style="color:black;"&gt;, &lt;span style="color:blue;"&gt;function&lt;span style="color:black;"&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; listener;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; errorHandler;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; address;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;function&lt;span style="color:black;"&gt;(options) {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;        
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; Service = {};
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (options &amp;amp;&amp;amp; options.address) {
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;address = options.address;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (options &amp;amp;&amp;amp; options.listener) {
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;listener = options.listener;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (options &amp;amp;&amp;amp; options.errorHandler) {
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;errorHandler = options.errorHandler;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;Service.init = &lt;span style="color:blue;"&gt;function&lt;span style="color:black;"&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; ws = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt; WebSocket(address);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ws.onmessage = &lt;span style="color:blue;"&gt;function&lt;span style="color:black;"&gt;(message) {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//Do whatever you need with 
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//SimpleMessage Object:
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//JSON.parse(message.data);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;};
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ws.onerror = errorHandler;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt; Service
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});&lt;/span&gt;
	&lt;/p&gt;&lt;p&gt;Now you can find a right AngularJS Controller or use app.run() to add following code that initializes the Puller and attaches to OnMessage and OnError so you can handle your messages on UI:
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; wsPoller = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt; webSocketPoller({
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;address: &amp;#39;ws://localhost/api/messages/GetMessages&amp;#39;,
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;listener: onNewSystemMessageFunction,
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;errorHandler: &lt;span style="color:blue;"&gt;function&lt;span style="color:black;"&gt; (error) { console.error(error); }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;})
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;wsPoller.init();&lt;/span&gt;
	&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1803373" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term="AngularJS" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/AngularJS/default.aspx"></category></entry><entry><title>Setup Swagger to authenticate against Azure Active Directory</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/11/23/setup-swagger-to-authenticate-against-azure-active-directory.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/11/23/setup-swagger-to-authenticate-against-azure-active-directory.aspx</id><published>2016-11-23T14:40:16Z</published><updated>2016-11-23T14:40:16Z</updated><content type="html">&lt;p&gt;After spending almost two days implementing security in my Web API application with Azure Active Directory and setting up the Swagger on the top and implementing Azure Active Directory &lt;strong&gt;via OAuth2 Implicit Grant&lt;/strong&gt; for it as well, there is definitely some tips and tricks that you should know that will save you at least half of the time implementing the same.
&lt;/p&gt;&lt;p&gt;First of all, some important things that you should make sure of:
&lt;/p&gt;&lt;ul&gt;&lt;li&gt;You have correctly implemented AAD in your Web API application.
&lt;/li&gt;&lt;li&gt;You have installed and configured (without security of course) Swagger in your Web API application.
&lt;/li&gt;&lt;li&gt;You have set the &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
			&lt;/span&gt;Tags on &lt;strong&gt;every single Web API Method&lt;/strong&gt; instead of whole Controller Class. This is important because if you put the Tag for whole Controller Class, Swagger will not recognize those methods as ones that need Authorization and will not render the Login Button (or exactly said Exclamation Mark image) that actually allows you to Login to AAD and come back with valid token.
&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Add new application to Azure Active Directory for Swagger
&lt;/h3&gt;&lt;p&gt;Now we should first add a new application to our Azure Active Directory. As of writing this Post, Azure Active Directory UI on latest Azure Management Portal is still not really usable so please use the old Azure Management Portal under &lt;a href="https://manage.windowsazure.com/"&gt;https://manage.windowsazure.com/&lt;/a&gt;.
&lt;/p&gt;&lt;p&gt;So here are the steps you need to do:
&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Add new Swagger Application in your Azure Active Directory (I will not explain how to do that in this post and if you already fulfill the requirements above, you already know how to do that). Just write down the Name you gave it. I will reference to it later in text as {nameSwagger}
&lt;/li&gt;&lt;li&gt;Write down the Client ID {clientIdSwagger} of your new application. That is the only thing that you will need. You don&amp;#39;t need to create any App/Secret Key etc.
&lt;/li&gt;&lt;li&gt;Add a Reply URL with the &amp;quot;/o2c-html&amp;quot; postfix. Simply said, your Reply URL should be your Swagger URL + &amp;quot;/o2c-html&amp;quot; at the end. It is important as this is the entry point where Swagger returns from AAD after login and reads the token.
&lt;/li&gt;&lt;li&gt;Under &amp;quot;Permissions to other applications&amp;quot;, make sure that &amp;quot;Windows Azure Active Directory&amp;quot; hast Application Permissions: &amp;quot;Read directory data&amp;quot; and Delegated Permissions: &amp;quot;Read directory data&amp;quot; and &amp;quot;Sign in and read user profile&amp;quot;.&lt;br /&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge1.png" alt="" /&gt;
		&lt;/li&gt;&lt;li&gt;Under &amp;quot;Permissions to other applications&amp;quot;, click on &amp;quot;Add application&amp;quot;, change Filter to &amp;quot;All apps&amp;quot; and add your Web API application that Swagger needs to access to. Then make sure that Web API application is now listed and that under Delegated Permissions you check the &amp;quot;Access {webApiAppName}&amp;quot; permission. {webApiAppName} is the name of you Web API application or better said, how you named it in your Azure Active Directory. We will need this parameter as well.&lt;br /&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge2.png" alt="" /&gt;
		&lt;/li&gt;&lt;li&gt;&lt;div&gt;Click on the &amp;quot;Manage Manifest&amp;quot; from the bottom toolbar and then &amp;quot;Download Manifest&amp;quot;. Open the JSON file, find the property named &amp;quot;oauth2AllowImplicitFlow&amp;quot; and set it to: &amp;quot;true&amp;quot;. Upload the Manifest file back.
&lt;/div&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge3.png" alt="" /&gt;
			&lt;/p&gt;&lt;/li&gt;&lt;li&gt;Click on the &amp;quot;View Endpoints&amp;quot; from the bottom toolbar and write down the last endpoint on the list named &amp;quot;OAuth 2.0 Authorization Endpoint&amp;quot;. I will reference it later in text as {aadAuthority}.
&lt;/li&gt;&lt;/ol&gt;&lt;h3&gt;Check the configuration of your Web API application under Azure Active Directory
&lt;/h3&gt;&lt;p&gt;Now we need check the configuration for our Web API application definition under Azure Active Directory:
&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Write down the App ID URI {appIdUri} of the application.
&lt;/li&gt;&lt;li&gt;Under &amp;quot;Permissions to other applications&amp;quot;, make sure that &amp;quot;Windows Azure Active Directory&amp;quot; hast Application Permissions: &amp;quot;Read directory data&amp;quot; and Delegated Permissions: &amp;quot;Read directory data&amp;quot; and &amp;quot;Sign in and read user profile&amp;quot;. (Same as above)
&lt;/li&gt;&lt;/ol&gt;&lt;h3&gt;Update your SwaggerConfig.cs file to support AAD via OAuth2 Implicit Grant
&lt;/h3&gt;&lt;p&gt;You can copy paste these lines to your solution. Just make sure you have all parameters I referenced in text with {} correctly set.
&lt;/p&gt;&lt;p&gt;In .EnableSwagger Configuration add following lines:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;c.OAuth2(&lt;span style="color:#a31515;"&gt;&amp;quot;oauth2&amp;quot;&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;.Description(&lt;span style="color:#a31515;"&gt;&amp;quot;OAuth2 Implicit Grant&amp;quot;&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;.Flow(&lt;span style="color:#a31515;"&gt;&amp;quot;implicit&amp;quot;&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;.AuthorizationUrl({aadAuthority})
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;.Scopes(scopes =&amp;gt;
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;scopes.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;user_impersonation&amp;quot;&lt;span style="color:black;"&gt;, &lt;span style="color:#a31515;"&gt;$&amp;quot;Access &lt;span style="color:black;"&gt;{appIdUri}&lt;span style="color:#a31515;"&gt;&amp;quot;&lt;span style="color:black;"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;c.OperationFilter&amp;lt;&lt;span style="color:#2b91af;"&gt;SwaggerOAuth2SecurityRequirements&lt;span style="color:black;"&gt;&amp;gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Under .EnableSwaggerUI Configuration add following lines:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;c.EnableOAuth2Support(
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;{clientIdSwagger}&lt;span style="color:black;"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;string&lt;span style="color:black;"&gt;.Empty,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;&amp;quot;http://localhost/&amp;quot;&lt;span style="color:black;"&gt;, //or where you host your app
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;{nameSwagger}&lt;span style="color:black;"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#a31515;font-family:Consolas;font-size:9pt;"&gt;&amp;quot; &amp;quot;&lt;span style="color:black;"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;Dictionary&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;, &lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;&amp;gt; { { &lt;span style="color:#a31515;"&gt;&amp;quot;resource&amp;quot;&lt;span style="color:black;"&gt;, &lt;/span&gt;{&lt;/span&gt;webApiAppName&lt;/span&gt;}&lt;span style="color:black;"&gt; } });
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;And of course the missing SwaggerOAuth2SecurityRequirements.cs class should look like this:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;SwaggerOAuth2SecurityRequirements&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;IOperationFilter&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Apply(&lt;span style="color:#2b91af;"&gt;Operation&lt;span style="color:black;"&gt; operation, &lt;span style="color:#2b91af;"&gt;SchemaRegistry&lt;span style="color:black;"&gt; schemaRegistry, &lt;span style="color:#2b91af;"&gt;ApiDescription&lt;span style="color:black;"&gt; apiDescription)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// First check if there is any Authorize attribute&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; authorizeAttributes = apiDescription.ActionDescriptor.GetCustomAttributes&amp;lt;&lt;span style="color:#2b91af;"&gt;AuthorizeAttribute&lt;span style="color:black;"&gt;&amp;gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (!authorizeAttributes.Any())
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// Create the operation.security property&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (operation.security == &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;operation.security = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
					&lt;span style="color:#2b91af;"&gt;List&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IDictionary&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;&amp;gt;&amp;gt;&amp;gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// Add the oauth2 security definition to the operation&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; oAuthRequirements = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;Dictionary&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{ &lt;span style="color:#a31515;"&gt;&amp;quot;oauth2&amp;quot;&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;Enumerable&lt;span style="color:black;"&gt;.Empty&amp;lt;&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;&amp;gt;() }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;};
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;operation.security.Add(oAuthRequirements);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;h3&gt;Test your Swagger with new configuration and OAuth2 security
&lt;/h3&gt;&lt;p&gt;Finally, if you have done everything correctly, you should be able to see the Red Exclamation Mark indicator on the top right corner of every API Method in Swagger. Red color means that you are not authenticated and if you click on &amp;quot;Try it out!&amp;quot; button you should get answer like this:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge4.png" alt="" /&gt;
		&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge5.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;By clicking on the sign, and logging in over Azure Active Directory page, you should get back to Swagger and the Exclamation Mark indicator should turn Blue. After that you will be able to call the method:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge6.png" alt="" /&gt;
		&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/112316_5F00_1443_5F00_SetupSwagge7.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;h3&gt;Final Words
&lt;/h3&gt;&lt;p&gt;Swagger is a great tool and I just simply love it! That is why I was willing to take the time and implement the Azure Active Directory security in it simply because I was not willing to give up on such great tool or to compromise by using my development instance without security. &lt;br /&gt;I hope that I have saved you some time!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1802777" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Security" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Security/default.aspx"></category><category term="Azure" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Azure/default.aspx"></category><category term="swagger" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/swagger/default.aspx"></category><category term="Azure Active Directory" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Azure+Active+Directory/default.aspx"></category></entry><entry><title>Set Azure Webjob to run in 64-bit Mode </title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/10/18/set-azure-webjob-to-run-in-64-bit-mode.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/10/18/set-azure-webjob-to-run-in-64-bit-mode.aspx</id><published>2016-10-18T14:47:18Z</published><updated>2016-10-18T14:47:18Z</updated><content type="html">&lt;p&gt;I just found out that my Azure Webjob was running 32-bit all the time without me even knowing it. As I tried to attach my debugger on the Webjob I got following error message:&lt;br /&gt;&lt;em&gt;&amp;quot;Unable to attach. The 64-bit version of the Visual Studio Remote Debugger (MSVSMON.EXE) cannot be used to debug 32-bit processes or 32-bit dumps. Please use the 32-bit version instead.&amp;quot;&lt;/em&gt;
	&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/101816_5F00_1447_5F00_SetAzureWeb1.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;That was a surprise because I was expecting my WebJobs to be running 64-bit, especially as I double checked under my Application Settings in Azure Portal that my Web App &lt;strong&gt;IS running&lt;/strong&gt; in 64-bit Mode. But no. WebJob Executables were acutely built as 32-bit Applications so I looked at my Build Configuration Settings and I found following Checkbox:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/101816_5F00_1447_5F00_SetAzureWeb2.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;I am not sure why this setting is a default for every Azure WebJob. It only might have sense if Azure Team thinks that WebJobs are planned for a small workloads but if you have (as in my case) very large workloads you will see that x64 servers with sufficient CPU and RAM outperform x86 by a healthy measure.
&lt;/p&gt;&lt;p&gt;So anyway, if you have huge workloads and process gigabytes of data with your WebJobs, make sure you unselect this Checkbox in your Build Configuration!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1800849" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term="Azure" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Azure/default.aspx"></category></entry><entry><title>How to secure Static Content under ASP.NET MVC and OWIN</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/10/10/how-to-secure-static-content-under-asp-net-mvc-and-owin.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/10/10/how-to-secure-static-content-under-asp-net-mvc-and-owin.aspx</id><published>2016-10-10T10:38:33Z</published><updated>2016-10-10T10:38:33Z</updated><content type="html">&lt;p&gt;As you can see in my &lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2016/10/06/how-to-implement-web-sign-on-with-adfs-in-asp-net-mvc-using-owin.aspx"&gt;previous post&lt;/a&gt;, I am currently working on securing an ASP.NET MVC Web Application based on OWIN. Because of that is this post some sort of continuation of my previous one.  
&lt;/p&gt;&lt;p&gt;So, my Web Application in pure Single Page Application that works on AngularJS and is being generated by a Gulp Task Runner as a static content. Because we also have some REST Web API Methods in our Application that are used by Single Page Application, it was not possible to implement the Security of our Web Application completely on Client via ADAL or any other Javascript Library so we used (in our case Active Directory Federation Services) Owin Security Provider and added it to our ASP.NET MVC Web Application. Now we can easily use &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;background-color:white;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Tags on our Web API Methods.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;But now we had a Problem of securing our Single Page Application that is actually a static content in our ASP.NET MVC Web Application. For that I injected this simple Code Snippet that will be called upon every Request:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.Use((context, cont) =&amp;gt;
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; ((context.Authentication.User != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;) &amp;amp;&amp;amp;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;(context.Authentication.User.Identity != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;) &amp;amp;&amp;amp;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;(context.Authentication.User.Identity.IsAuthenticated))
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt; cont();
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;else&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;context.Authentication.Challenge(&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);&lt;br /&gt;&lt;span style="color:blue;"&gt;return&lt;span style="color:black;"&gt;
							&lt;span style="color:#2b91af;"&gt;Task&lt;span style="color:black;"&gt;.Delay(0);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});&lt;/span&gt;
	&lt;/p&gt;&lt;p&gt;It simply checks if the user is authenticated, and if not, it Challenges our OWIN Authentication Provider to authenticate the user (in our case ADFS). Now we don&amp;#39;t even have to use our &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;background-color:white;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Tags&lt;/span&gt; because all Request will need to be authenticated, what can make some problems in certain cases if you have some Web API Methods that needs to be publicly exposed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1800381" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term="Security" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Security/default.aspx"></category><category term="OWIN" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/OWIN/default.aspx"></category></entry><entry><title>How to implement Web Sign On with ADFS in ASP.NET MVC using OWIN</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/10/06/how-to-implement-web-sign-on-with-adfs-in-asp-net-mvc-using-owin.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/10/06/how-to-implement-web-sign-on-with-adfs-in-asp-net-mvc-using-owin.aspx</id><published>2016-10-06T14:08:39Z</published><updated>2016-10-06T14:08:39Z</updated><content type="html">&lt;p&gt;In this Post I will (try to) shortly explain how to Implement Web Sign on with Active Directory Federation Services under ASP.NET MVC and OWIN/Katana as Middleware.
&lt;/p&gt;&lt;p&gt;There is plenty of Resources (read Code Snippets) on the Net about this subject, but what I actually found as important as the Code Snippets is actual Configuration of AD FS Server. The Configuration of ADFS is highly sensitive on every identifier, endpoint definition and even on every character like underscores, slashes, and of course prefixes like http/https are all exactly to be defined and used in your Web App!
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;ADFS Server Configuration&lt;/strong&gt;&lt;/span&gt;
	&lt;/p&gt;&lt;p&gt;That is why I will first show some screenshots and Configuration of my ADFS Server running on Azure VM Development Environment:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/100616_5F00_1413_5F00_Howtoimplem1.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;Under &lt;strong&gt;Authentication Policies&lt;/strong&gt;, you should enable &lt;strong&gt;Forms Authentication&lt;/strong&gt; for &lt;strong&gt;Extranet&lt;/strong&gt; users. That way it will be possible for users outside your Domain or on a Public Computer to get a nice Forms Authentication provided by ADFS and still get authenticated.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/100616_5F00_1413_5F00_Howtoimplem2.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;You should create &lt;strong&gt;new Relying Party Trust&lt;/strong&gt; with following Configuration: &lt;strong&gt;Display Name&lt;/strong&gt; you can write whatever you want of course. Under &lt;strong&gt;Relaying party identifiers&lt;/strong&gt;, you should add exactly your Web Application URL (Including correct prefix and slash at the end. If you have more Bindings on you Web Application, define all of them. &lt;br /&gt;&lt;em&gt;NOTE: Some users had problems if their Web Application URLs contains underscores, so if you can, try to avoid them.
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Under &lt;strong&gt;Endpoint&lt;/strong&gt; Tab, add a &lt;strong&gt;WS-Federation Passive Endpoint&lt;/strong&gt; with the same URL of your Web Application as in &lt;strong&gt;Relying party identifiers&lt;/strong&gt;. &lt;br /&gt;&lt;em&gt;Note: You can also define multiple if you have more the one Binding, but only one can be Default. 
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Now knowing your exact ADFS Configuration, you will definitely save time and avoid configuration conditioned Problems so we can continue and finally write some Code.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;ASP.NET Web Application with OWIN Middleware
&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;First we will start off with OWIN &lt;strong&gt;Startup.cs&lt;/strong&gt; Class. We have the Authentication Configuration Method outsourced into separate Class called &lt;strong&gt;Startup.Auth.cs&lt;/strong&gt;, like the Default ASP.NET MVC Web Application Template do it, but many of you have it all directly under &lt;strong&gt;Startup.cs&lt;/strong&gt;.
&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Startup.cs
&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Threading.Tasks;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.WsFederation;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Owin;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:blue;"&gt;assembly&lt;span style="color:black;"&gt;: &lt;span style="color:#2b91af;"&gt;OwinStartupAttribute&lt;span style="color:black;"&gt;(&lt;span style="color:blue;"&gt;typeof&lt;span style="color:black;"&gt;(adfs.demo.&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;))]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;namespace&lt;span style="color:black;"&gt; adfs.demo
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;partial&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
								&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Configuration(&lt;span style="color:#2b91af;"&gt;IAppBuilder&lt;span style="color:black;"&gt; app)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ConfigureAuth(app);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Startup.Auth.cs
&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Configuration;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.Cookies;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Owin;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.WsFederation;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;namespace&lt;span style="color:black;"&gt; adfs.demo
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;partial&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
								&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; ConfigureAuth(&lt;span style="color:#2b91af;"&gt;IAppBuilder&lt;span style="color:black;"&gt; app)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.UseCookieAuthentication(
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;CookieAuthenticationOptions&lt;span style="color:black;"&gt;
					&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;AuthenticationType = &lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.UseWsFederationAuthentication(
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationOptions&lt;span style="color:black;"&gt;
					&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;MetadataAddress = &lt;span style="color:#2b91af;"&gt;ConfigurationManager&lt;span style="color:black;"&gt;.AppSettings[&lt;span style="color:#a31515;"&gt;&amp;quot;ida:ADFSMetadata&amp;quot;&lt;span style="color:black;"&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;Wtrealm = &lt;span style="color:#2b91af;"&gt;ConfigurationManager&lt;span style="color:black;"&gt;.AppSettings[&lt;span style="color:#a31515;"&gt;&amp;quot;ida:Wtrealm&amp;quot;&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.SetDefaultSignInAsAuthenticationType(&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Here is important that you extract your &lt;strong&gt;ADFS Metadata Address&lt;/strong&gt; correctly from your Server which is by Default &lt;a href="http://youradfsserver.address.com/FederationMetadata/2007-06/FederationMetadata.xml"&gt;http://youradfsserver.address.com/FederationMetadata/2007-06/FederationMetadata.xml&lt;/a&gt;. 
&lt;/li&gt;&lt;li&gt;In Wtrealm out your &lt;strong&gt;Relying Party Identifier&lt;/strong&gt; from ADFS Server from first part of the Post. 
&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Both of this entries I have placed in Web.Config and it looks like this:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;&amp;lt;&lt;span style="color:#a31515;"&gt;appSettings&lt;span style="color:blue;"&gt;&amp;gt;&lt;span style="color:black;"&gt;
					&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;&amp;lt;&lt;span style="color:#a31515;"&gt;add&lt;span style="color:blue;"&gt;
					&lt;span style="color:red;"&gt;key&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;ida:ADFSMetadata&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;
											&lt;span style="color:red;"&gt;value&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;http://your-adfs-server.com/FederationMetadata/2007-06/FederationMetadata.xml&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt; /&amp;gt;&lt;span style="color:black;"&gt;
																	&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;&amp;lt;&lt;span style="color:#a31515;"&gt;add&lt;span style="color:blue;"&gt;
					&lt;span style="color:red;"&gt;key&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;ida:Wtrealm&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;
											&lt;span style="color:red;"&gt;value&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;https://your-exact-site-url/&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt; /&amp;gt;&lt;span style="color:black;"&gt;
																	&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;&amp;lt;/&lt;span style="color:#a31515;"&gt;appSettings&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Now that should be enough to have OWIN Security running with our ADFS Server. All you have to do now is to add the &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Tags on needed Controllers or &lt;span style="font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;AllowAnonymous&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;on single Actions that needs to be accessed &lt;/span&gt;anonymously.
&lt;/p&gt;&lt;p&gt;Here is one very simple way of using a Templated &lt;strong&gt;AccountController.cs&lt;/strong&gt; with our ADFS Implementation:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Web;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Web.Mvc;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.Cookies;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.WsFederation;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;namespace&lt;span style="color:black;"&gt; adfs.demo.Controllers
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;AccountController&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;Controller&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;AllowAnonymous&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Login(&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; returnUrl)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (!Request.IsAuthenticated)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;HttpContext.GetOwinContext().Authentication.Challenge(&lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
					&lt;span style="color:#2b91af;"&gt;AuthenticationProperties&lt;span style="color:black;"&gt; { RedirectUri = &lt;span style="color:#a31515;"&gt;&amp;quot;/adfs.demo&amp;quot;&lt;span style="color:black;"&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;
			&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; LogOff()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;string&lt;span style="color:black;"&gt; callbackUrl = Url.Action(&lt;span style="color:#a31515;"&gt;&amp;quot;Index&amp;quot;&lt;span style="color:black;"&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Home&amp;quot;&lt;span style="color:black;"&gt;, routeValues: &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;, protocol: Request.Url.Scheme);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;HttpContext.GetOwinContext().Authentication.SignOut(
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;AuthenticationProperties&lt;span style="color:black;"&gt; { RedirectUri = callbackUrl },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:#2b91af;font-family:Consolas;font-size:9pt;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType, &lt;span style="color:#2b91af;"&gt;CookieAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion
&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;It was actually pretty easy to implement the ADFS Authentication Provider in OWIN once you understand and have access the whole configuration of ADFS Server. So far it looks really fast and stable, beside few problems I had with Chrome and Firefox Browser that I will explain below. So as I promised, this was actually short post and I hope you will get your ADFS running smoothly just as I did. &lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chrome / Firefox Redirection Loop Problem&lt;br /&gt;&lt;/strong&gt;After my first Implementation of ADFS under OWIN, it looked like everything is working well. As soon as I tested it on Chrome and Firefox, I figured out that after Login, I am being redirected to ADFS Forms Authentication Web Page and back to my Default Return URL few times before getting Error Message (that was not telling me much about Error) on my ADFS Forms Authentication Web Page. &lt;br /&gt;&lt;br /&gt;In ADFS Event Log, there was this Error event:&lt;br /&gt;&lt;em&gt;Microsoft.IdentityServer.Web.InvalidRequestException: MSIS7042: The same client browser session has made &amp;#39;6&amp;#39; requests in the last &amp;#39;1&amp;#39; seconds. Contact your administrator for details.
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;There are many explanations and workarounds for the Redirection Loop Problem under OWIN Security, most of them have something to do with &lt;strong&gt;CookieManager&lt;/strong&gt; under OWIN but none of them has solved my problem. 
&lt;/p&gt;&lt;p&gt;My Solution:&lt;br /&gt;As soon as I have defined &amp;quot;fresh&amp;quot; new &lt;strong&gt;Relying Party Trust&lt;/strong&gt; (see first part of the Post) with exactly the same &lt;strong&gt;Relying Party Identifier&lt;/strong&gt; as an &lt;strong&gt;WS-Federation Passive Endpoint&lt;/strong&gt;
		&lt;strong&gt;URL&lt;/strong&gt; (and I really mean exactly the same, same &lt;strong&gt;prefix&lt;/strong&gt;, same &lt;strong&gt;URL&lt;/strong&gt; and &lt;strong&gt;/&lt;/strong&gt; at the end of URL, also see first part of the post) and then updated my &lt;strong&gt;Web.Config&lt;/strong&gt; with the exact Values, it all started working as a Charm.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1800058" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term="OWIN" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/OWIN/default.aspx"></category><category term="Active Directory" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Active+Directory/default.aspx"></category><category term="ADFS" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ADFS/default.aspx"></category></entry><entry><title>Including XML Comments in Swagger under ASP.NET Core</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/09/30/including-xml-comments-in-swagger-under-asp-net-core.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/09/30/including-xml-comments-in-swagger-under-asp-net-core.aspx</id><published>2016-09-30T14:44:47Z</published><updated>2016-09-30T14:44:47Z</updated><content type="html">&lt;p&gt;For all of you using Swagger, a great tool or let&amp;#39;s better say Framework to Document and Represent your RESTful Web API Service under ASP.NET Core, you will probably be interested in following few lines of Code that enables Swagger to use your Project XML Documentation as information source. 
&lt;/p&gt;&lt;p&gt;First of all, you have to enable Generation of XML Documentation for your ASP.NET Core Project under &lt;strong&gt;Project Properties&lt;/strong&gt; &amp;gt; &lt;strong&gt;Build&lt;/strong&gt; and Check the &amp;quot;&lt;strong&gt;XML Documentation file&lt;/strong&gt;&amp;quot; checkbox. After that you need to change your &lt;strong&gt;Startup.cs&lt;/strong&gt; file and add following lines to your Configuration for Swagger:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; ConfigureServices(&lt;span style="color:#2b91af;"&gt;IServiceCollection&lt;span style="color:black;"&gt; services)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// Add framework services.&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;services.AddMvc();
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;// Inject an implementation of ISwaggerProvider with defaulted settings applied&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;services.AddSwaggerGen();
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;            
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;services.ConfigureSwaggerGen(options =&amp;gt;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//Determine base path for the application.&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; basePath = &lt;span style="color:#2b91af;"&gt;PlatformServices&lt;span style="color:black;"&gt;.Default.Application.ApplicationBasePath;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:green;font-family:Consolas;font-size:9pt;"&gt;//Set the comments path for the swagger json and ui.&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;options.IncludeXmlComments(basePath + &lt;span style="color:#a31515;"&gt;&amp;quot;&lt;span style="color:#ff007f;"&gt;\\&lt;span style="color:#a31515;"&gt;MyProjectName.xml&amp;quot;&lt;span style="color:black;"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;            
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Hope this will save you some time searching for on the net, and continue to focus on more important things in your Project.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1799600" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term=".Net" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/.Net/default.aspx"></category><category term="dotnet" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/dotnet/default.aspx"></category><category term="swagger" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/swagger/default.aspx"></category><category term="dotnetcore" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/dotnetcore/default.aspx"></category><category term="aspnetcore" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/aspnetcore/default.aspx"></category></entry><entry><title>How to Audit Login activity in IdentityServer3</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2016/09/23/how-to-audit-login-activity-in-identityserver3.aspx" /><id>/blogs/armin_kalajdzija/archive/2016/09/23/how-to-audit-login-activity-in-identityserver3.aspx</id><published>2016-09-23T08:41:22Z</published><updated>2016-09-23T08:41:22Z</updated><content type="html">&lt;p&gt;In some use cases there is a security requirement for Auditing of User Login Activity, which is not covered by IdentityServer3.
&lt;/p&gt;&lt;p&gt;As we used the ASP.NET Identity as User Store, it also possible to implement some sort of Auditing as a custom SignInManager.cs in ASP.NET Identity, but that would not cover all Login Features that are supported by IdentityServer3, which us great Framework by the way and I had much joy of using it and I look forward of using the latest port of IdentityServer on ASP.NET Core.
&lt;/p&gt;&lt;p&gt;Searching for a best way to implement this, I found out two interesting Features in Framework:
&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Logging (more for Development-time logging, that is produces a quite extensive output and not that great for Auditing)
&lt;/li&gt;&lt;li&gt;Events (much better for production-time environments, firing four types of Events and is configurable. More on Events in IdentityServer3 here)
&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;First of all, we will extend our IdentityDbContext that we used for ASP.NET Identity User Storage with another Class called UserAudit.cs with all the columns I want to store:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Table&lt;span style="color:black;"&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;UserAuditEvents&amp;quot;&lt;span style="color:black;"&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;UserAudit&lt;span style="color:black;"&gt;
							&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Key&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;int&lt;span style="color:black;"&gt; Id { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;private&lt;span style="color:black;"&gt;
										&lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Required&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; UserId { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;private&lt;span style="color:black;"&gt;
										&lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Required&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;DateTimeOffset&lt;span style="color:black;"&gt; Timestamp { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;private&lt;span style="color:black;"&gt;
										&lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; } = &lt;span style="color:#2b91af;"&gt;DateTime&lt;span style="color:black;"&gt;.UtcNow;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Required&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;EventTypes&lt;span style="color:black;"&gt; AuditEvent { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; IpAddress { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;private&lt;span style="color:black;"&gt;
										&lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;static&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;UserAudit&lt;span style="color:black;"&gt; CreateAuditEvent(&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; userId, &lt;span style="color:#2b91af;"&gt;EventTypes&lt;span style="color:black;"&gt; auditEventType, &lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; ipAddress)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;UserAudit&lt;span style="color:black;"&gt; { UserId = userId, AuditEvent = auditEventType, IpAddress = ipAddress };
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;For EventTypes Enum you will need to add using of Thinktecture.IdentityServer.Core.Events namespace. It contains Integer values for following Events: Success = 1, Failure = 2, Information = 3 and Error = 4.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Now that we have a class which represents the database table, we should add it to the entity framework IdentityDbContext:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;LoginDbContext&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;IdentityDbContext&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;User&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;Role&lt;span style="color:black;"&gt;, &lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;IdentityUserLogin&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;IdentityUserRole&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;IdentityUserClaim&lt;span style="color:black;"&gt;&amp;gt;, &lt;span style="color:#2b91af;"&gt;ILoginDbContext&lt;span style="color:black;"&gt;
																							&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;virtual&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;DbSet&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;UserAudit&lt;span style="color:black;"&gt;&amp;gt; UserAuditEvents { &lt;span style="color:blue;"&gt;get&lt;span style="color:black;"&gt;; &lt;span style="color:blue;"&gt;set&lt;span style="color:black;"&gt;; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;At this point, we have a new database table defined which needs to be physically created. We will do this by creating a migration and applying it to the database or if you have AutomaticMigrationsEnabled = true then you just have to update your database by running the &amp;quot;Update-Database&amp;quot; in Package Manager Console of your Project.
&lt;/p&gt;&lt;p&gt;Now we have to implement our AuditEventService.cs as an Implementation of IEventService.cs. 
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;AuditEventService&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;IEventService&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;private&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;readonly&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;LoginDbContext&lt;span style="color:black;"&gt; _db;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt; AuditEventService(&lt;span style="color:#2b91af;"&gt;LoginDbContext&lt;span style="color:black;"&gt; dbContext)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;_db = dbContext;
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Raise&amp;lt;&lt;span style="color:#2b91af;"&gt;T&lt;span style="color:black;"&gt;&amp;gt;(&lt;span style="color:#2b91af;"&gt;Event&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;T&lt;span style="color:black;"&gt;&amp;gt; evt)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; localLoginDetails = evt.Details &lt;span style="color:blue;"&gt;as&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;LocalLoginDetails&lt;span style="color:black;"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (localLoginDetails != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:#2b91af;font-family:Consolas;font-size:9pt;"&gt;EnBWUserAudit&lt;span style="color:black;"&gt; newAuditRecord = &lt;span style="color:#2b91af;"&gt;EnBWUserAudit&lt;span style="color:black;"&gt;.CreateAuditEvent(
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;localLoginDetails.LoginUserName,
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;evt.EventType,
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:#2b91af;font-family:Consolas;font-size:9pt;"&gt;HttpContext&lt;span style="color:black;"&gt;.Current.Request.UserHostAddress
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;_db.UserAuditEvents.Add(newAuditRecord);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;_db.SaveChangesAsync();
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;As you can see, Raise&amp;lt;T&amp;gt; function will be called when any Event is fired and because we are implementing Login Auditing, I am only interested in Events.Details of type LocalLoginDetails as they contain all relevant information about the Login that we want to audit. Also, we are Injecting the DbContext into the Construct of the Service because we need it for storing our audit records. This Injection needs to be registered separated from our IEventService in order to work. Code for this in a minute.  
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;So the final part is here! Now we need to change our Startup.cs Class and add few lines to our IdentityServerServiceFactory. 
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; factory = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
							&lt;span style="color:#2b91af;"&gt;IdentityServerServiceFactory&lt;span style="color:black;"&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;First we need to register our AuditEventService and then the DbContext to the IdentityServerServiceFactory in order for Dependency Injection to work:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;factory.EventService = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
					&lt;span style="color:#2b91af;"&gt;Registration&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IEventService&lt;span style="color:black;"&gt;, &lt;span style="color:#2b91af;"&gt;AuditEventService&lt;span style="color:black;"&gt;&amp;gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;factory.Register(&lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
					&lt;span style="color:#2b91af;"&gt;Registration&lt;span style="color:black;"&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;LoginDbContext&lt;span style="color:black;"&gt;&amp;gt;(resolver =&amp;gt; &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
											&lt;span style="color:#2b91af;"&gt;LoginDbContext&lt;span style="color:black;"&gt;()));
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Now, it should all be working if there was not one &amp;quot;small&amp;quot; thing: IEventService configurations stored in EventOptions Class is by default turned off for all types of events. We need to add EventOptions to our IdentityServerOptions in order to get Events fired at all.
&lt;/p&gt;&lt;p&gt;My IdentityServerOptions looked at the end like this:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;var&lt;span style="color:black;"&gt; eventOptions = &lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;EventsOptions&lt;span style="color:black;"&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;RaiseSuccessEvents = &lt;span style="color:blue;"&gt;true&lt;span style="color:black;"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;RaiseFailureEvents = &lt;span style="color:blue;"&gt;true&lt;span style="color:black;"&gt;
				&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;};
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Finally, we can try to Login to our Application. If all went good, you should be able to see something like this in you Database Table &amp;quot;UserAuditEvents&amp;quot;:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/092316_5F00_0912_5F00_HowtoAuditL1.png" alt="" /&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Summing Up
&lt;/p&gt;&lt;p&gt;This blog post hopefully demonstrates the initial steps that we can follow to quite easily implement the IEventService in IdentityServer3 and extend it with nice auditing feature. I expect to be refactoring and extending this code further as my requirements determine.
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1799046" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Security" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Security/default.aspx"></category><category term="IdentityServer3" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/IdentityServer3/default.aspx"></category></entry><entry><title>How to integrate JavaScript Unit Tests in Visual Studio Online</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2015/09/22/how-to-integrate-javascript-unit-tests-in-visual-studio-online.aspx" /><id>/blogs/armin_kalajdzija/archive/2015/09/22/how-to-integrate-javascript-unit-tests-in-visual-studio-online.aspx</id><published>2015-09-22T12:34:40Z</published><updated>2015-09-22T12:34:40Z</updated><content type="html">&lt;p&gt;We all know the importance of Unit Testing and it should definitely have an important part of every Project, but we also know that todays Web Application have major parts of (at least UI) Logic implemented in diverse JavaScript libs that also needs to be Unit Tested and in also integrated in your Build Server Definitions.&lt;/p&gt;  &lt;p&gt;As I was searching for ways how to achieve that goal, I run upon the great Post from Mathew Aniyan (&lt;a href="http://blogs.msdn.com/b/visualstudioalm/archive/2012/07/09/javascript-unit-tests-on-team-foundation-service-with-chutzpah.aspx"&gt;link here&lt;/a&gt;) that describes how to use &lt;a href="https://github.com/mmanela/chutzpah"&gt;Chutzpah&lt;/a&gt;, JavaScript Unit Test Runner for Jasmine Unit Test Framework that comes with implemented Visual Studio Test Adapter. I was not 100% satisfied with the Solution, but I was more then happy to know that the Test Adapter of Chutzpah works on Visual Studio Online and that I don’t need to write any Command Lines in my Build Definition to run the Jasmine Specs (JavaScript Unit Tests) by myself.&lt;/p&gt;  &lt;p&gt;So lets take a look how I solved the JavaScript Unit Testing on my hosted Visual Studio Online Build Definition:&lt;/p&gt;  &lt;p&gt;1. Step was to create a new Test Project that will be used for JavaScript Unit Testing. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/solutionexplorer_5F00_1F5D5B81.png"&gt;&lt;img title="solutionexplorer" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="solutionexplorer" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/solutionexplorer_5F00_thumb_5F00_5808058E.png" width="293" height="426" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you can see, I named it Demo.Javascript.Test and structured it so that it has Folder &lt;strong&gt;Test&lt;/strong&gt; containing Specs Files and &lt;strong&gt;Sources&lt;/strong&gt; that will actually contain my JavaScript Sources that needs to be tested.     &lt;br /&gt;&lt;em&gt;(I strongly recommend not to copy your .js Sources from your Web Project to Test Project but to Link them to the Sources Folder)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;2. Step was to add Chutzpah and Jasmine-Test Nuget Packages to my Project.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/nuget_5F00_0CA861CA.png"&gt;&lt;img title="nuget" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="nuget" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/nuget_5F00_thumb_5F00_0831E103.png" width="640" height="297" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;(I also recommend not to Check-In Packages to your TFS and to let your Build Server get the “fresh” version on every Build. On how to do that, read &lt;a href="https://docs.nuget.org/consume/package-restore/msbuild-integrated"&gt;this link&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;3. Step was modifying my Build Definition on Visual Studio Online and to reference Chutzpah Test Adapter so that VSTest can understand our JavaScript Specs files and test them correctly.    &lt;br /&gt;To do that, add new &lt;strong&gt;Visual Studio Test&lt;/strong&gt; runner to your Build Definition and configure it like here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/builddefinition_5F00_580A963F.png"&gt;&lt;img title="builddefinition" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="builddefinition" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/builddefinition_5F00_thumb_5F00_5327E283.png" width="569" height="402" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The only two things you need to configure are:    &lt;br /&gt;- &lt;strong&gt;Test Assembly Wildcard&lt;/strong&gt;, which was in my case all Files ending with Specs with .js extension, and in every Subfolder.     &lt;br /&gt;- &lt;strong&gt;Path to Custom Test Adapters&lt;/strong&gt;, which should be pointing to local Sources Directory $(Build.SourcesDirectory) and then the Packages folder.&lt;/p&gt;  &lt;p&gt;Really cool thing is that we &lt;strong&gt;don’t need to Check-In any binaries&lt;/strong&gt; and have to update them from time to time. This way, on every Build, the MSBuild will get the latest NuGet Packages (Chutzpah in our case) and we will be using the latest binaries in our Visual Studio Test Runner.&lt;/p&gt;  &lt;p&gt;Now, when you run the Hosted Build on your Visual Studio Online you should get Output like following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/build_5F00_0BD28C91.png"&gt;&lt;img title="build" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="build" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/build_5F00_thumb_5F00_12859614.png" width="315" height="201" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In Logs of the Build Process you can then see all information about which Specs Files are tested and which Methods were being tested. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I hope I could save you some time or encourage you to add Unit Tests of your JavaScript Sources. Thanks to the Chutzpah Test Runner it is really simple do add Jasmine Unit Testing Framework to your Visual Studio Online which adds an Important feature to it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1549176" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Visual Studio" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Visual+Studio/default.aspx"></category><category term="Javascript" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Javascript/default.aspx"></category><category term="TDD" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/TDD/default.aspx"></category><category term="PhantomJS" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/PhantomJS/default.aspx"></category><category term="Jasmine" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Jasmine/default.aspx"></category><category term="Unit Testing" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Unit+Testing/default.aspx"></category><category term="Chutzpah" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Chutzpah/default.aspx"></category></entry><entry><title>How to use Client Certificate in Azure Web App</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2015/08/14/how-to-use-client-certificate-in-azure-web-app.aspx" /><id>/blogs/armin_kalajdzija/archive/2015/08/14/how-to-use-client-certificate-in-azure-web-app.aspx</id><published>2015-08-14T13:28:15Z</published><updated>2015-08-14T13:28:15Z</updated><content type="html">&lt;p&gt;If you need to Authenticate your Azure Web App (ASP.NET MVC Application) against WCF Service with a specified Client Certificate (.p12 in my case) you will find out that it is not quite simple as it sounds. There are plenty of samples of how to do it in Internet, but non of them has really worked for me! Here is a short list of what I have tested and what have worked and not worked:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. Using Certification Store&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First you need to upload your Certificate into your Azure Web App, and get the Thumbprint of it.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/1_5F00_33630312.png"&gt;&lt;img title="1" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="1" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/1_5F00_thumb_5F00_39A9D9A0.png" width="298" height="281" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;After that it is supposed to work with following line of code:&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;   &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;     &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;var service = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WCFServiceClient();&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;service.ClientCredentials.ClientCertificate.SetCertificate(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    StoreLocation.LocalMachine, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    StoreName.My, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    X509FindType.FindByThumbprint, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    &lt;span style="color:#006080;"&gt;&amp;quot;TUMBPRINT&amp;quot;&lt;/span&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This solution &lt;strong&gt;&lt;font color="#ff0000"&gt;DID NOT WORK&lt;/font&gt;&lt;/strong&gt;! It can not find any Certificate with than (or any) Thumbprint when running as Azure Web App. Also tried changing the StoreLocation to CurrentUser but it did not help.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Loading file from File System&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Second idea was to load it from my File System, so I put my Certificate in App_Data and rewritten my code to:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:120px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;var service = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WCFServiceClient();&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;service.ClientCredentials.ClientCertificate.Certificate = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; X509Certificate2(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    &lt;span style="color:#006080;"&gt;&amp;quot;FILENAME.p12&amp;quot;&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    &lt;span style="color:#006080;"&gt;&amp;quot;PASSWORD&amp;quot;&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This solution &lt;strong&gt;&lt;font color="#ff0000"&gt;ALSO &lt;/font&gt;&lt;font color="#ff0000"&gt;DID NOT WORK&lt;/font&gt;&lt;/strong&gt;! It could not find specified file on my File System.&amp;#160; By putting the Certificate on root of my Project and not in App_Data also did not help.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Loading the Certificate from Resource as byte[]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So I was looking at all overrides of X509Certificate2() ctor and I found that it is possible to give a Certificate as a byte[]. So I created a simple Resource file and added my Certificate as Existing File:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/2_5F00_321E6A33.png"&gt;&lt;img title="2" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="2" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/2_5F00_thumb_5F00_3F847D39.png" width="333" height="167" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I rewritten my code so I can get a byte[] out of my Resource file and initialize my Service Client with that Certificate:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:110px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;var service = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WCFServiceClient();&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;service.ClientCredentials.ClientCertificate.Certificate = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; X509Certificate2(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    (&lt;span style="color:#0000ff;"&gt;byte&lt;/span&gt;[])Properties.Resources.ResourceManager.GetObject(&lt;span style="color:#006080;"&gt;&amp;quot;RESOURCE&amp;quot;&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    &lt;span style="color:#006080;"&gt;&amp;quot;PASSWORD&amp;quot;&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;And this code &lt;strong&gt;&lt;font color="#008000"&gt;actually WORKED&lt;/font&gt;&lt;/strong&gt;! I was able to make a SSL/TLS Connection to my WCF Endpoint successfully!&lt;/p&gt;

&lt;p&gt;One more thing you should not forget is of course to define your Bindings in Web.Config correctly so that your client knows that he needs to Authenticate himself to Service with Client Certificate. Put or make sure you have following definition under your &amp;lt;system.serviceModel&amp;gt; tags:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:170px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;lt;bindings&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;  &amp;lt;basicHttpBinding&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    &amp;lt;binding name=&lt;span style="color:#006080;"&gt;&amp;quot;BasicHttpBinding_WCFService&amp;quot;&lt;/span&gt; maxBufferPoolSize=&lt;span style="color:#006080;"&gt;&amp;quot;20971520&amp;quot;&lt;/span&gt; maxBufferSize=&lt;span style="color:#006080;"&gt;&amp;quot;20971520&amp;quot;&lt;/span&gt; maxReceivedMessageSize=&lt;span style="color:#006080;"&gt;&amp;quot;20971520&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;      &amp;lt;security mode=&lt;span style="color:#006080;"&gt;&amp;quot;Transport&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &amp;lt;transport clientCredentialType=&lt;span style="color:#006080;"&gt;&amp;quot;Certificate&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;      &amp;lt;/security&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    &amp;lt;/binding&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;  &amp;lt;/basicHttpBinding&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;lt;/bindings&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Hope I saved you few hours of frustration!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1472422" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term="Security" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Security/default.aspx"></category><category term=".Net" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/.Net/default.aspx"></category><category term="Azure" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Azure/default.aspx"></category><category term="Windows Azure" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Windows+Azure/default.aspx"></category></entry><entry><title>How to use D3.js for Server Side Chart Generation in .NET (Part 3)</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2015/07/30/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-3.aspx" /><id>/blogs/armin_kalajdzija/archive/2015/07/30/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-3.aspx</id><published>2015-07-30T12:31:20Z</published><updated>2015-07-30T12:31:20Z</updated><content type="html">&lt;p&gt;&lt;strong&gt;How to use D3.js for Server Side Chart Generation in .NET&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2015/07/15/how-to-use-d3-js-for-graph-generation-in-asp-net-web-application-part-1.aspx" target="_blank"&gt;Part 1: Creating a Donut Chart using D3.js&lt;/a&gt;     &lt;br /&gt;&lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2015/07/22/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-2.aspx" target="_blank"&gt;Part 2: Render/Export D3.js Chart as PNG Image using PhantomJS&lt;/a&gt;     &lt;br /&gt;Part 3: Creating a simple REST Service using ASP.NET Web API&lt;/p&gt;  &lt;p&gt;In order to make our Chart Generation logic reusable I have decided to put it in a simple RESTful Service that will be implemented using ASP.NET Web API. We need to take all files that we created in previous parts of this series and place it in one temporary folder that will be used for Chart Generation.&lt;/p&gt;  &lt;p&gt;I have decided to use &lt;strong&gt;C:\Temp\Chart&lt;/strong&gt; folder,&amp;#160; and have copied following content:     &lt;br /&gt;&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="font-size:8pt;overflow:auto;cursor:text;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;border-left:silver 1px solid;margin:20px 0px 10px;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;   &lt;div id="codeSnippet" style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;     &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;donut_template.html&lt;/pre&gt;


    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;phantomjs.exe&lt;/pre&gt;


    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;render.js&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, &lt;strong&gt;phantomjs.exe&lt;/strong&gt; and &lt;strong&gt;render.js&lt;/strong&gt; are files that we used in Part 2 of our series. &lt;strong&gt;donut_template.html&lt;/strong&gt; is actually&lt;strong&gt; donut.html&lt;/strong&gt; file from the Part 1 with one following change: 

  &lt;br /&gt;I have removed the explicit definition of our &lt;strong&gt;dataset&lt;/strong&gt; used for rendering of Donut Chart: 

  &lt;br /&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="font-size:8pt;overflow:auto;cursor:text;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;border-left:silver 1px solid;margin:20px 0px 10px;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; data = [{ label: &lt;span style="color:#006080;"&gt;&amp;#39;Here&amp;#39;&lt;/span&gt;, value: 190000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#1e398f&amp;quot;&lt;/span&gt; },&lt;/pre&gt;


    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            { label: &lt;span style="color:#006080;"&gt;&amp;#39;Some&amp;#39;&lt;/span&gt;, value: 50000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#f7a600&amp;quot;&lt;/span&gt; },&lt;/pre&gt;


    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            { label: &lt;span style="color:#006080;"&gt;&amp;#39;Labels&amp;#39;&lt;/span&gt;, value: 420000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#e17e62&amp;quot;&lt;/span&gt; },&lt;/pre&gt;


    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            { label: &lt;span style="color:#006080;"&gt;&amp;#39;Needed&amp;#39;&lt;/span&gt;, value: 1530000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#985181&amp;quot;&lt;/span&gt; }];&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;br /&gt;With following line of code: 

&lt;div id="codeSnippetWrapper" style="font-size:8pt;overflow:auto;cursor:text;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;border-left:silver 1px solid;margin:20px 0px 10px;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; data = $$$PAYLOADINJECTION$$$;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This line will be used later in REST Service for Injecting the &lt;strong&gt;JSON&lt;/strong&gt; &lt;strong&gt;dataset&lt;/strong&gt; from our &lt;strong&gt;Server Side Model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now we can focus on creating a REST Service using ASP.NET Web API. 
  &lt;br /&gt;To do that, do the following steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Start new &lt;strong&gt;ASP.NET Web Application&lt;/strong&gt; Project in Visual Studio and chose &lt;strong&gt;Empty Template&lt;/strong&gt; with &lt;strong&gt;Web API&lt;/strong&gt; added core references &lt;/li&gt;

  &lt;li&gt;Add new Model, name it &lt;strong&gt;ChartModel&lt;/strong&gt; and add following definitions: 

    &lt;br /&gt;

    &lt;div id="codeSnippetWrapper" style="font-size:8pt;overflow:auto;cursor:text;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;border-left:silver 1px solid;margin:20px 0px 10px;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
      &lt;div id="codeSnippet" style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; ChartService.Models&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ChartModel&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; label { get; set; }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; value { get; set; }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; color { get; set; }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
    &lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Now add new Controller, name it &lt;strong&gt;ChartController&lt;/strong&gt; and place following code inside: 

    &lt;br /&gt;

    &lt;div id="codeSnippetWrapper" style="font-size:8pt;overflow:auto;cursor:text;border-top:silver 1px solid;height:1400px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;border-left:silver 1px solid;margin:20px 0px 10px;line-height:12pt;padding-right:4px;max-height:1400px;background-color:#f4f4f4;"&gt;
      &lt;div id="codeSnippet" style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Net;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Net.Http;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Net.Http.Headers;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Web.Hosting;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Web.Http;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Newtonsoft.Json;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; ChartService.Models;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; ChartService.Controllers&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;{&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ChartController : ApiController&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;    {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; HttpResponseMessage Post([FromBody] IEnumerable&amp;lt;ChartModel&amp;gt; payload)&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;        {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            String imageFile = GenerateChartImage(payload);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            MemoryStream imageStream = ReadChartImage(imageFile);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            var result = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; HttpResponseMessage(HttpStatusCode.OK);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            result.Content = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ByteArrayContent(imageStream.ToArray());&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            result.Content.Headers.ContentType = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MediaTypeHeaderValue(&lt;span style="color:#006080;"&gt;&amp;quot;image/png&amp;quot;&lt;/span&gt;);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; result;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; GenerateChartImage(IEnumerable&amp;lt;ChartModel&amp;gt; payload)&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;        {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            Guid chartID = Guid.NewGuid();&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; templatePath = &lt;span style="color:#006080;"&gt;@&amp;quot;C:\Temp\Chart\donut_template.html&amp;quot;&lt;/span&gt;;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; htmlPath = String.Format(&lt;span style="color:#006080;"&gt;@&amp;quot;C:\Temp\Chart\{0}.html&amp;quot;&lt;/span&gt;, chartID);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; pngPath = String.Format(&lt;span style="color:#006080;"&gt;@&amp;quot;C:\Temp\Chart\{0}.png&amp;quot;&lt;/span&gt;, chartID);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// Read HTML Template File&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; html = File.ReadAllText(templatePath);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// Create copy with Payload&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            html = html.Replace(&lt;span style="color:#006080;"&gt;&amp;quot;$$$PAYLOADINJECTION$$$&amp;quot;&lt;/span&gt;, JsonConvert.SerializeObject(payload));&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            File.WriteAllText(htmlPath, html);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// Start PhantomJS as Hidden Process in Background&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            ProcessStartInfo startInfo = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProcessStartInfo();&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            startInfo.CreateNoWindow = &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            startInfo.UseShellExecute = &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            startInfo.FileName = Path.Combine(&lt;span style="color:#006080;"&gt;@&amp;quot;C:\Temp\Chart\&amp;quot;, &amp;quot;&lt;/span&gt;phantomjs.exe&lt;span style="color:#006080;"&gt;&amp;quot;);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            startInfo.WorkingDirectory = @&amp;quot;&lt;/span&gt;C:\Temp\Chart\&lt;span style="color:#006080;"&gt;&amp;quot;;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            startInfo.WindowStyle = ProcessWindowStyle.Hidden;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            startInfo.Arguments = &amp;quot;&lt;/span&gt;render.js \&lt;span style="color:#006080;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; + htmlPath + &lt;span style="color:#006080;"&gt;&amp;quot;\&amp;quot; \&amp;quot;&amp;quot;&lt;/span&gt; + pngPath + &lt;span style="color:#006080;"&gt;&amp;quot;\&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (Process phantomJsProc = Process.Start(startInfo))&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;                {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;                    phantomJsProc.WaitForExit();&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; pngPath;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;                }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (Exception)&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt;;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;finally&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// At the end, don&amp;#39;t forget to delete temp. generated files&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;                File.Delete(htmlPath);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;        }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; MemoryStream ReadChartImage(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; imageFile)&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            FileStream fileStream = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; FileStream(imageFile, FileMode.Open);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            Image image = Image.FromStream(fileStream);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            MemoryStream memoryStream = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MemoryStream();&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            image.Save(memoryStream, ImageFormat.Png);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// Dispose objects and delete the temp. generated files&lt;/span&gt;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            image.Dispose();&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            fileStream.Close();&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            File.Delete(imageFile);&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; memoryStream;&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:white;"&gt;    }&lt;/pre&gt;


        &lt;pre style="border-top-style:none;font-size:8pt;overflow:visible;border-left-style:none;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now with this Implementation of &lt;strong&gt;ChartController.cs&lt;/strong&gt; we can simply &lt;strong&gt;POST&lt;/strong&gt; a &lt;strong&gt;JSON Payload&lt;/strong&gt; containing our &lt;strong&gt;dataset&lt;/strong&gt; that will be injected into our &lt;strong&gt;donut_template.html&lt;/strong&gt; file. &lt;/p&gt;

&lt;p&gt;After that we will run &lt;strong&gt;render.js&lt;/strong&gt; on &lt;strong&gt;PhantomJS&lt;/strong&gt; as hidden background process on our new generated html file and generate the PNG Image file that will be delivered in response to our http request as “&lt;strong&gt;image/png&lt;/strong&gt;” content type.&lt;/p&gt;

&lt;p&gt;You can easily test the functionality of code in &lt;strong&gt;Fiddler&lt;/strong&gt; by composing the &lt;strong&gt;POST request&lt;/strong&gt; to your new REST Service:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/fiddler_5F00_request_5F00_2D799E37.png"&gt;&lt;img title="fiddler_request" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="fiddler_request" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/fiddler_5F00_request_5F00_thumb_5F00_654BE25A.png" width="659" height="731" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By &lt;strong&gt;Executing&lt;/strong&gt; your request you should get following &lt;strong&gt;response&lt;/strong&gt; of your request:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/fiddler_5F00_result_5F00_5FFCFBA9.png"&gt;&lt;img title="fiddler_result" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="fiddler_result" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/fiddler_5F00_result_5F00_thumb_5F00_228C9722.png" width="661" height="635" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have a nice &lt;strong&gt;REST Web API Service&lt;/strong&gt; that uses &lt;strong&gt;D3.js Chart Library&lt;/strong&gt; for &lt;strong&gt;Server Side&lt;/strong&gt; generating, pre-rendering and delivering of Charts Images. &lt;/p&gt;

&lt;p&gt;Now you can create a &lt;strong&gt;ASP.NET Web Application&lt;/strong&gt; that will use the new &lt;strong&gt;REST Service&lt;/strong&gt; to dynamically generate and display the Chart.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1470958" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author></entry><entry><title>How to use D3.js for Server Side Chart Generation in .NET (Part 2)</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2015/07/22/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-2.aspx" /><id>/blogs/armin_kalajdzija/archive/2015/07/22/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-2.aspx</id><published>2015-07-22T11:56:00Z</published><updated>2015-07-22T11:56:00Z</updated><content type="html">&lt;p&gt;&lt;strong&gt;How to use D3.js for Server Side Chart Generation in .NET&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2015/07/15/how-to-use-d3-js-for-graph-generation-in-asp-net-web-application-part-1.aspx" target="_blank"&gt;Part 1: Creating a Donut Chart using D3.js&lt;/a&gt;     &lt;br /&gt;Part 2: Render/Export D3.js Chart as PNG Image using PhantomJS    &lt;br /&gt;&lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2015/07/30/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-3.aspx" target="_blank"&gt;Part 3: Creating a simple REST Service using ASP.NET Web API&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So after we have created a HTML Template of our Donut Chart in the previous part, I will be dealing with exporting of this Chart as a PNG Image in this part of the Series. &lt;/p&gt;  &lt;p&gt;For that purpose we will be using &lt;a href="http://phantomjs.org/" target="_blank"&gt;PhantomJS&lt;/a&gt;. It is a headless webkit engine that can be scriptable with Javascript and has native support for DOM, CSS and SVG. I have chosen the PhantomJS over other engines because it is really easy to use, supports various exports (PNG, PDF etc.) out of the box and its implementation of DOM is definitely better then using JSDom under Node.js/IO.js … at least this is my personal opinion.     &lt;br /&gt;One more thing that I like about PhantomJS is that is one single .exe file. This means you don’t have to install anything so it is portable and you can simply put him in the same folder as your HTML Template.&lt;/p&gt;  &lt;p&gt;Now we need to script the PhantomJS to do what we want. We need to create new Javascript fille called &lt;strong&gt;render.js&lt;/strong&gt; that will contain our rendering logic shown in the Code Snippet:&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:440px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:440px;background-color:#f4f4f4;"&gt;   &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;     &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; page = require(&lt;span style="color:#006080;"&gt;&amp;#39;webpage&amp;#39;&lt;/span&gt;).create(),&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    fs = require(&lt;span style="color:#006080;"&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    system = require(&lt;span style="color:#006080;"&gt;&amp;#39;system&amp;#39;&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    address, output;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (system.args.length &amp;lt; 3 || system.args.length &amp;gt; 3) &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;{&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    console.log(&lt;span style="color:#006080;"&gt;&amp;#39;Usage: render.js htmlFileName pngFileName&amp;#39;&lt;/span&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    phantom.exit(1);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;}&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    address = &lt;span style="color:#006080;"&gt;&amp;#39;file:///&amp;#39;&lt;/span&gt; + fs.absolute(system.args[1]);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    output = system.args[2];&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    page.open(address, &lt;span style="color:#0000ff;"&gt;function&lt;/span&gt; (status) {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (status !== &lt;span style="color:#006080;"&gt;&amp;#39;success&amp;#39;&lt;/span&gt;) {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            console.log(&lt;span style="color:#006080;"&gt;&amp;#39;Unable to load the address: &amp;#39;&lt;/span&gt; + address);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;            phantom.exit(1);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        } &lt;span style="color:#0000ff;"&gt;else&lt;/span&gt; {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;            window.setTimeout(&lt;span style="color:#0000ff;"&gt;function&lt;/span&gt; () {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;                page.render(output);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;                phantom.exit();&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            }, 200);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        }&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    });&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, first we are including &lt;strong&gt;Webpage&lt;/strong&gt;, &lt;strong&gt;File System&lt;/strong&gt; and &lt;strong&gt;System&lt;/strong&gt; Module from PhantomJS API. &lt;strong&gt;Webpage&lt;/strong&gt; is needed for loading and rendering of our HTML Chart Template, &lt;strong&gt;File System&lt;/strong&gt; gives us access to local file system for reading and writing the files and &lt;strong&gt;System&lt;/strong&gt; Module is there only for accessing the command arguments on start of the PhantomJS.&lt;/p&gt;

&lt;p&gt;Next thing is to check if the PhantomJS is started with enough arguments, and if everything is fine, we are proceeding to the main part of the script which is nothing more then loading the given HTML file in &lt;strong&gt;Webpage&lt;/strong&gt; Module and calling the &lt;strong&gt;Render()&lt;/strong&gt; method to the given output file. Depending of which file extension has been given to &lt;strong&gt;Render()&lt;/strong&gt; method (PNG, PDF, JPEG or GIF), it will deliver the correct output format of the file.&lt;/p&gt;

&lt;p&gt;Now when everything is in the same folder, we can simply start PhantomJS with following command:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;C:\Temp\Chart&amp;gt; phantomjs.exe render.js donut.html donut.png&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As a result, you should find your Donut Chart as PNG file written on the File System looking something like this:&lt;/p&gt;

&lt;p&gt;&lt;img title="donut" style="display:inline;" alt="donut" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/donut_5F00_thumb_5F00_1AADF2CB.png" width="608" height="416" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1470935" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Javascript" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Javascript/default.aspx"></category><category term="D3.js" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/D3.js/default.aspx"></category><category term="PhantomJS" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/PhantomJS/default.aspx"></category></entry><entry><title>How to use D3.js for Server Side Chart Generation in .NET (Part 1)</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2015/07/15/how-to-use-d3-js-for-graph-generation-in-asp-net-web-application-part-1.aspx" /><id>/blogs/armin_kalajdzija/archive/2015/07/15/how-to-use-d3-js-for-graph-generation-in-asp-net-web-application-part-1.aspx</id><published>2015-07-15T09:44:00Z</published><updated>2015-07-15T09:44:00Z</updated><content type="html">&lt;p&gt;After finally finishing my studies (for now), I am getting my &amp;quot;old life&amp;quot; back again! That means more time for coding, research and of course blogging. As one of my first (small?!) series that I will be writing will be about &amp;quot;How to use D3.js for Server Side Chart Generation in .NET&amp;quot; that will be splitted in few parts, so lets get started!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How to use D3.js for Server Side Chart Generation in .NET&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Part 1: Creating a Donut Chart using D3.js    &lt;br /&gt;&lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2015/07/22/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-2.aspx" target="_blank"&gt;Part 2: Render/Export D3.js Chart as PNG Image using PhantomJS&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2015/07/30/how-to-use-d3-js-for-server-side-chart-generation-in-net-part-3.aspx" target="_blank"&gt;Part 3: Creating a simple REST Service using ASP.NET Web API&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;First of all, we need a simple empty HTML Document that will be used later on as a Tamplate for our chart generation. It has do contain the D3.js logic for our Donut Chart and all other needed assets like CSS, Images etc.&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:360px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:400px;background-color:#f4f4f4;"&gt;   &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;     &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color:#800000;"&gt;DOCTYPE&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;html&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;meta&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;charset&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;style&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;body {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;  position: relative;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;  background-color: #FFF;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;}&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;svg {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    width: 100%;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    height: 100%;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;}&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;text {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    font-size: 15px;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    font-family: Arial;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;}&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;style&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;body&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;src&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;http://d3js.org/d3.v3.min.js&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;div&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;chart&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;body&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, we have defined few CSS global styles for Body, SVG, and Texts and the most important thing, we have included the latest minified D3.js version from their CDN and added a wrapper DIV with &lt;strong&gt;id=”chart”&lt;/strong&gt; that will contain our generated chart.&lt;/p&gt;

&lt;p&gt;Now we should define our &lt;strong&gt;dataset&lt;/strong&gt; that we will be using for our chart generation:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; data = [{ label: &lt;span style="color:#006080;"&gt;&amp;#39;Here&amp;#39;&lt;/span&gt;, value: 190000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#1e398f&amp;quot;&lt;/span&gt; },&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            { label: &lt;span style="color:#006080;"&gt;&amp;#39;Some&amp;#39;&lt;/span&gt;, value: 50000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#f7a600&amp;quot;&lt;/span&gt; },&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;            { label: &lt;span style="color:#006080;"&gt;&amp;#39;Labels&amp;#39;&lt;/span&gt;, value: 420000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#e17e62&amp;quot;&lt;/span&gt; },&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            { label: &lt;span style="color:#006080;"&gt;&amp;#39;Needed&amp;#39;&lt;/span&gt;, value: 1530000, color: &lt;span style="color:#006080;"&gt;&amp;quot;#985181&amp;quot;&lt;/span&gt; }];&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I strongly suggest that you put all properties you will need in the dataset, so you don’t have to change anything in this Template HTML when you start using it from your ASP.NET Web Application later in this series.&lt;/p&gt;

&lt;p&gt;Before we start implementing the chart logic, we should also put following variables that are needed for correct calculation of our donut:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; svg_width = 600;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; svg_height = 400;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#008000;"&gt;/* Radius of the Donut should be a third of defined space */&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; radius = Math.min(svg_width, svg_height) / 3;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Now we are all set to start writing the chart generation code! I will split it in two parts, first will be rendering of needed SVG and Layout elements and second will be rendering of our Donut slices. So first part should look like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:310px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:310px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; svg = d3.select(&lt;span style="color:#006080;"&gt;&amp;quot;#chart&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        .attr(&lt;span style="color:#006080;"&gt;&amp;quot;style&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;width: &amp;quot;&lt;/span&gt; + svg_width + &lt;span style="color:#006080;"&gt;&amp;quot;px; height: &amp;quot;&lt;/span&gt; + svg_height + &lt;span style="color:#006080;"&gt;&amp;quot;px&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        .append(&lt;span style="color:#006080;"&gt;&amp;quot;svg&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        .append(&lt;span style="color:#006080;"&gt;&amp;quot;g&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    svg.append(&lt;span style="color:#006080;"&gt;&amp;quot;g&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        .attr(&lt;span style="color:#006080;"&gt;&amp;quot;class&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;slices&amp;quot;&lt;/span&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; pie = d3.layout.pie()&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    .sort(&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    .value(&lt;span style="color:#0000ff;"&gt;function&lt;/span&gt;(d) {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; d.value;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    });&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; arc = d3.svg.arc()&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    .outerRadius(radius)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    .innerRadius(radius * 0.5);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;svg.attr(&lt;span style="color:#006080;"&gt;&amp;quot;transform&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;translate(&amp;quot;&lt;/span&gt; + svg_width / 2 + &lt;span style="color:#006080;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt; + svg_height / 2 + &lt;span style="color:#006080;"&gt;&amp;quot;)&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;First we have extended our wrapper DIV with &lt;strong&gt;width&lt;/strong&gt; and &lt;strong&gt;height&lt;/strong&gt; styles, and appended a SVG element to it. In this SVG element we will render our Donut chart. Then we created a d3.js &lt;strong&gt;pie layout object&lt;/strong&gt; and assigned a &lt;strong&gt;value&lt;/strong&gt; property to return same named &lt;strong&gt;value&lt;/strong&gt; property from our dataset (return &lt;strong&gt;d.value&lt;/strong&gt;, where “&lt;strong&gt;d&lt;/strong&gt;” is always assigned dataset for given object).&lt;/p&gt;

&lt;p&gt;Next thing is to define an arc object with &lt;strong&gt;outerRadius&lt;/strong&gt; and &lt;strong&gt;innerRadius&lt;/strong&gt;. By defining the &lt;strong&gt;innerRadius&lt;/strong&gt; we have actually created a Donut chart out of Pie chart by creating that hole in the middle of Pie that is half of our radius. &lt;/p&gt;

&lt;p&gt;Last but not least, we want to position the chart in the middle of our wrapping div by calling the &lt;strong&gt;translate&lt;/strong&gt; function with half of &lt;strong&gt;svg_width&lt;/strong&gt; and &lt;strong&gt;svg_height&lt;/strong&gt; as x,y position parameters.&lt;/p&gt;

&lt;p&gt;Now, all we need to do is to generate the Donut/Pie slices in form of SVG Paths and interpolate it on our arc object that is already defined and centered in our wrapper DIV:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;height:350px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right:silver 1px solid;width:97.5%;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:350px;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&lt;span style="color:#008000;"&gt;/* Render Pie Slices */&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; slice = svg.select(&lt;span style="color:#006080;"&gt;&amp;quot;.slices&amp;quot;&lt;/span&gt;).selectAll(&lt;span style="color:#006080;"&gt;&amp;quot;path.slice&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    .data(pie(data));&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;slice.enter()&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    .insert(&lt;span style="color:#006080;"&gt;&amp;quot;path&amp;quot;&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    .style(&lt;span style="color:#006080;"&gt;&amp;quot;fill&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;function&lt;/span&gt;(d) { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; d.data.color; });&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;slice        &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    .transition().duration(1000)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    .attrTween(&lt;span style="color:#006080;"&gt;&amp;quot;d&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;function&lt;/span&gt;(d) {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;._current = &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;._current || d;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; interpolate = d3.interpolate(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;._current, d);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;._current = interpolate(0);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;function&lt;/span&gt;(t) {&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; arc(interpolate(t));&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;        };&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;    })&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:#f4f4f4;"&gt;slice.exit()&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;width:100%;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;border-left-style:none;line-height:12pt;padding-right:0px;background-color:white;"&gt;    .remove();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Here is where the whole “magic” happens, that is at least what I thought when I first started using D3.js!&amp;#160; In short words, we are assigning our dataset to a &lt;strong&gt;pie layout&lt;/strong&gt; object and that object again in our &lt;strong&gt;.selectAll&lt;/strong&gt; statement. 

  &lt;br /&gt;This &lt;strong&gt;.selectAll&lt;/strong&gt; statement can be somehow hard to understand at the beginning but you will get used to it. Now we are going throughout every &lt;strong&gt;dataset&lt;/strong&gt; entry and basically inserting a &lt;strong&gt;path&lt;/strong&gt; element into our SVG with &lt;strong&gt;fill&lt;/strong&gt; color value from our dataset property named &lt;strong&gt;color&lt;/strong&gt; and in next step we are interpolating it on our &lt;strong&gt;arc&lt;/strong&gt; element. &lt;/p&gt;

&lt;p&gt;At the end, we should be able to see our new Donut chart by opening the HTML file in any Browser and it should be looking like on this &lt;a href="http://developers.de/blogs/armin_kalajdzija/d3demos/donut.html" target="_blank"&gt;Preview Page&lt;/a&gt; here.&lt;/p&gt;

&lt;p&gt;In the next Part of this Series, I will be explaining how to Render/Export this SVG Chart as PNG Image file using PhantomJS.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1462421" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="ASP.NET" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx"></category><category term="Javascript" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Javascript/default.aspx"></category><category term=".Net" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/.Net/default.aspx"></category><category term="D3.js" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/D3.js/default.aspx"></category></entry><entry><title>jQuery Mobile - Search Filter placeholder text change</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2013/01/23/jquery-mobile-search-filter-placeholder-text-change.aspx" /><id>/blogs/armin_kalajdzija/archive/2013/01/23/jquery-mobile-search-filter-placeholder-text-change.aspx</id><published>2013-01-23T10:28:00Z</published><updated>2013-01-23T10:28:00Z</updated><content type="html">&lt;p&gt;If you are developing Multilanguage (or more dynamical) jQuery Mobile apps, the first thing you figure out is that Search Filter Placeholder on the of the Views is very much static and all you can do with it is to define its text directly in you HTML Code.&lt;/p&gt;  &lt;p&gt;That was really bad if you needed to change this text dynamically (translation purposes etc.) but with the jQuery Mobile version 4.0 there is way to access this Property from the code. You are still not able to bind it but it is definitely better then having it static.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;All you need to do is following:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background:white;color:green;"&gt;//
// Bind your function on ‘pagechange’ document event
&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;$(document).bind(&lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;#39;pagechange&amp;#39;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;, &lt;/span&gt;&lt;span style="background:white;color:blue;"&gt;function &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;() {
    &lt;/span&gt;&lt;span style="background:white;color:green;"&gt;//Changes the Filter Placeholder text
    &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;changeFilterPlaceholder($(&lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;#39;.ui-page-active&amp;#39;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;).attr(&lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;));
});&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="background:white;color:black;"&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="background:white;color:green;"&gt;//
// Changes the Filter Placeholder Text
&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;changeFilterPlaceholder = &lt;/span&gt;&lt;span style="background:white;color:blue;"&gt;function &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;(id) {

    &lt;/span&gt;&lt;span style="background:white;color:blue;"&gt;var &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;filterText = &lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;;

    &lt;/span&gt;&lt;span style="background:white;color:blue;"&gt;if &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;(id == &lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;quot;Home&amp;quot;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;) {
        filterText = &lt;span style="background:white;color:#a31515;"&gt;&amp;quot;Home or whatever you want&amp;quot;&lt;/span&gt;;
    } &lt;/span&gt;&lt;span style="background:white;color:blue;"&gt;else if &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;(id == &lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;quot;Settings&amp;quot;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;) {
        filterText = &lt;span style="background:white;color:#a31515;"&gt;&amp;quot;Settings of whatever you want&amp;quot;&lt;/span&gt;;
    } &lt;/span&gt;&lt;span style="background:white;color:black;"&gt;

    $(&lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;#39;.ui-input-search &amp;gt; .ui-input-text&amp;#39;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;)&lt;br /&gt;         .attr(&lt;/span&gt;&lt;span style="background:white;color:#a31515;"&gt;&amp;#39;placeholder&amp;#39;&lt;/span&gt;&lt;span style="background:white;color:black;"&gt;, filterText);
}&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="background:white;color:black;"&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;pre class="code"&gt;&lt;span style="background:white;color:black;"&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In the next Posts I will be addressing more typical “Problems” developing 
  &lt;br /&gt;jQuery Mobile apps like Optimizing Performance on iPhone 4 or less, 

  &lt;br /&gt;Translating the jQuery Mobile apps etc. so stay tuned!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=507113" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Javascript" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Javascript/default.aspx"></category><category term="jQueryMobile" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/jQueryMobile/default.aspx"></category><category term="HTML" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/HTML/default.aspx"></category></entry><entry><title>Visual Studio 2012 Debugger: Unable to start program ‘http://localhost/….’ The system cannot find the file specified.</title><link rel="alternate" type="text/html" href="/blogs/armin_kalajdzija/archive/2013/01/16/visual-studio-2012-debugger-unable-to-start-program-http-localhost-the-system-cannot-find-the-file-specified.aspx" /><id>/blogs/armin_kalajdzija/archive/2013/01/16/visual-studio-2012-debugger-unable-to-start-program-http-localhost-the-system-cannot-find-the-file-specified.aspx</id><published>2013-01-16T13:35:39Z</published><updated>2013-01-16T13:35:39Z</updated><content type="html">&lt;p&gt;I used to get this Error Message in Visual Studio 2012 when I was about the start debugging my MVC or any other art of Web Application under Internet Explorer as soon as I installed the first RTM version of Visual Studio 2012 and Windows 8, but I fixed it fast and I thought it is not that big deal to Blog about.&lt;/p&gt;  &lt;p&gt;Lately I saw very much Posts asking about the same problem so I was thinking that I should actually blog about it shortly an so try to save someone&amp;#39;s time and nerves. :)&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;So the Problem is following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;If you are using Windows 8 and Visual Studio 2012&lt;/li&gt;    &lt;li&gt;You want to debug any Web Application using Internet Explorer&lt;/li&gt;    &lt;li&gt;If you use Domain Account and have in you Active Directory a Home Folder configured for you account&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Then you should probably get following Error Message upon starting you Internet Explorer attached to Debugger in Visual Studio:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/153644_5F00_1F6E1D88.png"&gt;&lt;img title="153644" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="153644" src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/153644_5F00_thumb_5F00_3B12A97E.png" width="496" height="191" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;The easiest way (maybe not the only) is to do following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Log in to your Domain Controller and Open “Active Directory Users and Computers”, and under your Account, delete the Home Folder Setting (local path) under Profile Tab.     &lt;br /&gt;(If you don’t have permissions to do this, contact your administrator and ask him to do this)&lt;/li&gt;    &lt;li&gt;Log out from you Workstation, and log in again.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Your debugger should be working now!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=496624" width="1" height="1"&gt;</content><author><name>akalajdzija</name><uri>http://developers.de/members/akalajdzija/default.aspx</uri></author><category term="Visual Studio" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Visual+Studio/default.aspx"></category><category term="Tips &amp;amp; Tricks" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Tips+_2600_amp_3B00_+Tricks/default.aspx"></category><category term="Debugging" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/Debugging/default.aspx"></category><category term=".Net" scheme="http://developers.de/blogs/armin_kalajdzija/archive/tags/.Net/default.aspx"></category></entry></feed>