Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. public void Configuration(IAppBuilder app)
  2. {
  3. app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  4. app.UseWsFederationAuthentication(
  5. new WsFederationAuthenticationOptions
  6. {
  7. Wtrealm = realm,
  8. MetadataAddress = adfsMetadata
  9. });
  10. app.UseCookieAuthentication(new CookieAuthenticationOptions
  11. {
  12. AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
  13. });
  14. }
  15.  
  16. using System;
  17. using System.IdentityModel.Protocols.WSTrust;
  18. using System.IdentityModel.Tokens;
  19. using System.Net;
  20. using System.Net.Http;
  21. using System.ServiceModel;
  22. using System.ServiceModel.Security;
  23. using Thinktecture.IdentityModel.Extensions;
  24. using Thinktecture.IdentityModel.WSTrust;
  25.  
  26. namespace ConsoleApplication1
  27. {
  28. class Program
  29. {
  30. private const string UserName = "USERNAME";
  31. private const string Password = "PASSWORD";
  32. private const string Domain = "DOMAIN";
  33. private const string ADFSEndpoint = "ADFS ENDPOINT";
  34. private const string ApiBaseUri = "THE API";
  35. private const string ApiEndPoint = "AN ENDPOINT";
  36.  
  37. static void Main(string[] args)
  38. {
  39. SecurityToken token = RequestSecurityToken(); // Obtain security token from ADFS.
  40. CallApi(token); // Call api.
  41. Console.ReadKey(); // Stop console from closing
  42. }
  43.  
  44. private static SecurityToken RequestSecurityToken()
  45. {
  46. var trustChannelFactory =
  47. new WSTrustChannelFactory(new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
  48. new EndpointAddress(new Uri(ADFSEndpoint)))
  49. {
  50. TrustVersion = TrustVersion.WSTrust13,
  51. Credentials = { UserName = { UserName = UserName + "@" + Domain, Password = Password } },
  52. };
  53.  
  54. var requestSecurityToken = new RequestSecurityToken
  55. {
  56. RequestType = RequestTypes.Issue,
  57. KeyType = KeyTypes.Bearer,
  58. AppliesTo = new EndpointReference(ApiBaseUri)
  59. };
  60.  
  61. RequestSecurityTokenResponse response;
  62. var securityToken = trustChannelFactory.CreateChannel().Issue(requestSecurityToken, out response);
  63.  
  64. return securityToken;
  65. }
  66.  
  67. private static async void CallApi(SecurityToken securityToken)
  68. {
  69. using (var handler = new HttpClientHandler { CookieContainer = new CookieContainer() })
  70. {
  71. using (var client = new HttpClient(handler))
  72. {
  73. handler.CookieContainer.MaxCookieSize = 8000; // Trying to make sure I can fit it in the cookie
  74.  
  75. var cookie = new Cookie {
  76. Name = "FedAuth",
  77. Value = Base64Encode(securityToken.ToTokenXmlString()),
  78. HttpOnly = true,
  79. Secure = true
  80. };
  81. handler.CookieContainer.Add(new Uri(ApiBaseUri), cookie);
  82. var response = client.GetAsync(new Uri(ApiBaseUri + ApiEndPoint)).Result;
  83. string result = await response.Content.ReadAsStringAsync();
  84. Console.WriteLine(result);
  85. }
  86. }
  87. }
  88.  
  89. public static string Base64Encode(string plainText)
  90. {
  91. var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  92. return System.Convert.ToBase64String(plainTextBytes);
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement