如何在 WebAPI 2 中进行 CORS 身份验证? [英] How to make CORS Authentication in WebAPI 2?
问题描述
场景很简单,我需要从另一台服务器(不同于API服务器)登录来获取访问令牌.
The scenario is simple, I need to log in from another server (different from the API server) to retrieve the access token.
我在 API 服务器上安装了 Microsoft.Owin.Cors
包.在 Startup.Auth.cs
文件中,在 public void ConfigureAuth(IAppBuilder app)
下,我在
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
中,在public static void Register(HttpConfiguration config)
下,我添加了以下几行:
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);
我还应该改变什么?
推荐答案
看看我发现了什么!
在
中添加一些自定义标头.
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.
这篇关于如何在 WebAPI 2 中进行 CORS 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!