With ApiController, Authentication.Challenge not prompting Microsoft login for SSO. it executes SignIn action method, with out any errors. If I change from ApiController to Controller then it's prompting. does any one know how to prompt for Microsoft login using ApiController?
public class ValuesController : ApiController
{
[System.Web.Http.Route("api/values/signin")]
[System.Web.Http.HttpGet]
public void SignIn()
{
if (!System.Web.HttpContext.Current.Request.IsAuthenticated)
{
HttpContext.Current.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
}
public class ValuesController : Controller
{
public void SignIn()
{
if (!System.Web.HttpContext.Current.Request.IsAuthenticated)
{
HttpContext.Current.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
}
We also faced a similar problem on our product.
The issue was the following:
Challengesets401status code for current response, which is later handled by a responsible OWIN Middleware, so if status code is not401the middleware won't handle the response and won't trigger the redirect.But the default behavior of
voidaction ofApiControllersets204response status code. Therefore401is overwritten with204, as a result nothing happens.So there are several solutions:
ApiControllerif you canApiControllerbut notvoidaction. Use for example something like thisvoidmethod andApiControllerthen you can end the response and then the status code won't be modified.