Search code examples
asp.netsslowinx509certificate2ws-federation

WSFederation Sign-in - Asp.net 4.6.1


So I'm trying to sort out web-based authentication using the WSFederation protocal. We've sorted out the setup, and my web app can reach the login page, after some headache:

(Asp.net on-premises authentication - The remote certificate is invalid according to the validation procedure)

Now I'm getting a 'IDX10201: None of the the SecurityTokenHandlers could read the 'securityToken' error. From what I understand, we'll need middleware to deal with the security tokens. So I'm trying to get started with this:

https://www.scottbrady91.com/Katana/WS-Federation-Token-Encryption-using-Microsoft-Katana

So I've set the TokenValidationParameters option in the WsFederationAuthenticationOptions, but I'm getting an error from VisualStudio saying that 'Cert' does not exist in the current context. I'm confused as to why, as my code is nearly identical to the guides.

I'm also wondering if our certificate has simply been improperly configured. I came across some SSL guidelines for ADFS, and I know our IT guy hasn't gone down that road (yet). I'd like to rule that out as a possible cause, but if someone knows that it is, or is not, the cause, it'd save me time and be greatly appreciated.

EDIT: After some reading, there are some things that are unclear to me? We're using an ADFS server to handle the credentials, but as I understand it, ADFS should also handle our tokens without any additional work. Am I wrong? Should I be using middleware? Or is there a problem with the ADFS server configuration?

using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.WsFederation;
using Owin;
using System.Security.Cryptography.X509Certificates;
using System.Security;
using System.Net.Security;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.IdentityModel.Tokens;
using Microsoft.Owin;
using RCHHRATool;
using System.Net;
using System.IdentityModel.Selectors;

namespace RCHHRATool
{

    public partial class Startup
    {
        private static string realm = ConfigurationManager.AppSettings["ida:Wtrealm"];
        private static string adfsMetadata = ConfigurationManager.AppSettings["ida:ADFSMetadata"];
        private X509Certificate2 certificate;

        public void ConfigureAuth(IAppBuilder app)
        {
            Debug.WriteLine("Configure Auth Started");
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions {
                                                AuthenticationType = 
                                                    WsFederationAuthenticationDefaults.AuthenticationType });

            //System.Net.ServicePointManager.ServerCertificateValidationCallback.
            //ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCB;
            var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            foreach(X509Certificate2 cert in store.Certificates)
            {
                Debug.WriteLine(cert.Issuer);
                if (cert.Issuer.Equals("CN=xxxxx.xxxxx.com"))
                {
                    this.certificate = new X509Certificate2(cert);
                }

            }



            app.UseWsFederationAuthentication(
                new WsFederationAuthenticationOptions
                {
                    SignInAsAuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType,
                    Wtrealm = "https://localhost:44340",
                    Wreply = "http://localhost:56879",
                    MetadataAddress = adfsMetadata,
                    AuthenticationType = "adfs",
                    SecurityTokenHandlers = new SecurityTokenHandlerCollection
                    {
                        new X509SecurityTokenHandler
                        {
                            Configuration = new SecurityTokenHandlerConfiguration
                            {
                                IssuerTokenResolver = new X509CertificateStoreTokenResolver(StoreName.Root,
                                    StoreLocation.LocalMachine)
                            }
                        }
                    }
                    //},
                    //TokenValidationParameters = new TokenValidationParameters
                    //{
                    //    ValidAudience = "https://localhost:44340/",
                    //    ValidIssuer = "xxxxx.xxxxx.com",
                    //    IssuerSigningToken = new X509SecurityToken(this.certificate)
                    //}

                });  
        }  

Solution

  • Turns out that asp.net (framework 4.6.1) & ws-federation doesn't handle encrypted security tokens out of the box. I followed a great guide to resolve the token error. After some tuning (watch your certificate footprint, and make sure your certificates are in trusted root), I managed to get the authentication working.