Поделиться через


How to authenticate with ADSF in Silverlight 5 Application

How to authenticate with ADSF in Silverlight 5 Application

This article uses sample from https://hashtagfail.com/post/11094642160/wcf-wif-silverlight-active-federation 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 https://hashtagfail.com/post/11094642160/wcf-wif-silverlight-active-federation - 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"?>
<access-policy>
   <cross-domain-access>
     <policy>
       <allow-from http-methods="*" http-request-headers="*">    
         <domain uri="*"/>
 <domain uri="https://*" />
 <domain uri="https://*" />
       </allow-from>    
       <grant-to>    
         <resource path="/" include-subpaths="true"/>
  <socket-resource port="4502-4530" protocol="tcp" />
       </grant-to>
     </policy>
   </cross-domain-access>
</access-policy>

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

 

Error:

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, https://sqlbackend.contoso.com/FibonacciFederatedAuth/ is the web application hosting the Silverlight Application.

And, SQLReports.contoso.com is the ADSF server.

 

<microsoft.identityModel>

<service>

     
<audienceUris mode="Never">

       
<add value=https://sqlbackend.contoso.com/FibonacciFederatedAuth/ />

     
</audienceUris>

     
<federatedAuthentication>

       
<wsFederation passiveRedirectEnabled="false"
issuer="https://sqlreports.contoso.com/adfs/services/trust/13/usernamemixed"
realm="https://sqlbackend.contoso.com/FibonacciFederatedAuth"
requireHttps="true" />

       
<cookieHandler requireSsl="true" />

     
</federatedAuthentication>

     
<applicationService>

       
<claimTypeRequired>

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

         
<claimType type=https://schemas.xmlsoap.org/ws/2005/05/identity/claims/name optional="true" />

         
<claimType type=https://schemas.microsoft.com/ws/2008/06/identity/claims/role optional="true" />

       
</claimTypeRequired>

     
</applicationService>

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

       
<trustedIssuers>

         
<add thumbprint="E189797A491467A5B1DB027D1F5EFBB97B1D0476" name="https://SQLReports.contoso.com/adfs/services/trust" />

       
</trustedIssuers>

     
</issuerNameRegistry>

     
<certificateValidation certificateValidationMode="None"/>

 </service>

</microsoft.identityModel>

 

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
tokenXml)      
{
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("https://sqlreports.contoso.com/adfs/services/trust/13/usernamemixed"));

                    var registry = new ConfigurationBasedIssuerNameRegistry();

                   registry.AddTrustedIssuer("Thumbprint", "https://SQLReports.contoso.com/adfs/services/trust");

                    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("https://sqlbackend.contoso.com/FibonacciFederatedAuth/");

               
RequestedSecurityToken token = rsts.RequestedSecurityToken;

            }

 

PS. ADSF Raw token xml Example:

 

<xenc:EncryptedData
Type=\"https://www.w3.org/2001/04/xmlenc#Element\"
xmlns:xenc=\"https://www.w3.org/2001/04/xmlenc#\"><xenc:EncryptionMethod
Algorithm=\"https://www.w3.org/2001/04/xmlenc#aes256-cbc\"
/><KeyInfo xmlns=\"https://www.w3.org/2000/09/xmldsig#\"><e:EncryptedKey
xmlns:e=\"https://www.w3.org/2001/04/xmlenc#\"><e:EncryptionMethod
Algorithm=\"https://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p\"><DigestMethod
Algorithm=\"https://www.w3.org/2000/09/xmldsig#sha1\" /></e:EncryptionMethod><KeyInfo><o:SecurityTokenReference
xmlns:o=\"https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><X509Data><X509IssuerSerial><X509IssuerName>CN=DefaultApplicationCertificate</X509IssuerName><X509SerialNumber>56811571836600510090139452222342497478</X509SerialNumber></X509IssuerSerial></X509Data></o:SecurityTokenReference></KeyInfo><e:CipherData><e:CipherValue>yQQxlmji+Czxpnban/MX6rD40I4NJU728KtHqBl1cFz7dQXvokqNZYklY0V6MK9k8ICtfTOYe7t8dQOwYOYQ/SuVFrGeXGgVAibT8DN4DpZmlVHgFIXHcL7XVIdTrFYYwVdrNrkrvnT+DxCTx8oCUR1ddbrqPBwchwqYCy6UPmDjUj2p0Wwftse/fcMUmoVObGr79PxqKFp02HrVX481Fpoov5HKBaT0AGUH/ziniwCgeM6p2XBn3JC3JmiCQzFukKNpJtzWXJH0xc6eEZr57NxybZ2+zvYa85uKiJGIUahgu+CjR6Z3dNDaPZ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