I recently had a case where I needed to programmatically determine which zone in my SharePoint 2010 web application had forms-based authentication enabled and then return the public/default URL of that zone.
The reason was a custom application page in SharePoint was sending out emails with links back to SharePoint, and in this case those links needed to always redirect users to the FBA-enabled zone so we send them through a custom login/registration process we’d configured on that zone. Administrators using this custom application page might be accessing the page from one of the non-FBA-enabled zones on the web application – hence the reason to programmatically determine the correct URL to include in the email.
Here’s the code I ended up using to find the URL:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
public static string GetFbaZoneUrl(SPWeb web)
{
string url = null;
foreach (var zone in webApp.IisSettings.Keys)
{
var settings = webApp.IisSettings[zone];
var fbaProvider = webApp.IisSettings[zone].FormsClaimsAuthenticationProvider;
if (fbaProvider != null)
{
// We found the zone on the web app where FBA is enabled.
// This is the zone whose URL we want.
SPAlternateUrl fbaUrl = webApp.AlternateUrls.GetResponseUrl(zone, false);
if (fbaUrl != null)
{
url = fbaUrl.IncomingUrl;
}
break;
}
}
if (url != null && !url.EndsWith("/"))
{
url += "/";
}
return url;
}
As you can see in the code, I break from the loop after finding the first FBA-enabled zone in my web application because I know there will never be more than one in my case. If for some reason you have multiple zones configured to use FBA, you could modify this code slightly to return a list of all FBA URLs or filter down to the one(s) you want by some criteria (such as the membership/role provider names). The AlternateUrls property of the SPWebApplication object allows me to grab the response URL (i.e. the default URL) for a given zone.