zoukankan      html  css  js  c++  java
  • asp.net cors solution

    I have a simple actionmethod, that returns some json. It runs on ajax.example.com. I need to access this from another site someothersite.com.
    
    If I try to call it, I get the expected...:
    
    Origin http://someothersite.com is not allowed by Access-Control-Allow-Origin.
    I know of two ways to get around this: JSONP and creating a custom HttpHandler to set the header.
    
    Is there no simpler way?
    
    Is it not possible for a simple action to either define a list of allowed origins - or simple allow everyone? Maybe an action filter?
    
    Optimal would be...:
    
    return json(mydata, JsonBehaviour.IDontCareWhoAccessesMe);
    For plain ASP.NET MVC Controllers
    
    Create a new attribute
    
    public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            base.OnActionExecuting(filterContext);
        }
    }
    
    Tag your action:
    
    [AllowCrossSiteJson]
    public ActionResult YourMethod()
    {
        return Json("Works better?");
    }
    
    For ASP.NET Web API
    
    using System;
    using System.Web.Http.Filters;
    
    public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Response != null)
                actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
    
            base.OnActionExecuted(actionExecutedContext);
        }
    }
    
    Tag a whole API controller:
    
    [AllowCrossSiteJson]
    public class ValuesController : ApiController
    {
    
    Or individual API calls:
    
    [AllowCrossSiteJson]
    public IEnumerable<PartViewModel> Get()
    {
        ...
    }
    
    If you are using IIS 7+, you can place a web.config file into the root of the folder with this in the system.webServer section:
    
    <httpProtocol>
       <customHeaders>
          <clear />
          <add name="Access-Control-Allow-Origin" value="*" />
       </customHeaders>
    </httpProtocol>
    See: http://msdn.microsoft.com/en-us/library/ms178685.aspx And: http://enable-cors.org/#how-iis7
    
    
    This is really simple , just add this in web.config
    
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="http://localhost" />
          <add name="Access-Control-Allow-Headers" value="X-AspNet-Version,X-Powered-By,Date,Server,Accept,Accept-Encoding,Accept-Language,Cache-Control,Connection,Content-Length,Content-Type,Host,Origin,Pragma,Referer,User-Agent" />
          <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
          <add name="Access-Control-Max-Age" value="1000" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
    In Origin put all domains that have access to your web server, in headers put all possible headers that any ajax http request can use, in methods put all methods that you allow on your server
    
    regards :)
  • 相关阅读:
    1月10日 TextView
    1月9日 布局2
    30 Adapter适配器
    29 个人通讯录列表(一)
    28 ListView控件
    27 登录模板
    26 Activity的启动模式
    25 Activity的生命周期
    24 得到Activity返回的数据
    23 Activity的传值2(bundle)
  • 原文地址:https://www.cnblogs.com/zengpeng/p/8145603.html
Copyright © 2011-2022 走看看