As indicated by the other answers, using referrer checks on its own is not sufficient and you really should be using anti-forgery tokens.
However, as @jeffsix has pointed out, you can use referrer checks as a Defence-in-Depth (DID) strategy, so an attacker would need to defeat multiple, independent, defenses to execute a successful attack.
The ValidateReferrerAttribute attribute below can be used on your HttpPost MVC actions. If the referrer is null, it does nothing. If the referrer is not null then it checks that it is equal to the specified host name. You just need to add it wherever you are using the ValidateAntiForgeryTokenAttribute already, so it would be very easy to add.
/// <summary>
/// For POST requests, checks that the requests referrer is the current site. This could be used along side the ValidateAntiForgeryToken
/// Note that many clients do not send the referrer, so we do nothing in this case.
/// This attribute can be used as part of a Defence-in-Depth (DID) strategy, so an
/// attacker would need to defeat multiple, independent, defenses to execute a successful attack.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class ValidateReferrerAttribute : FilterAttribute, IAuthorizationFilter
{
/// <summary>
/// Called when authorization is required.
/// </summary>
/// <param name="filterContext">The filter context.</param>
/// <exception cref="System.ArgumentNullException">filterContext</exception>
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if ((filterContext.HttpContext.Request.UrlReferrer != null) &&
string.Equals(filterContext.HttpContext.Request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(filterContext.HttpContext.Request.UrlReferrer.Host, filterContext.HttpContext.Request.Url.Host, StringComparison.OrdinalIgnoreCase))
{
this.HandleExternalPostRequest(filterContext);
}
}
/// <summary>
/// Handles post requests that are made from an external source.
/// By default a 403 Forbidden response is returned.
/// </summary>
/// <param name="filterContext">The filter context.</param>
/// <exception cref="System.Web.HttpException">Request not allowed.</exception>
protected virtual void HandleExternalPostRequest(AuthorizationContext filterContext)
{
throw new HttpException((int)HttpStatusCode.Forbidden, "Request not allowed.");
}
}