Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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
Javascript ASP.net标识的CORS问题_Javascript_C#_Asp.net_Angularjs - Fatal编程技术网

Javascript ASP.net标识的CORS问题

Javascript ASP.net标识的CORS问题,javascript,c#,asp.net,angularjs,Javascript,C#,Asp.net,Angularjs,我和我的一个朋友在angular.js项目中工作,我们遇到了一个特定的CORS跨源请求问题。服务器是一个Microsoft ASP.NET restful API,我将angular.js与Node.js一起使用 我们在服务器端启用了CORS,并且能够获得其他所有内容的响应,接受用户登录,我们正在使用ASP.NET标识。我们总是得到同样的错误,我将发布贝娄,以及张贴从客户端。所以基本上我的问题是,有人知道如何解决这个问题吗?谢谢 无法加载XMLHttpRequest。请求的资源上不存在“Acce

我和我的一个朋友在angular.js项目中工作,我们遇到了一个特定的CORS跨源请求问题。服务器是一个Microsoft ASP.NET restful API,我将angular.js与Node.js一起使用

我们在服务器端启用了CORS,并且能够获得其他所有内容的响应,接受用户登录,我们正在使用ASP.NET标识。我们总是得到同样的错误,我将发布贝娄,以及张贴从客户端。所以基本上我的问题是,有人知道如何解决这个问题吗?谢谢

无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“localhost”。响应的HTTP状态代码为400

function login(username, password) {
        var innerconfig = {
            url: baseUrl + "/api/v1/accounts/login",
            data: {
                username: username,
                password: password,
                grant_type: "password"
            },
            method: "POST",
            headers:
            {
                'Accept': 'text/json'
            }
        };
        return $http(innerconfig).then(onSuccess, requestFailed);
        function onSuccess(results) {
            if (results && results.data) {
                $rootScope.access_token = results.data.access_token;
                return results.data;
            }
            return null;
        }
    }

尝试在标题中设置内容类型,这可能会解决此问题


标题:{'Content Type':'application/x-www-form-urlencoded'}

这通常发生,因为为您提供令牌的应用程序在CORS启动之前启动。 修理它很容易。您只需要转到IdentityConfig.cs,其中有一个函数名为

public static ApplicationUserManager Create
  (IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
这将为令牌请求启用CORS。 但问题是,当我们这样做时,其他正常请求将开始抛出错误,因为我们已经两次授予了访问源*。一次在identiy,另一次在cors。 如果遇到此错误,请在刚粘贴的标识配置中的cors代码上使用此if语句

if(context.Request.ContentType == "text/plain")

谢谢你的回复。但这并没有解决问题
if(context.Request.ContentType == "text/plain")