我已经建立了一个很好的 MVC4 应用程序,决定保护它,一切都很好。下面是我用来帮助防止 XSS 攻击的过滤器作为示例。
public class IsPostedFromThisSiteAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
//TODO: - Possible problems in future:
//1. Is there a way to handle the execptions and just display a friendly error page / message.
if (filterContext.HttpContext != null)
{
if (filterContext.HttpContext.Request.UrlReferrer == null)
throw new System.Web.HttpException("Invalid Submission");
//if (filterContext.HttpContext.Request.UrlReferrer.Host != "localhost/YeatsClinical.PatientPortal.Web")
if (filterContext.HttpContext.Request.UrlReferrer.AbsolutePath != ConfigurationManager.AppSettings["SiteURL"])
throw new System.Web.HttpException("This form was not submitted from this site!");
}
}
}
我的 MVC 应用程序安装了 Elmah,它也很好用。
问题是,我知道上面的错误,我不想显示异常消息,或者记录它或类似的东西(所有这些我都可以轻松完成)。相反,我想通过向我的用户显示一条漂亮的小消息来正确处理它。如何更改上面的代码以允许它显示带有漂亮小消息的视图或部分视图,或者只是在某处输出一些文本以让用户以一种不会导致应用程序崩溃的良好方式知道出了什么问题,也许在这种特殊情况下,将他返回到正确的登录屏幕。