title: C# - Retrieve X-Auth-Token via ADFS Authentication
body: <p>We currently have a corporate web application that runs on Apache. It is setup for SAML authentication via ADFS 4.0. When accessing the application via a browser, the users windows credentials (Intranet Zone Setting) are passed through to ADFS and then a successful login attempt is made.</p>
  
  <p>The web application can be accessed using rest APIs. In order to execute/use the web API's, a X-Auth-Token header (64 characters long) needs to be passed to it. Via the browser, if i go to <a href="https://webapp.com/api/v1/session/saml-login" rel="nofollow noreferrer">https://webapp.com/api/v1/session/saml-login</a>, a JSON response is returned after being authenticated via ADFS. The first entry in the web application JSON response is the X-Auth-Token. </p>
  
  <p>I would like to develop various client applications that will leverage the rest API's. Because the applications will be executed client side, i do not want to pass usernames and passwords, so instead i thought of authenticating the client application via the users context using ADFS. Note: There is the ability to pass explicit user application credentials via a JSON payload to <a href="https://webapp.com/api/v1/session/login" rel="nofollow noreferrer">https://webapp.com/api/v1/session/login</a> which in turn does return the X-Auth-Token, and i have tested this with a c# application - and it works... but as mentioned before, i do not want to use explicit credentials.</p>
  
  <p>As a start, i have created another c# application (using the code from <a href="https://gavindraper.com/2013/01/31/ad-fs-token-based-authentication-in-code/" rel="nofollow noreferrer">https://gavindraper.com/2013/01/31/ad-fs-token-based-authentication-in-code/</a>) which returns the following token claim information from ADFS.</p>
  
  <p><a href="https://i.stack.imgur.com/vacmJ.jpg" rel="nofollow noreferrer">Claim Response Screenshot</a></p>
  
  <p>Since i believe i have successfully authenticated to ADFS, is there anyway i can via c# or another means, pass this claim information to the web application server so i can then get the X-Auth-Token and store it as a variable so i can then pass it to my c# code for web application API authentication? </p>
  
  <p>From within the same c# application instance, if i execute </p>
  
  <pre><code>var client = new WebClient();
  var text = client.DownloadString("https://webapp.com/api/v1/session/saml-login");
  Console.WriteLine(text);
  Console.ReadLine();
  </code></pre>
  
  <p>I was hoping the X-Auth-Token in the JSON response would be returned from the above code. But this is not the case... instead the output shows login failure to ADFS because windows credentials were not used to authenticate to ADFS.</p>
  
  <p>Anyway possible i can simply get the X-Auth-Token from the web application server by passing the ADFS claim without the use of explicit user application or windows credentials?</p>
  
  <p>Cheers</p>
  
  <p>Craig</p>