Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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
Asp.net web api ';访问控制允许原点';标头包含多个值'''*';_Asp.net Web Api - Fatal编程技术网

Asp.net web api ';访问控制允许原点';标头包含多个值'''*';

Asp.net web api ';访问控制允许原点';标头包含多个值'''*';,asp.net-web-api,Asp.net Web Api,我正在使用ASP.NET WEB Api,并且正在使用身份验证令牌,Internet Explorer将生成令牌字符串: 但使用任何其他浏览器都会显示以下错误: “访问控制允许来源”标题包含多个值 AJAX代码: function ObtenerToken(user, password) { var data = { username: user, password: password, grant_type: "password" } $

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

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

“访问控制允许来源”标题包含多个值

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
    {

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

我很确定这是因为您要在所有这三个位置添加访问控制允许源标题。您应该在Startup.Auth.cs、WebApiConfig.cs或控制器中启用CORS—其中一个位置,而不是全部三个位置。

您需要启用CORS

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        ConfigureOAuth(app);

        WebApiConfig.Register(config);

        // Make sure this line is called before ConfigureAuth(app), 
        // app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        app.UseWebApi(config);
    }

您需要启用cors

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        ConfigureOAuth(app);

        WebApiConfig.Register(config);

        // Make sure this line is called before ConfigureAuth(app), 
        // app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        app.UseWebApi(config);
    }