As I mentioned in my previous post here that I will explain how to auto-redirect the home realm discovery page to an ADFS namespace (claims provider trust) based on client’s IP so here I am.
Let’s say you have many ADFS servers (claims providers trusts) linked to a central ADFS 4.0 server and you want to auto-redirect the user to a linked ADFS server login page based on user’s IP instead of letting the user to choose a respective ADFS server from the list on the home realm discovery page as explained in the below request flow diagram.
You can do so by doing some customization as mentioned below:
- Create a database of IP ranges mapped to ADFS namespaces
- Develop a Web API which returns the relevant ADFS namespace based on request IP
- Add custom code in onload.js file on the central ADFS 4.0 server to call the Web API and do the redirection
It is assumed that all the boxes including Central ADFS, linked ADFS, Web Server, SQL Server are setup. All the nitties and gritties are sorted out in terms of firewall rules, DNS lookups, SSL certificates. If not then you can get help from an infrastructure guy on that.
Lets perform the required action on SQL, Web and ADFS Server.
SQL Server
Perform the following actions on the SQL Server:
- Create a new database
- Create a new table called Registration as shown below
- Insert some records in the table for the linked ADFS server IP range, for example
Start IP: 172.31.117.1, End IP: 172.31.117.254, Redirect Name: http://adfs.adminlab.com/adfs/services/trust
Web Server
Perform the following actions for the Web API development and deployment:
- Create a new ASP.NET MVC Web API project using Visual Studio
- Create a new class called Redirect.cs as shown below (I would have used the same name as database table name ‘Registration’ but it’s OK for now)
- Insert a new Web API controller class called ResolverController.cs as shown below. What we are doing here is getting the request IP address and getting the IP ranges from the database, comparing the request IP address with the IP ranges from the database by converting both to long IP address. If the request IP is in range then returning the redirect object.
- Add a connection string in the web.config named DbConnectionString pointing to the database we created above.
- Deploy this web API project to the web server IIS
- Configure the HTTPS binding as well for this web API project using the SSL certificate
- Note down the URL of the web API, something like ‘https://{Web-Server-Web-API-URL}/api/resolver/get’, this will be used in the onload.js
Central ADFS 4.0 Server
Perform the following actions on the central ADFS 4.0 server:
- Run the following PowerShell command to export current theme to a location
Export-AdfsWebTheme -Name default -DirectoryPath D:\Themes\Custom
- Run the following PowerShell command to create a new custom theme based on current theme
New-AdfsWebTheme -Name custom -SourceName default
- Update onload.js file extracted in step 1 at D:\Themes\Custom\theme\script with following code added at the end of the file. What we are doing here is calling the web API which returns the matched Redirect object with RedirectName as ADFS namespace and setting the HRD.selection as that redirect name.
- Run the following PowerShell command to update back the onload.js file in the theme
Set-AdfsWebTheme -TargetName custom -AdditionalFileResource @{Uri=’/adfs/portal/script/onload.js’;path=”D:\Themes\Custom\theme\script\onload.js”}
- Run the following PowerShell command to make the custom theme as your default theme
Set-AdfsWebConfig -ActiveThemeName custom -HRDCookieEnabled $false
Now when you test from your linked ADFS server or a client machine linked to the linked ADFS server (which is linked to a central ADFS server), the auto-redirect kicks in from onload.js and forwards it to web API which gets the client IP and matches it with relevant ADFS where the request came from and redirects the user to the relevant ADFS login page, instead of user selecting the relevant ADFS namespace from the available list on home realm discovery page.
If the relevant match is not found, the default home realm discovery page with list of available ADFS namespaces is shown.