ID3242: The security token could not be authenticated or authorized
<p>At the moment i'm working on a project where my application has to validate with ADFS to login.</p>

<p>I found some code to get the token from ADFS but it keeps giving me the error above without any good description about the error.</p>

<p>This is the code I use:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Net;
using System.IdentityModel.Protocols.WSTrust;
using System.IdentityModel.Tokens;

namespace ADFS_token_test_3
{
    class Program
    {
    static void Main(string[] args)
    {
        go();
    }

    static public EndpointAddress ep;
    static public WSTrustChannelFactory factory;

    public static string go()
    {
        WS2007HttpBinding binding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential);
        binding.Security.Message.EstablishSecurityContext = false;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

        binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
        ep = new EndpointAddress("https://companyname.nl/adfs/services/trust/13/usernamemixed");

        factory = new WSTrustChannelFactory(binding, ep);
        factory.TrustVersion = TrustVersion.WSTrust13;

        factory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        factory.Credentials.UserName.UserName = "username";
        factory.Credentials.UserName.Password = "password";

        var rst = new RequestSecurityToken
        {
            RequestType = RequestTypes.Issue,
            AppliesTo = new EndpointReference("https://companyname.nl/adfs/services/trust/13/usernamemixedr"),
            KeyType = KeyTypes.Bearer,
        };

        IWSTrustChannelContract channel = factory.CreateChannel();
        // Error line 
        GenericXmlSecurityToken genericToken = channel.Issue(rst)
         as GenericXmlSecurityToken;

        return genericToken.TokenXml.InnerXml.ToString();
    }
  }
}
</code></pre>

<p>Error occurs on the line with the error comment.</p>

<p>Anybody has a clue?</p>