【发布时间】:2015-09-26 05:23:45
【问题描述】:
我的架构中有三个应用程序。
它们在同一台服务器上,但具有不同的端口号。
A - Token Application (port 4444) - Asp.net WebApi
B - API Application (port 3333) - Asp.net WebApi
C - UI Application (port 2222) - AngularJS App.
申请流程如下
1- UI 项目从令牌应用程序获取令牌(它需要 Windows 身份验证。)
例如:awxrsdsaWeffs12da
2- UI 应用程序将此令牌放入名为“accessToken”的自定义标头中
例如:accessToken:awxrsdsaWeffs12da
3- UI 应用程序向 API 应用程序发送请求
例如:http:myaddress:3333/api/TheRestServiceHere
UI 应用程序出现 401 错误。 它发送 OPTIONS 方法。 (我猜是预检问题)
在我的 web api 项目中,我启用了如下所示的 Cors。
public static void Register(HttpConfiguration config)
{
....
//CORS
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
....
}
配置
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//CORS
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
json.SerializerSettings.Formatting = Formatting.None;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
所以我正在寻找调用 API 应用程序 (B) 控制器的解决方案 并获得 200 :)
问候
【问题讨论】:
-
你能用你的 web api“路由配置”更新你的问题吗?
-
从浏览器调用WebAPI时能找到JSON结果吗?
-
是的,我可以通过邮递员和浏览器拨打电话。
标签: c# angularjs asp.net-web-api http-headers cors