Last active
May 6, 2017 10:00
-
-
Save pkarneliuk/e80e1a3be2fc6016f20f8bd494b9f0c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Net; | |
using System.ServiceModel; | |
using System.ServiceModel.Security; | |
using System.ServiceModel.Channels; | |
using System.ServiceModel.Description; | |
using System.IdentityModel; | |
using System.IdentityModel.Protocols.WSTrust; | |
using System.IdentityModel.Tokens; | |
using Microsoft.IdentityModel.Protocols.WSTrust.Bindings; | |
using Microsoft.IdentityModel.Protocols.WSTrust; | |
using System.ServiceModel.Dispatcher; | |
using WSTrustChannelFactory = System.ServiceModel.Security.WSTrustChannelFactory; | |
using RequestSecurityToken = System.IdentityModel.Protocols.WSTrust.RequestSecurityToken; | |
using WSTrustChannel = System.ServiceModel.Security.WSTrustChannel; | |
namespace SAML_Example | |
{ | |
class SAML_Example | |
{ | |
static void Main(string[] args) | |
{ | |
var appliesTo = "https://epbyminw1035t1"; | |
var dmsPath = "/api/v1/session/saml-login"; | |
var relyingpartyEndpoint = appliesTo + dmsPath; | |
try | |
{ | |
string samlToken = GetSamlClaimWSTrustKRB(appliesTo); | |
Console.WriteLine(samlToken); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
} | |
public static string GetSamlClaimWSTrustKRB(string appliesTo) | |
{ | |
// Allow all certificates | |
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; | |
var endpointAddress = "https://ping.cluster.dom:9031/idp/sts.wst?TokenProcessorId=Username"; | |
EndpointAddress ep = new EndpointAddress(new Uri(endpointAddress)); | |
UserNameWSTrustBinding binding = new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential, HttpClientCredentialType.Basic); | |
WSTrustChannelFactory factory = new WSTrustChannelFactory(binding, ep); | |
ClientCredentials clientCredentials = new ClientCredentials(); | |
clientCredentials.UserName.UserName = "username"; // User, Password for http basic authentication | |
clientCredentials.UserName.Password = "password"; | |
factory.Endpoint.Behaviors.RemoveAll<ClientCredentials>();//Remove previous clientCredentials | |
factory.Endpoint.EndpointBehaviors.Add(clientCredentials); | |
factory.TrustVersion = TrustVersion.WSTrustFeb2005; | |
factory.Credentials.SupportInteractive = false; | |
WSTrustChannel channel = (WSTrustChannel)factory.CreateChannel(); | |
var rst = new RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new EndpointReference(appliesTo) }; | |
rst.TokenType = Microsoft.IdentityModel.Tokens.SecurityTokenTypes.OasisWssSaml2TokenProfile11; | |
try | |
{ | |
Console.WriteLine("Attempting to retrieve SAML assertion"); | |
GenericXmlSecurityToken token = channel.Issue(rst) as GenericXmlSecurityToken; | |
Console.WriteLine("Successfully retrieved SAML assertion"); | |
return token.TokenXml.OuterXml; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Exception: " + ex.Message); | |
Console.WriteLine("InnerException: " + ex.InnerException); | |
throw ex; | |
} | |
finally | |
{ | |
factory.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
server.log lines related to invalid response for request SAML token with configuration "DEFAULT TOKEN TYPE" SAML 2.0