GVKun编程网logo

CORS $ .ajax会话cookie(访问控制允许证书和withCredentials = true)(http控制访问cors)

26

对于想了解CORS$.ajax会话cookie的读者,本文将是一篇不可错过的文章,我们将详细介绍访问控制允许证书和withCredentials=true,并且为您提供关于ajax–IOS9WKWeb

对于想了解CORS $ .ajax会话cookie的读者,本文将是一篇不可错过的文章,我们将详细介绍访问控制允许证书和withCredentials = true,并且为您提供关于ajax – IOS9 WKWebView访问控制允许源、AngularJS withCredentials、ASP.NET Core 2.0 Identity中的Cookies.ApplicationCookie.AutomaticChallenge = false的替代是什么?、ASP.NET Core 3.1 _CookieConsentPartial.cshtml 接受 cookie的有价值信息。

本文目录一览:

CORS $ .ajax会话cookie(访问控制允许证书和withCredentials = true)(http控制访问cors)

CORS $ .ajax会话cookie(访问控制允许证书和withCredentials = true)(http控制访问cors)

我知道这个问题已经被问了十二遍或更多遍了,给出的每一个回答都表明我做对了,但也许我遗漏了一些东西。

AJAX可以像这样满足CORS请求…

$.ajax({url: ''someotherdomain.com'',type: ''post'',data: {key: ''value''},dataType: ''json'',async: false,crossDomain: true,beforeSend: function(xhr){    xhr.withCredentials = true;},success: function(x, status, xhr){},error: function(xhr, status, error){}});

PHP可以像这样满足CORS请求…

header(''Access-Control-Max-Age: 1728000'');header(''Access-Control-Allow-Origin: http://someotherdomain.com'');header(''Access-Control-Allow-Methods: POST'');header(''Access-Control-Allow-Headers: Content-MD5, X-Alt-Referer'');header(''Access-Control-Allow-Credentials: true'');header("Content-Type: application/json; charset=utf-8");

根据所有文档,只要设置了“ Access-Control-Allow-Credentials”服务器端标头和“ withCredentials =
true”客户端标头,域之间的会话cookie处理就应该透明。我想念什么吗?

答案1

小编典典
async: false

阻止了每次请求都将会话cookie发送回服务器。下面修复了它。

async: true

尽管这样做确实允许浏览器在进行跨源请求共享调用时设置会话cookie,但我现在遇到以下情况的问题:

服务器A使用CORS向客户端发送响应客户端向服务器B发出请求

XMLHttpRequest -> PHP -> Session handler -> MySQL -> Stored Procedure

由于PHP会话管理中的MUTEX锁具有异步特性,因此显然需要强制使用其他标头选项(例如XCookie)或类似方法来手动设置cookie,以保持服务器会话和客户端请求同步。

这种特殊的解决方法不太适合我,因为我相信它将为会话劫持和会话重播攻击媒介开辟一条轻松的通道。

使用SSL / TLS包装的连接可能有助于防止上述情况,但是就为客户端独立提供安全措施而言,我认为这不足够。

有人对此有任何想法吗?

ajax – IOS9 WKWebView访问控制允许源

ajax – IOS9 WKWebView访问控制允许源

我使用loadFileURL在IOS WKWebview上加载本地html文件:allowedReadAccesstoURL ..
但是当我发送ajax请求时,它失败。

错误是:

Origin null is not allowed by Access-Control-Allow-Origin.

我可以设置一些属性来解决这个错误,像在Android中,我们可以使用setAllowUniversalAccessFromFileURLs吗?

尝试使用loadRequest而不是loadFileURL。它为我工作:
Nsstring* path = [[NSBundle mainBundle] pathForResource:@"pageName" ofType:@"html"];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
[self.webView loadRequest:request];

之后,我的查询工作正常,而之前我有一个’不能请求从null’错误。

AngularJS withCredentials

AngularJS withCredentials

我一直在一个AngularJS项目,必须发送AJAX调用到restfull webservice。这个webservice是在另一个域上,所以我不得不在服务器上启用cors。我通过设置这些标题:
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin","http://localhost:8000");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials","true");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods","GET,POST,DELETE,PUT");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers","Content-Type,Accept,X-Requested-With");

我可以发送AJAX请求从AngularJS到后端,但我面临一个问题,当我尝试获取一个会话的属性。我相信这是因为sessionid cookie没有发送到后端。

我能够解决这个在jQuery中通过设置withCredentials为true。

$("#login").click(function() {
    $.ajax({
        url: "http://localhost:8080/api/login",data : '{"identifier" : "admin","password" : "admin"}',contentType : 'application/json',type : 'POST',xhrFields: {
            withCredentials: true
        },success: function(data) {
            console.log(data);
        },error: function(data) {
            console.log(data);
        }
    })
});

$("#check").click(function() {
    $.ajax({
        url: "http://localhost:8080/api/ping",method: "GET",success: function(data) {
            console.log(data);
        }
    })
});

我面临的问题是,我不能让这个工作在AngularJS与$ http服务。我试着这样:

$http.post("http://localhost:8080/api/login",$scope.credentials,{withCredentials : true}).
            success(function(data) {
                $location.path('/');
                console.log(data);
            }).
            error(function(data,error) {
                console.log(error);
            });

任何人都可以告诉我我做错了什么?

你应该传递一个配置对象,像这样
$http.post(url,{withCredentials: true,...})

或在旧版本:

$http({withCredentials: true,...}).post(...)

参见your other question。

ASP.NET Core 2.0 Identity中的Cookies.ApplicationCookie.AutomaticChallenge = false的替代是什么?

ASP.NET Core 2.0 Identity中的Cookies.ApplicationCookie.AutomaticChallenge = false的替代是什么?

我从ASP.NET Core 1.1升级到2.0,现在有401个未经授权的响应被更改为302重定向响应.这在以前是我在1.1中的一个问题,并通过以下代码进行了缓解:

services.AddIdentity<User,IdentityRole>(identityOptions =>
{
    identityOptions.Cookies.ApplicationCookie.AutomaticChallenge = false;
})

但是,identityOptions上不再有Cookies属性.

我也试过添加以下内容(并且还注意到我以前在我的应用中不需要这种扩展方法):

services.AddCookieAuthentication(cookieAuthenticationoptions => {
    cookieAuthenticationoptions.LoginPath = ""; // also tried null
    cookieAuthenticationoptions.AccessDeniedpath = ""; // also tried null
    cookieAuthenticationoptions.logoutPath = ""; // also tried null
});

该代码似乎对默认重定向路径或行为没有影响.如何在Core 2.0中阻止这些重定向?

解决方法

如 https://github.com/aspnet/Announcements/issues/262中所述,您现在必须使用services.AddAuthentication()扩展在全局级别配置默认方案处理程序.

为了防止Identity注册的cookie处理程序处理挑战,将DefaultChallengeScheme替换为对应于不同处理程序的方案(例如JWT承载处理程序).

services.AddIdentity<User,IdentityRole>();

services.AddAuthentication(options =>
{
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
});

如果 – 无论出于何种原因 – 选择不同的处理程序不是您的选择,那么您将必须使用services.ConfigureApplicationCookie()来注册自定义CookieAuthenticationEvents.(On)RedirectToLogin事件以更改Identity返回“未经授权的响应”的方式”.

这是返回401响应的示例:

services.ConfigureApplicationCookie(options =>
{
    options.Events.OnRedirectToLogin = context =>
    {
        context.Response.StatusCode = 401;

        return Task.CompletedTask;
    };
});

ASP.NET Core 3.1 _CookieConsentPartial.cshtml 接受 cookie

快速更新我的进度以分享解决方案,以防其他人被此绊倒

发布到 IIS 的网站必须具有 SSL 绑定/在使用网站时必须强制使用 https://。

我安装了一个证书和设置重定向规则,嘿,presto.... 同意 Cookie 已创建!万岁!

干杯,

罗布

我们今天的关于CORS $ .ajax会话cookie访问控制允许证书和withCredentials = true的分享就到这里,谢谢您的阅读,如果想了解更多关于ajax – IOS9 WKWebView访问控制允许源、AngularJS withCredentials、ASP.NET Core 2.0 Identity中的Cookies.ApplicationCookie.AutomaticChallenge = false的替代是什么?、ASP.NET Core 3.1 _CookieConsentPartial.cshtml 接受 cookie的相关信息,可以在本站进行搜索。

本文标签: