0

I have to ASP.NET applications: one is a Web API and the other a MVC that makes requests to the first.

Everthing works in development (VS with IIS Express) but now I publish both applications to a production server and I can´t have access to the api using the MVC app.

I have CORS enabled on the API:

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

On the MVC app, my api base url is set to https://localhost:44351/ and then in the IIS I have this bindings (WEB Api site):

bindings

I can make requests to the API with postman but when I run the my MVC app, I can´t make requests (and again, I could make them in development).

Thank you.

EDIT

Controller code example (MVC):

        try
        {
            var client = WebApiHttpClient.GetClient();

            var response = await client.PostAsync("Token", content);
            if (response.IsSuccessStatusCode)
            {
                TokenResponse tokenResponse =
                await response.Content.ReadAsAsync<TokenResponse>();

                WebApiHttpClient.storeToken(tokenResponse);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return Content("response.StatusCode);
            }
        }
        catch
        {
            return Content("Error");
        }

My HttpClient implementation:

    public const string WebApiBaseAddress = "https://localhost:44351/";

    public static HttpClient GetClient()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(WebApiBaseAddress);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new
        MediaTypeWithQualityHeaderValue("application/json"));
        return client;
    }

EDIT 2

InnerException message:

the underlying connection was closed could not establish trust relationship for the ssl/tls

1 Answers1

2

Just to summarize the discussion in the comments. The problem was due to use of self-signed certificate in IIS for Web API application.

Temporary solution for this problem is to add this line of code in application startup class (Global.asax):

ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

Of course you should not use that line of code in production. In production environment you should have a valid SSL certificate.

More information can be found in this post: Could not establish trust relationship for SSL/TLS secure channel -- SOAP

Community
  • 1
  • 1
Viktors Telle
  • 763
  • 9
  • 22