1

Hello I'm having an Issue with ASP.NET WEB API Login. I've already enabled CORS in my back end

here is from my WebApiConfig.cs

var cors = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*");
            config.EnableCors(cors);

web.config

 <customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Authorization" />
   <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>

from my REACT APP

 var params = {
                'userName' : this.state.email,
                'password' : this.state.password,
                'grant_type' : 'password'
            };

            var formData = new FormData();

            for (var k in params) {
                formData.append(k, params[k]);
            }

            fetch(loginApiUrl, {
                method: 'post',
                headers : {
                    'Access-Control-Allow-Origin' : '*',
                    'Accept' : 'application/json',
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body : formData
            })
            .then(json => json())
            .then(x => {
                alert(json.access_token)
               // localStorage.setItem('auth_token', `Bearer ${json.access_token}`)
            })

And I also use webpack dev server here is the config to allow CORS

 devServer : {
        headers: { "Access-Control-Allow-Origin": "*" },
        contentBase : path.join(__dirname, "dist"),
        compress : true,
        hot: true,
        open: true,
    },

And I keep getting this error from Google chrome console

Failed to load http://localhost:xxxx/token: Response for preflight does not have HTTP ok status.

Any help to get this to use OPTIONS verb?

Amin Mozhgani
  • 604
  • 1
  • 7
  • 22

0 Answers0