ID4257: X.509 certificate 'CN=SharePoint Security Token Service, OU=SharePoint, O=Microsoft, C=US' validation failed by the token handler.

I am trying to build a Security Token, Serialize, Read and Validate it, all inside a Console Application. Following is the code. I am getting an exception while trying to Validate the Token and also while trying to write (serialize) the token. The SharePoint 2013 site I am using here uses FBA Claims and the site is a Multi Tenant site.

 string siteUrl = "url of the site";
                Uri uri = new Uri(siteUrl);
                System.IdentityModel.Tokens.SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(uri, "CustomMembershipProvider", "CustomRoleProvider", "user1", "password1", SPFormsAuthenticationOption.PersistentSignInRequest);
                if (token != null)
                {
                    string assertionXML = ((System.IdentityModel.Tokens.GenericXmlSecurityToken)(token)).TokenXml.OwnerDocument.InnerXml;
                    using (StringReader sr = new StringReader(assertionXML))
                    {
                        using (XmlReader reader = XmlReader.Create(sr))
                        {
                            if (!reader.ReadToFollowing("saml:Assertion"))
                            {
                                throw new Exception("Assertion not found!");
                            }
                            SecurityTokenHandlerCollection collection = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
                            collection.ElementAt(2).Configuration.AudienceRestriction.AllowedAudienceUris.Add(uri);
                            //string serializedToken = collection.WriteToken(token); <-- This line also throws exception
                            System.IdentityModel.Tokens.SecurityToken newToken = collection.ReadToken(reader.ReadSubtree());
                            ReadOnlyCollection<System.Security.Claims.ClaimsIdentity> claims = collection.ValidateToken(newToken); <-- This line also throws exception
                        }
                    }
                }
Can anyone please help?

September 12th, 2015 1:58pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics