Ok, let's see what ValidateAntiforgeryTokenAttribute does (Reflector / ILSpy is your friend):
public void OnAuthorization (AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException ("filterContext");
}
string antiForgeryTokenName = AntiForgeryData.GetAntiForgeryTokenName (null);
string antiForgeryTokenName2 = AntiForgeryData.GetAntiForgeryTokenName (filterContext.HttpContext.Request.ApplicationPath);
HttpCookie httpCookie = filterContext.HttpContext.Request.Cookies [antiForgeryTokenName2];
if (httpCookie == null || string.IsNullOrEmpty (httpCookie.Value))
{
throw ValidateAntiForgeryTokenAttribute.CreateValidationException ();
}
AntiForgeryData antiForgeryData = this.Serializer.Deserialize (httpCookie.Value);
string text = filterContext.HttpContext.Request.Form [antiForgeryTokenName];
if (string.IsNullOrEmpty (text))
{
throw ValidateAntiForgeryTokenAttribute.CreateValidationException ();
}
AntiForgeryData antiForgeryData2 = this.Serializer.Deserialize (text);
if (! string.Equals (antiForgeryData.Value, antiForgeryData2.Value, StringComparison.Ordinal))
{
throw ValidateAntiForgeryTokenAttribute.CreateValidationException ();
}
string username = AntiForgeryData.GetUsername (filterContext.HttpContext.User);
if (! string.Equals (antiForgeryData2.Username, username, StringComparison.OrdinalIgnoreCase))
{
throw ValidateAntiForgeryTokenAttribute.CreateValidationException ();
}
if (! this.ValidateFormToken (antiForgeryData2))
{
throw ValidateAntiForgeryTokenAttribute.CreateValidationException ();
}
}
Well, obviously, the cookie name for the token is made from the application path:
string antiForgeryTokenName2 = AntiForgeryData.GetAntiForgeryTokenName (filterContext.HttpContext.Request.ApplicationPath);
HttpCookie httpCookie = filterContext.HttpContext.Request.Cookies [antiForgeryTokenName2];
So, you create your own filter, just copy-paste this code and change it to respect the port (or something else because you distinguish your applications):
string antiForgeryTokenName2 = AntiForgeryData.GetAntiForgeryTokenName (filterContext.HttpContext.Request.ApplicationPath + filterContext.HttpContext.Request.Url.Port);
HttpCookie httpCookie = filterContext.HttpContext.Request.Cookies [antiForgeryTokenName2];
Thus, the cookie name ("RequestVerificationToken_Lw") will also change by port.
And, of course, we cannot forget to change this cookie name while creating a token. Unfortunately, you need to copy-paste "repeat" 2 things here - first, the AntiForgeryToken extension method to call your own AntiForgeryWorker, and then AntiForgeryWorker itself - just override the GetAntiForgeryTokenAndSetCookie method, this is the same as before:
string antiForgeryTokenName = AntiForgeryData.GetAntiForgeryTokenName (httpContext.Request.ApplicationPath);
Well, that seems like a mess, and its definitely not a DRY solution, but if you really want it, you can do it in a few minutes. Just use a reflector and copy :)