{"componentChunkName":"component---src-templates-blog-post-js","path":"/2008/08/08/aspnet-delegate-identity-from-a-web-application-to-a-back-end-web-application/","result":{"data":{"site":{"siteMetadata":{"title":"Vidar's musings"}},"markdownRemark":{"id":"32764ae4-8a38-571a-a953-b1a7bc271746","excerpt":"One of the things that seem very simple on a Powerpoint presentation, but are not that simple in practice, is having a web user’s identity forwarded from a…","html":"<p>One of the things that seem very simple on a Powerpoint presentation, but are not that simple in practice, is having a web user’s identity forwarded from a calling web application to another web application when using Kerberos.</p>\n<p>The case is as follows: I have an intranet application A which uses Integrated Windows Authentication to authenticate the user. During processing of a request from a web users, application A then makes an HTTP request to intranet application B. Application B requires the web user to be authenticated to process the request. The often most attractive solution for solving this is what Microsoft refers to as identity delegation. Simple in a Powerpoint presentation, but alas, not so simple in practice.</p>\n<p>First of all, there are a number of preconditions in the computing environment configuration that need to be fulfilled. I found a very good summary of gotchas in this respect <a href=\"http://forums.asp.net/p/163508/400236.aspx#163797\">here</a>. In my case, the points 2 and 6 was missing (I knew about the other once beforehand). So, when all configuration stuff set up, then the only thing left is the code and configuration in the application A.</p>\n<p>Basically, you need to make the application impersonate the web user (meaning that it will run with the credentials of the web user). There are two ways to do this. If you wish the entire request to run as the web user, you can insert an &#x3C;identity impersonate=”true” /> element under &#x3C;system.web> in the application’s web.config. Or, if you wish only the request to application B to run as the web user, you can do this programmatically:</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token keyword\">using</span> <span class=\"token namespace\">System<span class=\"token punctuation\">.</span>Security<span class=\"token punctuation\">.</span>Principal</span><span class=\"token punctuation\">;</span>\n<span class=\"token range operator\">..</span><span class=\"token punctuation\">.</span>\n<span class=\"token class-name\">WindowsIdentity</span> identity <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>WindowsIdentity<span class=\"token punctuation\">)</span>HttpContext<span class=\"token punctuation\">.</span>Current<span class=\"token punctuation\">.</span>User<span class=\"token punctuation\">.</span><span class=\"token function\">Identity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">using</span> <span class=\"token punctuation\">(</span>identity<span class=\"token punctuation\">.</span><span class=\"token function\">Impersonate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// ... code to call application B goes here ...</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Then, the next task is to call application B itself. You can do this by creating a web request:</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token class-name\">HttpWebRequest</span> request <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>HttpWebRequest<span class=\"token punctuation\">)</span>WebRequest<span class=\"token punctuation\">.</span><span class=\"token function\">Create</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"http://www.somethingcompletelydifferent.com\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nrequest<span class=\"token punctuation\">.</span>ImpersonationLevel <span class=\"token operator\">=</span> System<span class=\"token punctuation\">.</span>Security<span class=\"token punctuation\">.</span>Principal<span class=\"token punctuation\">.</span>TokenImpersonationLevel<span class=\"token punctuation\">.</span>Delegation<span class=\"token punctuation\">;</span>\nrequest<span class=\"token punctuation\">.</span>UseDefaultCredentials <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n<span class=\"token range operator\">..</span><span class=\"token punctuation\">.</span>\n<span class=\"token class-name\">HttpWebResponse</span> response <span class=\"token operator\">=</span> request<span class=\"token punctuation\">.</span><span class=\"token function\">GetResponse</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token range operator\">..</span><span class=\"token punctuation\">.</span></code></pre></div>\n<p>The important things to notice here is that we set the ImpersonationLevel property to “Delegation” and that we set the UseDefaultCredentials property to “true”. So, it together, we get:</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token keyword\">using</span> <span class=\"token namespace\">System<span class=\"token punctuation\">.</span>Security<span class=\"token punctuation\">.</span>Principal</span><span class=\"token punctuation\">;</span>\n<span class=\"token range operator\">..</span><span class=\"token punctuation\">.</span>\n<span class=\"token class-name\">WindowsIdentity</span> identity <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>WindowsIdentity<span class=\"token punctuation\">)</span>HttpContext<span class=\"token punctuation\">.</span>Current<span class=\"token punctuation\">.</span>User<span class=\"token punctuation\">.</span><span class=\"token function\">Identity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">using</span> <span class=\"token punctuation\">(</span>identity<span class=\"token punctuation\">.</span><span class=\"token function\">Impersonate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token class-name\">HttpWebRequest</span> request <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>HttpWebRequest<span class=\"token punctuation\">)</span>WebRequest<span class=\"token punctuation\">.</span><span class=\"token function\">Create</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"http://www.somethingcompletelydifferent.com\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    request<span class=\"token punctuation\">.</span>ImpersonationLevel <span class=\"token operator\">=</span> System<span class=\"token punctuation\">.</span>Security<span class=\"token punctuation\">.</span>Principal<span class=\"token punctuation\">.</span>TokenImpersonationLevel<span class=\"token punctuation\">.</span>Delegation<span class=\"token punctuation\">;</span>\n    request<span class=\"token punctuation\">.</span>UseDefaultCredentials <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n    <span class=\"token range operator\">..</span><span class=\"token punctuation\">.</span>\n    <span class=\"token class-name\">HttpWebResponse</span> response <span class=\"token operator\">=</span> request<span class=\"token punctuation\">.</span><span class=\"token function\">GetResponse</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token range operator\">..</span><span class=\"token punctuation\">.</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>You can then test that it works in application B by checking the name in <code class=\"language-text\">HttpContext.Current.User.Identity.Name</code>.</p>","frontmatter":{"title":"ASP.NET: Delegate identity from a web application to a back end web application","date":"August 08, 2008","description":null}},"previous":{"fields":{"slug":"/2008/08/07/springnet-programatically-add-objects-to-the-existing-xml-application-context/"},"frontmatter":{"title":"Spring.NET: programatically add objects to the existing (XML) application context"}},"next":{"fields":{"slug":"/2008/09/19/pure-functions-and-testability/"},"frontmatter":{"title":"Pure functions and testability"}}},"pageContext":{"id":"32764ae4-8a38-571a-a953-b1a7bc271746","previousPostId":"aff7979d-8997-5f6b-b758-2a20f94217a1","nextPostId":"bccb5fa4-8d12-5279-b8ad-ed9633567b2a"}},"staticQueryHashes":["2841359383","3257411868"]}