2016-10-17 17 views
8

Tôi đang cố gắng tạo một ứng dụng web hoạt động với các yêu cầu cross-origin (CORS) trong MVC 5. Tôi đã thử tất cả mọi thứ mà không có bất kỳ kết quả nào.Làm thế nào để kích hoạt yêu cầu xuất xứ chéo trong ASP.NET MVC

Với một thuộc tính

public class AllowCrossSiteJsonAttribute: ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
      filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); 

     base.OnActionExecuting(filterContext); 
    } 
} 

Với thuộc tính EnableCors

[EnableCors("*")] 

Không có gì làm tôi bắt đầu nghĩ rằng nó là không thể

+1

Các bạn đã đọc mẫu: https://www.asp.net/web-api/overview/security/enabling-cross- origin-requests-in-web-api –

Trả lời

3

Để kích hoạt yêu cầu cross-xứ, thêm thuộc tính [EnableCors] để điều khiển Web API của bạn hoặc phương pháp điều khiển:

[EnableCors(origins: "http://systematixindia.com", headers: "*", methods: "*")] 


public class TestController : ApiController 
    { 
     // Controller method`enter code here`s not shown... 
    } 

Read More

+0

Giải pháp của bạn dành cho Web Api, đọc tiêu đề câu hỏi! Nó không hoạt động với MVC. –

0

CORS Kích hoạt trong MVC 5 (lõi) trước tiên cần thêm gói Microsoft.AspNetCore.Cors vào dự án của bạn. Sau đó cấu hình startup.cs như thế này cho tất cả các trang web

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddMvc(); 
    //Add Cors support to the service 
    services.AddCors(); 

    var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy(); 

    policy.Headers.Add("*");  
    policy.Methods.Add("*");   
    policy.Origins.Add("*"); 
    policy.SupportsCredentials = true; 

    services.ConfigureCors(x=>x.AddPolicy("AllPolicy", policy)); 

} 


public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    // Configure the HTTP request pipeline. 

    app.UseStaticFiles(); 
    //Use the new policy globally 
    app.UseCors("AllPolicy"); 
    // Add MVC to the request pipeline. 
    app.UseMvc(); 
} 

và sau đó bạn cũng có thể sử dụng như thế này

[EnableCors("AllPolicy")] 

Chi tiết here

+0

Lỗi: EnableCors không lấy nguồn gốc tham số –

+0

phiên bản asp.net mvc nào bạn đang sử dụng? – Mostafiz

+0

Tôi đang sử dụng MVC 5 –

8

Thêm các thiết lập cấu hình trong file web.config của bạn để thiết lập giá trị cho Access-Control-Allow-Origin trong customHeaders như thế này -

<configuration> 
<system.webServer> 
    <httpProtocol> 
    <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
    </httpProtocol> 
</system.webServer> 
</configuration> 

Bạn muốn truy cập thisthis để biết thêm chi tiết và một số tùy chọn khác.

0

Tôi nghĩ bạn phải thêm nó vào bước "OnAuthentication" hoặc thêm cấu hình vào cấu hình web của bạn.Bạn có thể thử mã của tôi :) nó hoạt động

public class AllowCrossSiteJsonAttribute : ActionFilterAttribute, IAuthorizationFilter 
    { 
     public void OnAuthorization(AuthorizationContext filterContext) 
     { 
      filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
     } 

    } 
14

Những gì tôi suy nghĩ thuận tiện nhất là tạo lớp học của riêng bạn như sau:

enter image description here

với đoạn mã sau vào nó:

using System; 
using System.Web.Mvc; 

public class AllowCrossSiteAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4200"); 
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "*"); 
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true"); 

     base.OnActionExecuting(filterContext); 
    } 
} 

Sau này nó cho phép bạn sử dụng trang trí này trên một phương pháp hoặc trên toàn bộ điều khiển

enter image description here enter image description here

Bạn nên có thể thấy điều đó trong tiêu đề phản hồi sau khi bắt đầu dure

enter image description here

Thank để này response

0

Tôi đã thành công bằng cách sử dụng thực hiện OWIN CORS (Microsoft.Owin.Cors NuGet) để kích hoạt CORS cho Controller MVC và Owin middleware, ngoài ApiControllers . Microsoft.AspNet.WebApi.Cors (sử dụng config.EnableCors() và thuộc tính [EnableCors]) dường như chỉ hoạt động với ApiControllers.

Xem http://benfoster.io/blog/aspnet-webapi-cors để biết mã mẫu.

0

Bạn cũng có thể sử dụng mã dưới đây để cho phép yêu cầu cross-nguồn gốc

public void ProcessRequest (HttpContext context) 
     { 
    context.Response.AddHeader("Access-Control-Allow-Origin" , "*"); 
} 
Các vấn đề liên quan