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 :)
  • 相关阅读:
    高中教材记录
    帮我解决逆序问题的网友:@18数院啦啦啦。恶人:16 师大 很菜 刘琳(2196879114) 2:32:49 PM
    丘维声的视频地址,全部课堂
    理工医疗报销电话
    可汗化学和二项式定理的地址
    二次函数问题
    *0000181894BD*---------北大医院条形码
    三月里的小雨
    语文容易读错的字
    mongodb部署
  • 原文地址:https://www.cnblogs.com/zengpeng/p/8145603.html
Copyright © 2011-2022 走看看