Skip to content

Instantly share code, notes, and snippets.

@AlexanderBrevig
Last active August 29, 2015 13:57
Show Gist options
  • Save AlexanderBrevig/9806518 to your computer and use it in GitHub Desktop.
Save AlexanderBrevig/9806518 to your computer and use it in GitHub Desktop.
using Microsoft.IdentityModel.Protocols.WSTrust;
using Microsoft.IdentityModel.Protocols.WSTrust.Bindings;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.IdentityModel.Tokens;
string stsEndpoint = "https://YOURURL.com/adfs/services/trust/13/usernamemixed";
string relyingPartyUri = "https://attensi.com";
WSTrustChannelFactory factory = new WSTrustChannelFactory(
new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
new EndpointAddress(stsEndpoint)
);
factory.TrustVersion = TrustVersion.WSTrust13;
factory.Credentials.UserName.UserName = "pwcusername";
factory.Credentials.UserName.Password = "pwcpassword";
RequestSecurityToken rst = new RequestSecurityToken
{
RequestType = Microsoft.IdentityModel.Protocols.WSTrust.WSTrust13Constants.RequestTypes.Issue,
AppliesTo = new EndpointAddress(relyingPartyUri),
KeyType = Microsoft.IdentityModel.Protocols.WSTrust.WSTrust13Constants.KeyTypes.Bearer,
};
IWSTrustChannelContract channel = factory.CreateChannel();
try {
SecurityToken token = channel.Issue(rst);
Console.WriteLine("Token Id: " + token.Id);
catch (Exception e){
//if authentication is failed, exception will be thrown. Error is inside the innerexception.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment