Authenticate user by ADFS (Active Directory Federation Service)

nunu picture nunu · May 18, 2012 · Viewed 16.1k times · Source

I need to check whether particular user exist OR not in Active Directory by ADFS.

So, I want my ADFS to check user Authentication by UserName/Password.

Could anybody please provide the sample code OR tutorial for the same.

Thanks in advance!

Answer

flayn picture flayn · May 23, 2012

To use Username/Password authentication you can use the

trust/13/UsernameMixed

endpoint of the ADFS 2.0.

This does NOT check if the user exists in the Active Directory!

In code you request the token like this:

WSTrustChannelFactory adfsfactory = new WSTrustChannelFactory(new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
                            StsEndpoint);

adfsfactory.TrustVersion = TrustVersion.WSTrust13;

// Username and Password here...
factory.Credentials.UserName.UserName = "domain\username";
factory.Credentials.UserName.Password = "password";

IWSTrustChannelContract channel = adfsfactory.CreateChannel();

// request the token
SecurityToken token = channel.Issue(rst);

Then create the channel factory for your service using your token:

var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.Message);

var factory = new ChannelFactory<IYourInterface >(binding, "your service address");

factory.ConfigureChannelFactory();

IYourInterface channel = factory.CreateChannelWithIssuedToken(token);

Hope this helps!