2

I am not sure if this is the way to go but here is what I am trying to achieve. I have two different types of controller action methods. And I need two different authentication method on each of them.

E.g. HomeController

[Authorize(AuthenticationSchemes = "Windows")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

ServiceController

[Authorize(AuthenticationSchemes = "BasicAuthentication")]
[Route("api/my-service")]
[ApiController]
public class ServiceController : ControllerBase
{
    [Route("Evaluate")]
    [HttpPost]
    public EvaluateResponse Evaluate([FromBody]EvaluateRequest request)
    {
        //return something;
    }
}

Startup.cs (ConfigureServices)

services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddAuthentication("BasicAuthentication")
            .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

Startup.cs (Configure)

app.UseAuthentication();

Code for Basic authentication is implemented like mentioned here.

Now, if I am trying to invoke api/my-service/Evaluate it's still invoking windows authentication.

How can I implement two different authentication technique, one for HomeController and another for ServiceController?

5
  • mitchelsellers.com/blogs/2018/03/20/… Commented Jul 18, 2019 at 19:05
  • @RyanWilson I went through that article already. I am not able to understand, how would I tell HttpRequest to bypass windows authentication, validate based on basic authentication if it is redirected to ServiceController? Commented Jul 18, 2019 at 19:13
  • You can specify different authentication schemes for different controllers. Commented Jul 18, 2019 at 19:18
  • @Shaggy This shows how to use both Jwt and Basic in the same project, it's an API project but it should still be able to show you what you need (stackoverflow.com/questions/49148648/…). I'm kind of confused by your architecture though, if your Service contains API's exposed to the outside world as you put it, why not make it it's own standalone entity, and call out to it from the web project? Commented Jul 18, 2019 at 19:18
  • Let's just assume I'd like to have windows authentication on one controller and basic authentication on another controller. I have updated my question with the required code. Thanks. Commented Jul 18, 2019 at 19:47

2 Answers 2

0

My code was working, just needed to add following in launchSettings.json file

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you should not need to put windowsAuthentication for basic authentication

it should look like "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:xxxx", "sslPort": xxxxx } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.