我的架构中有三个应用程序。
它们位于同一服务器上,但具有不同的端口号。
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);
....
}
Config
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 :)
Regards