Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Configuration(IAppBuilder app)
- {
- app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
- app.UseWsFederationAuthentication(
- new WsFederationAuthenticationOptions
- {
- Wtrealm = realm,
- MetadataAddress = adfsMetadata
- });
- app.UseCookieAuthentication(new CookieAuthenticationOptions
- {
- AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
- });
- }
- using System;
- using System.IdentityModel.Protocols.WSTrust;
- using System.IdentityModel.Tokens;
- using System.Net;
- using System.Net.Http;
- using System.ServiceModel;
- using System.ServiceModel.Security;
- using Thinktecture.IdentityModel.Extensions;
- using Thinktecture.IdentityModel.WSTrust;
- namespace ConsoleApplication1
- {
- class Program
- {
- private const string UserName = "USERNAME";
- private const string Password = "PASSWORD";
- private const string Domain = "DOMAIN";
- private const string ADFSEndpoint = "ADFS ENDPOINT";
- private const string ApiBaseUri = "THE API";
- private const string ApiEndPoint = "AN ENDPOINT";
- static void Main(string[] args)
- {
- SecurityToken token = RequestSecurityToken(); // Obtain security token from ADFS.
- CallApi(token); // Call api.
- Console.ReadKey(); // Stop console from closing
- }
- private static SecurityToken RequestSecurityToken()
- {
- var trustChannelFactory =
- new WSTrustChannelFactory(new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
- new EndpointAddress(new Uri(ADFSEndpoint)))
- {
- TrustVersion = TrustVersion.WSTrust13,
- Credentials = { UserName = { UserName = UserName + "@" + Domain, Password = Password } },
- };
- var requestSecurityToken = new RequestSecurityToken
- {
- RequestType = RequestTypes.Issue,
- KeyType = KeyTypes.Bearer,
- AppliesTo = new EndpointReference(ApiBaseUri)
- };
- RequestSecurityTokenResponse response;
- var securityToken = trustChannelFactory.CreateChannel().Issue(requestSecurityToken, out response);
- return securityToken;
- }
- private static async void CallApi(SecurityToken securityToken)
- {
- using (var handler = new HttpClientHandler { CookieContainer = new CookieContainer() })
- {
- using (var client = new HttpClient(handler))
- {
- handler.CookieContainer.MaxCookieSize = 8000; // Trying to make sure I can fit it in the cookie
- var cookie = new Cookie {
- Name = "FedAuth",
- Value = Base64Encode(securityToken.ToTokenXmlString()),
- HttpOnly = true,
- Secure = true
- };
- handler.CookieContainer.Add(new Uri(ApiBaseUri), cookie);
- var response = client.GetAsync(new Uri(ApiBaseUri + ApiEndPoint)).Result;
- string result = await response.Content.ReadAsStringAsync();
- Console.WriteLine(result);
- }
- }
- }
- public static string Base64Encode(string plainText)
- {
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
- return System.Convert.ToBase64String(plainTextBytes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement