带有MVC5,Webapi和Angular Js的Cors [英] Cors with MVC5, Webapi and Angular Js

查看:78
本文介绍了带有MVC5,Webapi和Angular Js的Cors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过两个项目测试一个简单的Visual Studio解决方案:

I'm testing a simple Visual studio solution with two project:

  • 项目A:Web项目中具有相同的角度代码,即用于登录用户的登录名/功能(发布到项目B的帐户api).帐户Api根据用户/名称给我令牌.

  • project A: web project where there same angular code, i.e login/function to autenticate user (post to account api to project B). Account Api give me token based on user/name.

项目B:Web api,在那里我可以获取相同的简单数据(http GET),并且在相同的帐户api中(即集成的api/token,基于凭据为我提供身份验证令牌).

project B: web api where I can get same simple data (http GET), and where there are same account api (i.e. integrated api/token that give me auth token based on credential).

这两个项目都托管在同一台服务器上,但名称和端口不同: http://web.project.com:1100

Either project are hosted on the same server but with different name and port: http://web.project.com:1100 and http://webapi.project.com:1101 (common domain project.com).

我的问题与CORS有关:当尝试使用project B从projectA登录用户时,我收到

My problem is about CORS: when try to login an user from projectA with project B I receive

缺少访问控制允许来源

Access-Control-Allow-Origin missing

(在浏览器控制台中),来自wepabpi.project.com:1101.

(in browser console ) from wepabpi.project.com:1101.

这是因为域托管在不同的域中.

This is because domain are hosted in different domain.

我安装了此软件包:

Install-Package Microsoft.AspNet.WebApi.Cors 

并将这一行添加到webapiconfig.cs(注册方法)

and I add this line in webapiconfig.cs (register method)

config.enableCors();

,然后将此属性添加到我的api控制器中:

and I add this attribute in my api controller:

[EnableCors(来源:" http://web.project.com:1100 ",标头:"",方法:"")]

[EnableCors(origins: "http://web.project.com:1100", headers: "", methods: "")]

第一个问题:Webproject使用angularjs http调用发送请求.在我的浏览器中,与Web项目联系以 http://web.project.com:1100 页面调用Web api,但来源为 http://web.project.com:1100 或我的浏览器来源?我不知道网络服务器是否使用浏览器的原点来调用此原点或角度.我不知道角度代码是由Web服务器还是浏览器运行.

First question: Webproject send request by using angularjs http call. When from my browser contact the webproject to http://web.project.com:1100 the page call web api, but the origin is http://web.project.com:1100 or my browser origin? I don't understand if webserver make call with this origin or angular using the browser origin. I don't understand if angular code are running by webserver or browser.

第二个问题:还将这一行添加到控制器和启用的cors中,我得到了相同的错误.我想令牌生成需要相同的额外配置,但是在哪里?这是因为标准令牌生成不是特定的控制器.

Second question: also add this line to controller and enabled cors, I get the same error. I suppose that token generation need same extra configuration, but where? This because standar token generation is not a specific controller.

谢谢

推荐答案

您将在您的angularjs服务或工厂中添加标头 因为这是两个不同的项目.并且第一个项目代码仅包含本地服务器.第二个项目代码包含远程位置,因此您将添加远程位置ip地址和头文件

you will add header in your angularjs service or factory because both are two different projects. and first project code contains local server only.second project code contains remote location so you will add remote location ip address and header file

 this.getloggedInUserData = function() {
    var defer = $q.defer();
    var loggedInUserPromise =http({
    url: "http://192.168.1.8:8080/ABCD1.5/getloggedFarmerData",
    method: "GET",
    headers: {
    'Accept': 'application/json',   'Content-Type': 'application/json'
    }
    });

这篇关于带有MVC5,Webapi和Angular Js的Cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆