Search code examples
servicestackbasic-authentication

ServiceStack Basic Authentication HtmlRedirect is not respected by MVC ServiceStackController


I'm probably not understanding something but I have the issue below:

AppHost.cs

Plugins.Add(new AuthFeature(
            () => new AuthUserSession(),
            new IAuthProvider[] { new BasicAuthProvider() }) { HtmlRedirect = null });

HomeController.cs

[Authenticate]
public class HomeController : ServiceStackController

The issue

The issue is that when I try to access the HomeController, I am redirected to ~/login?redirect=.....

I would assume that by setting HtmlRedirect to null, would also affect the MVC controllers too, but it doesn't seem to.

Is this expected behaviour? or is am I doing something wrong?

My end goal is to have the browser prompt with a challenge / response basic auth box.


Solution

  • Since this commit you are able to override the default behavior when authentication failed:

    [Authenticate]
    public class HomeController : ServiceStackController
    {
        public override ActionResult AuthenticationErrorResult
        {
            get
            {
                //return 401 Unauthorized for example
                return new HttpStatusCodeResult(401);
            }
        }
    }
    

    ServiceStackController.AuthorizationErrorResult can be modified in the same way.

    Setting HtmlRedirect to null doesn't work in this case, because the behavior of the [Authenticate] attribute (and all other ServiceStack attributes) is slightly different when used with ASP.net MVC controllers instead of ServiceStack services.