本文介绍了网页API 2 preflight CORS为承载令牌的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用程序与AngularJS前端和网页API 2后端,它使用承载令牌进行身份验证。

I have a web-app with an AngularJS front-end and a Web Api 2 back-end, and it uses bearer-tokens for authentication.

一切都很好,在Firefox和放大器; IE浏览器,但浏览器,我最初的登录请求有时是pre-投放。

All is well in FireFox & IE, but with Chrome, my initial login request is SOMETIMES pre-flighted.

下面是从AngularJS服务电话:

Here's the call from the AngularJS service:

$ http.post(的http://本地主机:55483 /令牌,数据{标题:{内容类型:应用程序/ x-WWW- 。表单urlen codeD'}})成功(功能(响应){...});

$http.post(http://localhost:55483/token, data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) { ... });

在preflight请求被踢来踢去与允许的访问控制原产地的错误。

The preflight request gets kicked back with an "Allow-Access-Control-Origin" error.

不过,如果我再次点击登录按钮(从而重新发送上述要求),一切都很好。

However, if I click the Login button again (thereby re-sending the above request) all is well.

这是如何prevent /告警/处理这个任何想法?

Any idea on how to prevent/trap/handle this?

PS:我用的LOC

PS: I use the LOC

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

ApplicationOAuthProvider.cs 文件把CORS允许头的/令牌请求,这在IE,Firefox和有时在Chrome罚款。

in the ApplicationOAuthProvider.cs file to put the CORS allow-header on the /Token request, which works fine in IE, FireFox and sometimes in Chrome.

推荐答案

下面是花式评论:

The below is Fancy comment:

与帮助下LeftyX 06月29想通了这一点,从职位
  这LOC app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 来的
  第一行中的 ConfigureAuth Startup.Auth.cs 方式。
- 然后,
  删除此LOC
   context.OwinContext.Response.Headers.Add(访问控制允许来源,
  新的[] {*});
GrantResourceOwnerCredentials()的方法
   ApplicationOAuthProvide.cs

preflight CORS请求得到他们
  处理得当,然后实际的requet经过



感谢的人,你救我一整天。

因为这里面有多种家伙,我把你的意见来回答框其他人可以看到它。



我不想让投了这一点。请我的答案,而不是发表评论



谢谢


Thank man, you save my whole day.
Cause it happens for many guys, I bring your comment to answer box for other guys can see it.

I don't want to get vote up for this. Please comment on my answer instead

Thank you

这篇关于网页API 2 preflight CORS为承载令牌的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:16
查看更多