0

I have a problem with calling the API controller from another project.

These are what I have now:

  • One Solution which contains 9 projects in it

  • 1 of the 9 projects is a web project that has dependency on another 4 projects within the same solution

  • 1 of the 9 projects is a console apps that also has a dependency on other 4 projects

  • The console apps and Web projects do not have dependency on each other. They will run independently

  • Console app needs to make Https post request to the web project, so that certain events can be triggered in the web project

  • Both are deployed independently, and always runs at same server

In the console apps, I have following code:

static readonly HttpClient httpClient = new HttpClient();
var response = await httpClient.PostAsync("http://localhost:57110/Api/WebProjectApi/WebProjectController1", null);

In the web, I have this controller:

[System.Web.Http.HttpPost]
public async Task<IHttpActionResult> WebProjectController1()
{
//blah blah
}

Problem:

Response is always 401 - Unauthorised even though I don't have authentication setup.

I even doubt I was able to test it properly, since there's no inter-dependency between these 2 projects, when I start up console apps from Visual Studio, the web apps cannot be running locally. Is there a way for me to test locally?

EDIT

The full error message as below:

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  X-SourceFiles: =?UTF-8?B?QzpcQ29kZVxJZ25pdGVcYnJhbmNNccHJveHlWb3Rpbmdcc3JjXERvdE5ldFxHSUWduaXRlLldlYlxBcGlcUHJveHlWb3bmdBcGlcQXBwcm92ZZGlmaWNhdGlvbg==?=
  Cache-Control: private
  Server: Microsoft-IIS/10.0
  WWW-Authenticate: Negotiate
  WWW-Authenticate: NTLM
  X-Powered-By: ASP.NET
  Date: Wed, 25 Jul 2018 03:02:10 GMT
  Content-Length: 6208
  Content-Type: text/html; charset=utf-8
}

This post seems to be exactly same thing as I want, but no clear answer was posted there :(

Call asp mvc Controller Action from another application in .net?

EDIT 2

Ultimately, I still need the authentication on the controller, for security reasons. For this post, I didn't mentioned that part as I think it would be another topic (e.g. How to impersonate as someone that console app defines at runtime.). Hence, here I am just asking how to integrate without authentication first.

xzk
  • 827
  • 2
  • 18
  • 43
  • 1
    Is the web project and console app in the same solution? If so, you can set multiple startup projects. Right click the solution, choose properties, then under "Startup Project" choose "Multiple startup projects". – w4g3n3r Jul 25 '18 at 02:40
  • @w4g3n3r it definitely helped! However, the HTTP `401` error still persists. – xzk Jul 25 '18 at 03:03

1 Answers1

1

Try adding the [AllowAnonymous] attribute before your action method on the controller to turn off authentication for just that endpoint.

You can also try adding <authentication mode="None" /> to the web.config if you want to turn off authentication for the entire application.

w4g3n3r
  • 967
  • 5
  • 8