When I goto a page in my webapp that has the Authenticate Attribute and I am not logged in I get a 401 error. But I want to have a redirect to my login page.
My request - I tried adding the HtmlRedirect
[Route("/auth-test")]
public class TestRequest { }
[Authenticate(HtmlRedirect = "/login")]
public string Get(TestRequest r)
{
return "OK!";
}
My Configure.Auth - I tried adding the HtmlRedirect on the AuthFeature
public class ConfigureAuth : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureAppHost(appHost => {
appHost.Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider()
}, "/login"));
});
}
I think the answer must be simple but I just can’t find it…
I can’t reproduce this, the HtmlRedirect set in the constructor should be all you need to redirect 401 Unauthorized redirects:
appHost.Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider()
}, "/login"));
I’m assuming there’s something else in the App interfering with it. If you can put a small stand-alone repro on GitHub I’ll be able to identify the issue. The vue-mjs is a good template to start with configured with ServiceStack Auth:
Sorry about my unreproducible question! But thank you for the answer. It helped me to find the problem
This was the problem:
In my AppHost I had: Plugins.Add(new HandleExceptions());
With this plugin:
public class HandleExceptions : IPlugin
{
public void Register(IAppHost appHost)
{
appHost.ServiceExceptionHandlers.Add(HandleServiceExceptionDelegate);
}
public object HandleServiceExceptionDelegate(IRequest httpReq, object request, Exception ex)
{
//return null; // I should have used this return null for the default behavior
return DtoUtils.CreateErrorResponse(request, ex, new ResponseStatus(ex.ToErrorCode(), ex.Message));
}
}
When I changed the CreateErrorResponse to return null the redirect worked again.