' Access-Control-Allow-Origin'标头包含多个值' ',' *'

时间:2016-12-23 18:56:11

标签: asp.net-web-api

我正在使用ASP.NET WEB Api,并且我使用身份验证令牌,Internet Explorer生成令牌字符串:

enter image description here

但是使用任何其他浏览器都会显示以下错误:

  

' Access-Control-Allow-Origin'标头包含多个值

enter image description here

AJAX代码:

function ObtenerToken(user, password) {

            var data = { username: user, password: password, grant_type: "password" }

            $.ajax({
                url: 'http://localhost:50196/Token',
                method: "post",
                data: data,
                contentType: "application/json",
                error: function (e) {
                    alert('Hubo un error al intentar autenticar al usuario.');
                    console.log(e);
                },
                success: function (response) {
                    var token = response.access_token;
                    alert(token);

                }
            });
        }

文件Startup.Auth.cs我设置了以下内容

app.UseCors(CorsOptions.AllowAll);

在WebApiConfig.cs

EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);

在班级控制器中:

[EnableCors(origins: "*", headers: "*", methods: "*")]
    public class UsuariosController : ApiController
    {

2 个答案:

答案 0 :(得分:0)

我很确定这是因为你在所有这三个地方添加了Access-Control-Allow-Origin标头。您应该在Startup.Auth.cs中,在WebApiConfig.cs中或在控制器中启用CORS - 其中一个地方,而不是全部三个。

答案 1 :(得分:0)

你需要启用角色。

// Settings
error_reporting(E_ALL);
ini_set('max_execution_time', 0);
require_once ('classes/class.multicurl.php');

set_time_limit(0);
// Short Delay
$delay = rand(2, 4);
// Long Delay
$longdelay = rand(4, 7);
// Checking Proxies
$fileName = "leeched/proxies.txt";
// where to save successful proxies
$success = "goodproxies/success.txt";
$source = file('sources/sources.txt');
// SET Cookie
$c = new Curl;
foreach($source as $sources) {
  // Request To Delete Duplicate Proxies
  $c->addRequest(trim($sources));
}
$c->chunk(25);
$c->perform();
$proxies = array();
foreach($c->results as $url => $res) {
  // REGEX MATCH
  preg_match_all('@[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}:[0-9]    {1,6}@', $res, $m);
  $eachproxy = stream_get_contents($res);
  $proxies[$url] = $m[0]; {
    while ($proxies == time() && $eachproxy > 4) { // go into "waiting" when       we going to fast
      usleep(100000); // wait .1 second and ask again
    }
    if ($proxies != time()) { // remember to reset this second and the cnt
      $proxies = time();
      $eachproxy = 0;
    }
  }
  foreach($proxies as $url => $parr) {
    $str = implode("\n", $parr);
    file_put_contents('leeched/proxies.txt', $str);
    $k = count($parr);
    $str2 = date('h:i:s d m') . " | \t" . $k . "\t" . $url . "\n";
    file_put_contents('logs/counts.txt', $str2, FILE_APPEND);
  }
  $uar = file('leeched/proxies.txt');
  $uar = array_unique($uar);
  $str = implode("\n", $uar) . "\n";
  $str = preg_replace('/^\h*\v+/m', '', $str);
  file_put_contents('leeched/proxies.txt', $str);
}
// Proxy Testing
if (!is_file($fileName)) die('Proxy file not available');
$proxies = file($fileName);
for ($p = 0; $p < count($proxies); $p++) {
  $ch = curl_init(); //initizlize and set url
  curl_setopt($ch, CURLOPT_URL, "http://www.yordomain.com/check.php");
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_HTTPGET, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_PROXY, trim($proxies[$p]));
  $data = curl_exec($ch);
  usleep(100000);
  if (strpos($data, 'Anonymous') !== false) {
    usleep(100000);
    echo "<img src=\"images/good.png\">&nbsp;&nbsp;<font  color=\"#7CFC00\"><strong>" . $proxies[$p] . " </font></strong><font color=\"#FFFFE0\"><strong>   THIS IS A WORKING ANONYMOUS PROXY SAVED TO /goodproxies/success.txt</font></strong><font color=\"yellow\"><strong> " . "Total time: " . curl_getinfo($ch, CURLINFO_TOTAL_TIME) . " seconds!</font></strong><img src=\"images/small.png\"> <br/><br/>";
    $f = fopen($success, "a");
    fwrite($f, $proxies[$p]);
    fclose($f);
  }
  elseif (curl_errno($ch)) {
    usleep(100000);
    echo "<img src=\"images/bad.png\">&nbsp;&nbsp;<font color=\"white\"><strong>" . $proxies[$p] . " </font></strong><font color=\"red\"><strong>ERROR:</font></strong><font color=\"#00FFFF\"><strong> " . curl_error($ch) . " </font></strong><img src=\"images/redx.png\"> <br/><br/>";
  }
  else {
    echo "<img src=\"images/warning.png\">&nbsp;&nbsp;<font color=\"#7CFC00\"><strong> " . $proxies[$p] . "   </font></strong><font color=\"white\"><strong> THERE WAS NO ERROR CONNECTING BUT THIS PROXY IS NOT ANONYMOUS!  NOT SAVED</font></strong> <font color=\"#FF69B4\"><strong>(No content from source)</font></strong><img src=\"images/redx.png\"> <br/><br/>";
  }
  flush();
  curl_close($ch);
}
$done = "done";
echo $done;