April 13th, 2012
Requesting a Token from ADFS 2.0 using WS-Trust with Username and Password
In a previous post I showed how to request tokens to ADFS using WS-Trust based on the identity of the user that requests the token.
Due to I’ve received a lot of requests on the subject, here’s the code to do the same but using username and password, I mean request tokens from ADFS 2.0 using username and password based identity.
var stsEndpoint = "https://[server]/adfs/services/trust/13/UsernameMixed"; var relayPartyUri = "https://localhost:8080/WebApp"; var factory = new WSTrustChannelFactory( new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential), new EndpointAddress(stsEndpoint)); factory.TrustVersion = TrustVersion.WSTrust13; // Username and Password here... factory.Credentials.UserName.UserName = user; factory.Credentials.UserName.Password = password; var rst = new RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new EndpointAddress(relayPartyUri), KeyType = KeyTypes.Bearer, }; var channel = factory.CreateChannel(); SecurityToken token = channel.Issue(rst);
I hope you find it useful!
April 16th, 2012 at 1:29
[...] Requesting a Token from ADFS 2.0 using WS-Trust with Username and Password [...]
October 8th, 2012 at 11:16
Hi Leandro,
How do I add this token into a webservice call?
WCF call? or can I use it with ASMX?
Regards,
James