Kyle Vanderstoep
Kyle Vanderstoep

Reputation: 771

Anonymous Authentication with Custom Controller

I am trying to implement a custom controller in Acumatica for development purposes. But I cant seem to figure out how to sidestep Acumatica auth and allow access without authentication.

Here is my Controller:

https://www.acumatica.com/blog/using-asp-net-web-api-mvc-with-acumatica/

    [RoutePrefix("test")]
    public class TestController: ApiController
    {
        [HttpGet]
        [Route()]
        [AllowAnonymous]
        public IHttpActionResult PerformAction()
        {
            return Ok("Actions Available");
        }
}

And here is my startup

public class Startup
{
    public static void Configuration(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
    }
}

public class ServiceRegistration : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        GlobalConfiguration.Configure(Startup.Configuration);
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    }
}

But when I send a GET to {baseUrl}/test in Postman, it returns 401 unauthorized. If I open my browser, log in and go to that same route, I recieve "actions available"

What am I missing to allow anonymous Auth on a custom WebApi Controller?

Thanks

Upvotes: 0

Views: 122

Answers (1)

Kyle Vanderstoep
Kyle Vanderstoep

Reputation: 771

Authorization can be customized inside the Autofac module in the extension library. Reference PX.Export, PX.Hosting (this was done for 2021R1)

public class Startup
{
    public static void Configuration(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
    }
}

public class ServiceRegistration : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        GlobalConfiguration.Configure(Startup.Configuration);
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

// Configuration of Authorize here
        builder.Configure<AuthenticationManagerOptions>(options =>
            options.AddLocation("sourcecontrol").WithAnonymous());
    }
}

Upvotes: 1

Related Questions