如何使CORS在验证2的WebAPI? [英] How to make CORS Authentication in WebAPI 2?

查看:87
本文介绍了如何使CORS在验证2的WebAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该场景很简单,我需要从另一台服务器(从API​​服务器不同)登录检索访问令牌。

The scenario is simple, I need to log in from another server (different from the API server) to retrieve the access token.

我装 Microsoft.Owin.Cors 封装API服务器上。在 Startup.Auth.cs 文件,在公共无效ConfigureAuth(IAppBuilder应用),我

I installed Microsoft.Owin.Cors package on the API Server. In Startup.Auth.cs file, under public void ConfigureAuth(IAppBuilder app), I added in

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

WebApiConfig.cs ,在公共静态无效的注册(HttpConfiguration配置),我在这行补充说:

In WebApiConfig.cs, under public static void Register(HttpConfiguration config), I added in these lines:

// Cors
var cors = new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS");
config.EnableCors(cors);

还有什么我应该改变?

What else should I change?

推荐答案

看看我发现!

添加一些自定义标题里面的< system.webServer方式>

Add in some custom headers inside <system.webServer>.

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
  </customHeaders>
</httpProtocol>

然后我可以做CORS认证。

Then I can do the CORS authentication.

这篇关于如何使CORS在验证2的WebAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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