How to authenticate with ADSF in Silverlight 5 Application
Published Nov 16 2018 06:47 AM 399 Views
First published on MSDN on Jul 18, 2013

How to authenticate with ADSF in Silverlight 5 Application

This article uses sample from as start point and gives an example how to modify that sample to setup ADSF as STS token service for Silverlight 5 to authenticate against. ADSF returns saml token to Web Application that hosting SL5 application, and is decrypted and then get user Claims back to Silverlight Application for later WCF calls from Silverlight Application.

1. We start with Sample from - download and setup the sample. This sample uses its own in-process STS service. We are going to replace the STS service with ADSF as STS token service.

2. Remove the project IdentityProviderAndSts from the sample solution.

3. Add ADSF STS token service

See the attached document for examples how to add ADSF STS token service in a project. Note the attached doc is a separated example on how to add ADSF STS token service to MVC Web Application. It is referenced here for the screen shots in the document how to add ADSF STS Service Reference - the stesp are the same. You can ignore the MVC part which is unrelated to this topic.

4. Deploy cleintaccesspolicy.xml to the root of the web site for ADSF, otherwise cross domain calls will be blocked.

Below is an example. Tighten teh security as you need,

<?xml version="1.0" encoding="utf-8"?>
<allow-from http-methods="*" http-request-headers="*">
<domain uri="*"/>
<domain uri="http://*" />
<domain uri="https://*" />
<resource path="/" include-subpaths="true"/>
<socket-resource port="4502-4530" protocol="tcp" />

5. If you see error regarding audienceUri , you will need to modify the web.config as below:


ID1032: At least one 'audienceUri' must be specified in the SamlSecurityTokenRequirement when the AudienceUriMode is set to 'Always' or 'BearerKeyOnly'.Either add the valid URI values to the AudienceUris property of SamlSecurityTokenRequirement,  or turn off checking by specifying an AudienceUriMode of 'Never' on the SamlSecurityTokenRequirement.

Resolve this error by adding (or changing) this in web.config. Note it is the Service tag that does not have any name in it. (there are other service tags with name= in web.config, those are generated by when adding the ADSF STS reference and so cannot be changed.)

Note: in this sample below, is the web application hosting the Silverlight Application.

And, is the ADSF server.



<audienceUris mode="Never">

<add value= />



<wsFederation passiveRedirectEnabled="false"
requireHttps="true" />

<cookieHandler requireSsl="true" />




<!--Following are the claims offered by STS 'http://localhost/IdentityProviderAndSts'. Add or uncomment claims that you require by your application and then update the federation metadata of this application.-->

<claimType type= optional="true" />

<claimType type= optional="true" />



<issuerNameRegistry type="Microsoft.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35">


<add thumbprint="E189797A491467A5B1DB027D1F5EFBB97B1D0476" name="" />



<certificateValidation certificateValidationMode="None"/>



comment out
<!--federatedServiceHostConfiguration name="FibonacciService.FibonacciService" /--> if exists.

6. Decrypt the raw token returned by ADSF in a helper project.

Take the sample solution as an example,  we need to change the code in tokenProcessor.cs (in SL.IdentityModel.Server project):

private SecurityToken ReadXmlToken(string
using (StringReader strReader = new StringReader(tokenXml))

using (XmlReader reader=XmlReader.Create(strReader))


X509Certificate2 cert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=DefaultApplicationCertificate");

SecurityTokenHandlerCollection handlers = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();

List<SecurityToken>tokens = new List<SecurityToken>();

tokens.Add(new X509SecurityToken(cert));

SecurityTokenResolver serviceResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(new ReadOnlyCollection<SecurityToken>(tokens), true);

handlers.Configuration.ServiceTokenResolver = serviceResolver;

handlers.Configuration.AudienceRestriction.AllowedAudienceUris.Add(new Uri(""));

var registry = new ConfigurationBasedIssuerNameRegistry();

registry.AddTrustedIssuer("Thumbprint", "");

handlers.Configuration.IssuerNameRegistry = registry;

var samlToken = handlers.ReadToken(reader);

return samlToken;


7. If you are considering passing the raw token xml returned by ADSF to Silverlight application for further options such as passing the raw token with the WCF calls and let remote WCF service decrypt the raw token to get User Claims out of it, below is how you can add the raw token into cache and then retrieve it in Silverlight Application. Note we cannot decrypt the token inside Silverlight project.

In ClaimsIdentitySessionManager.cs file (project SL.IdentityModel), modify the following code:

private void trustClient_IssueCompleted( object sender, IssueCompletedEventArgs e )


if ( null == e.Error)


//jason added

RequestSecurityTokenResponse rstr = e.Result;

string appliesTo;

if (null == rstr.AppliesTo || null == rstr.AppliesTo.Uri)


throw new ArgumentException("No appliesTo in RequestSecurityTokenResponse");


appliesTo = rstr.AppliesTo.Uri.AbsoluteUri;

_tokenCache.AddTokenToCache(rstr.AppliesTo.Uri.AbsoluteUri, rstr);

//jason added ends

ClaimsIdentitySessionManager.Current.SetSessionCookieAsync( e.Result.RequestedSecurityToken.RawToken );


else if ( null != SignInComplete )


SignInComplete( sender, new SignInEventArgs( null, e.Error ) );



//jason added

public RequestSecurityTokenResponse getRSTRFromTokenCache(string appliesTo)


return _tokenCache.GetTokenFromCache(appliesTo);


//jason added ends

Retrieve the raw token in SL5 application from the cache, such as mainpage.xaml.cs:

if (ClaimsIdentitySessionManager.Current.User.ClaimsIdentity.IsAuthenticated)

RequestSecurityTokenResponse rsts=ClaimsIdentitySessionManager.Current.getRSTRFromTokenCache("");

RequestedSecurityToken token = rsts.RequestedSecurityToken;


PS. ADSF Raw token xml Example:

/><KeyInfo xmlns=\"\"><e:EncryptedKey
Algorithm=\"\" /></e:EncryptionMethod><KeyInfo><o:SecurityTokenReference

MVC3 with Windows Identity Foundation and ADFS.docx

Version history
Last update:
‎Nov 16 2018 06:47 AM
Updated by: