Angular WithCredentials 不适用于 POST、PUT

     2023-03-31     34

关键词:

【中文标题】Angular WithCredentials 不适用于 POST、PUT【英文标题】:Angular WithCredentials not working for POST, PUT 【发布时间】:2018-05-02 09:02:19 【问题描述】:

我正在开发一个 Angular 网站:

• localhost/test 上的 .Net Framework 服务器

• 本地主机上的客户端:4200

我的 cookie 身份验证工作正常,我的 GET 请求工作正常。

   this.httpClient.get<Test>('dummyUrl',  withCredentials: true );

在我的服务器的 web.config 中,我有以下内容。

    <add name="Access-Control-Allow-Credentials" value="true"/>
    <add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
    <add name="Access-Control-Allow-Methods" value="*" />

鉴于此设置,我希望 PUT 和 POST 请求同样适用。

    this.httpClient.put('dummyUrl', this.payload,  withCredentials: true );

我从这里得到了 401 Unauthorized。


我的调查

我看到的请求方法是 OPTIONS,它告诉我这在飞行前失败了,并且这个问题与 CORS 而不是 Angular 有关。我一直无法弄清楚我错过了什么。

如果您有任何指导,我将不胜感激。

【问题讨论】:

你能解决问题的地方,我面临的问题完全一样 【参考方案1】:

如果您在前端使用带角度的asp net core api,那么在web api中,启动设置(在configure方法下),

设置以下设置

        app.UseCors(options =>
        
            options.SetIsOriginAllowed(origin => true);
            //options.AllowAnyOrigin();
            options.AllowAnyHeader();
            options.AllowAnyMethod();
            options.AllowCredentials();   <-- This one is important
        );

它应该可以工作。

【讨论】:

***.com/questions/61279426/… options.AllowCredentials() 是 http post 工作所必需的,它修复了 CORS 问题【参考方案2】:

试试下面的代码。

import  RequestOptions, Headers  from '@angular/http';

let headers = new Headers( 'Content-Type': 'application/json' );
let options = new RequestOptions( headers: headers, withCredentials: true );

this.httpClient.put('dummyUrl', this.payload, options);

【讨论】:

感谢您的回答。 RequestOptions 已被弃用,所以我觉得我应该避免它。为了调试,我还是尝试了它并得到了相同的 401 行为。【参考方案3】:

我也遇到了类似的问题,即使在我的 POST 请求中使用withCredentials: true

在处理 CORS 的 Spring Boot 服务器代码中,我将 Access-Control-Allow-Origin 标头设置为 *。我将 * 更改为 http://localhost:4200,我的 Angular 应用程序就是从那里提供服务的。

之后一切正常。希望这可以帮助某人。

【讨论】:

Angular 4 - 在每个请求上设置 withCredentials - cors cookie

】Angular4-在每个请求上设置withCredentials-corscookie【英文标题】:Angular4-settingwithCredentialsoneveryrequest-corscookie【发布时间】:2018-04-2812:44:19【问题描述】:我的Angular客户端与后端分离,并且我在后端启用了cors,一切正常,除了我的... 查看详情

Angular WithCredentials 不适用于 POST、PUT

】AngularWithCredentials不适用于POST、PUT【英文标题】:AngularWithCredentialsnotworkingforPOST,PUT【发布时间】:2018-05-0209:02:19【问题描述】:我正在开发一个Angular网站:•localhost/test上的.NetFramework服务器•本地主机上的客户端:4200我的cook... 查看详情

在不同端口上运行 localhost 调用 dotnet 应用程序的 Angular 应用程序在降级时获得无推荐人。由于 withCredentials?

...et应用程序的Angular应用程序在降级时获得无推荐人。由于withCredentials?【英文标题】:Angularapprunninglocalhostcallingdotnetappondifferentportgetsno-referrer-when-downgrade.DuetowithCredentials?【发布时间】:2020-07-2419:16:13【问题描述】:我 查看详情

AngularJS withCredentials未发送

】AngularJSwithCredentials未发送【英文标题】:AngularJSwithCredentialsNotSending【发布时间】:2014-04-0117:53:56【问题描述】:在AngularJS中,我在子域中有我的RestfulAPI,但我遇到了cookie/会话没有跨域共享的问题。对于Angular,我正在这样做... 查看详情

为 withCredentials 指定 CORS 标头:true

】为withCredentials指定CORS标头:true【英文标题】:SpecifyingCORSheadersforwithCredentials:true【发布时间】:2018-10-1900:04:37【问题描述】:我需要指定CORS标头,因为我需要在每个请求中发送cookie密钥。我在不同的服务器上有一个带有JavaSpr... 查看详情

Ajax POST - 不推荐用于同步请求的“XMLHttpRequest.withCredentials”

】AjaxPOST-不推荐用于同步请求的“XMLHttpRequest.withCredentials”【英文标题】:AjaxPOST-\'XMLHttpRequest.withCredentials\'forsynchronousrequestsisdeprecated【发布时间】:2015-08-1514:39:00【问题描述】:我想做的事情应该很简单。我正在尝试将一些表... 查看详情

预检标题不符合预期

...。我的意思是,只有有限的成功。当我将Angular请求中的withCredentials设置为true时,Preflight标头试图查看API是否会返回正确的标头,包括Access-C 查看详情

带有 withCredentials 的 Ajax 调用:true 不是在请求中发送 cookie

】带有withCredentials的Ajax调用:true不是在请求中发送cookie【英文标题】:AjaxcallwithwithCredentials:trueisnotsendingcookieinrequest【发布时间】:2019-04-0219:18:44【问题描述】:我的场景:example.com是用户正在访问的django页面。会话由服务器设... 查看详情

CORS withCredentials - 支持有限?

】CORSwithCredentials-支持有限?【英文标题】:CORSwithCredentials-supportlimited?【发布时间】:2013-11-0902:45:34【问题描述】:我注意到某些浏览器似乎不支持CORS请求中的withCredentials,至少在某些情况下是这样。具体来说,在IE10中,尝试... 查看详情

角度 4 不发送 cookie 到服务器

...ver【发布时间】:2018-06-1123:53:12【问题描述】:当我使用withCredentials时,我可以看到cookie被发送到服务器。但是将任何内容发送到服务器。我使用laravel作为后端,使用angular4作为前端。此外,chrome开发者工具上没有显示cookie,我... 查看详情

使用 HttpClient 和 RXJS 将数据发布到 Web API withCredentials: true

】使用HttpClient和RXJS将数据发布到WebAPIwithCredentials:true【英文标题】:UsingHttpClientandRXJStoPOSTdatatoWebAPIwithCredentials:true【发布时间】:2018-07-2421:58:35【问题描述】:我有一个Angular服务已连接到我的.NETWebAPI,我正在尝试将POST一些数... 查看详情

跨域请求设置withcredentials(转)

  最近在做运动城项目,这一个项目下面有多个子项目,如主数据项目,pos项目等。主数据项目的域名为www.topmall.com,POS项目的域名为pos.topmall.com。即两个项目的主域名相同,子域名不相同。  我们的登陆认证是放在主数据... 查看详情

CORS withCredentials XHR 预检未在 Firefox 中发布 Cookie

】CORSwithCredentialsXHR预检未在Firefox中发布Cookie【英文标题】:CORSwithCredentialsXHRpreflightnotpostingCookiesinFirefox【发布时间】:2012-04-2509:14:03【问题描述】:我正在尝试发布带有凭据的CORSXHR帖子。它在Chrome中运行良好,但在Firefox中则... 查看详情

Internet Explorer 10 忽略 XMLHttpRequest 'xhr.withCredentials = true'

】InternetExplorer10忽略XMLHttpRequest\\\'xhr.withCredentials=true\\\'【英文标题】:InternetExplorer10isignoringXMLHttpRequest\'xhr.withCredentials=true\'InternetExplorer10忽略XMLHttpRequest\'xhr.withCredentials=true\'【发布时间】:2012-09 查看详情

OkHttp POST 请求设置 withCredentials 为 True?

】OkHttpPOST请求设置withCredentials为True?【英文标题】:OkHttpPOSTrequestsetwithCredentialstoTrue?【发布时间】:2019-12-0400:22:15【问题描述】:无法通过DjangoRestFramework令牌授权,因为我无法使用OkHttpRequestBuilder设置withCredentials=true。(我指... 查看详情

withcredentials--相同主域跨域解决方法

主域相同,而子域不同,存在跨域问题。在高版本浏览器下,可以设置withCredentials来解决。  xhrFields:{withCredentials: true}  低版本浏览器标准IE6、IE7、IE8、IE9或者document.documentMode<10 查看详情

如何将 withCredentials 选项传递给 Request?

】如何将withCredentials选项传递给Request?【英文标题】:HowtopasswithCredentialsoptiontoRequest?【发布时间】:2018-11-2907:57:57【问题描述】:根据CORSspec,withCredentials:true可以在XHR或Fetch请求中设置,以在飞行前请求中包含授权凭据。我正... 查看详情

尽管 withCredentials:true 未能在 GET 请求中发送 Cookie

】尽管withCredentials:true未能在GET请求中发送Cookie【英文标题】:FailingtosendaCookieinGETRequestdespitewithCredentials:true尽管withCredentials:true未能在GET请求中发送Cookie【发布时间】:2020-05-2409:51:43【问题描述】:我打算使用SpringBootRESTAPI、JWT(... 查看详情