Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net CORS-响应为401时丢失允许源标题-未授权_C#_Ajax_Cors - Fatal编程技术网

C# .net CORS-响应为401时丢失允许源标题-未授权

C# .net CORS-响应为401时丢失允许源标题-未授权,c#,ajax,cors,C#,Ajax,Cors,我试图确定当响应未经授权时,为什么/如何丢失“Access Control Allow Origin”头 我正在使用基于令牌的身份验证,其中令牌有一个过期日期。当此令牌过期时,服务器返回401 unauthorized,这是它应该返回的,但在chrome(以及IE和FF)中,它从未看到Allow Origin标头和错误,通常出现CORS错误:XMLHttpRequest无法加载http://my.rest.service. 请求的资源上不存在“Access Control Allow Origi

我试图确定当响应未经授权时,为什么/如何丢失“Access Control Allow Origin”头

我正在使用基于令牌的身份验证,其中令牌有一个过期日期。当此令牌过期时,服务器返回401 unauthorized,这是它应该返回的,但在chrome(以及IE和FF)中,它从未看到Allow Origin标头和错误,通常出现CORS错误:
XMLHttpRequest无法加载http://my.rest.service. 请求的资源上不存在“Access Control Allow Origin”标头。起源'http://localhost因此,不允许访问

我不确定这些是否相关,因为auth逻辑工作正常,只是当响应为401时CORS会阻塞

C#CORS处理程序 这个调用的结果是我前面提到的CORS错误

据我所知,防火墙或中间件剥离这些内容并没有任何问题,因为任何其他非401ajax请求都可以正常执行


有没有想过为什么标题会消失?

我遇到了完全相同的问题,并通过在返回之前将CORS标题显式添加到401响应中解决了这个问题

var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Bearer", "errorMessage"));
response.Headers.Add(AccessControlAllowOrigin, "*");
return response;

AccessControlAllowOrigin位于哪个命名空间中?
function executeAjax (method, url, data, token) {

    url = (url.indexOf('/') === 0) ? url : "/" + url;

    var options = {
        method: method,
        url: app.settings.apiUrlRoot + url,
        data: data
    };

    token = token || localStorage.getItem("sessionKey");

    options.headers = {
        "Accept": "application/json",

        //header for enabling CORS
        "X-EnableCors": 'true'
    }

    if (token !== undefined && token !== null)
    {
        options.headers["X-ADAuth"] = token,
    }

    return $.ajax(options);
};
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Bearer", "errorMessage"));
response.Headers.Add(AccessControlAllowOrigin, "*");
return response;