P3P Header Info in MVC

ぃ、小莉子 提交于 2019-12-18 16:48:14

问题


I'm not sure where I'm suppose to put this in my Asp.net MVC website:

HttpContext.Current.Response.AppendHeader("P3P", "CP=\\\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\\\"");

I put it in the:

public static void RegisterRoutes(RouteCollection routes)
{
  HttpContext.Current.Response.AppendHeader("P3P", "CP=\\\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\\\"");
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
      "Default", // Route name
      "{controller}/{action}/{id}", // URL with parameters
      new { controller = "Account", action = "Logon", id = UrlParameter.Optional }
  );

}

But I get back

Response is not available in this context.

Anyone know where I am suppose to put this?


回答1:


You can put it in the web.config:

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="P3P" value='CP="CAO PSA OUR"'/>

This way you do not need to put it in the code.

See this SO answer for details on what the value means.




回答2:


Assuming you want this header on every response, something like this should do it

public class P3PHeaderAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.AppendHeader("P3P", "CP=\\\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\\\"");

    }
}

then add the filter to the global collection

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new P3PHeaderAttribute());
    }



回答3:


You should create a class that inherits ActionFilter and overrides OnResultExecuting() to add that header.

Then, add it to the global filters collection.



来源:https://stackoverflow.com/questions/13975777/p3p-header-info-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!