Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mmaty 115 posts 289 karma points
    3 days ago
    mmaty
    0

    Umbraco 15 Backoffice API

    I'm going my first steps with Umbraco 15. What I want to achieve is getting and calling a backoffice api controller like it was possible with UmbracoAuthorizedApiController in the past.

    I learned, that the APIs now have the route /umbraco/management/api/v1/ plus some additional route segments. I can't manage to get a simple controller done. Consider the following code:

    [VersionedApiBackOfficeRoute("laykit/environment")]
    [ApiVersion("1.0")]
    public class EnvironmentController : ManagementApiControllerBase
    {
        [HttpGet]
        public string Get()
        {
            return "Teststring";
        }
    }
    

    What is the right route to call this API from my backoffice extension? Is the above definition false? This would be the client code:

    fetch("/umbraco/management/api/v1/laykit/environment")
        .then(data=>this.data = data);
    

    I tried with and without the get segment, it doesn't work. What I also want to achieve is, that the backoffice login should be sufficient to be authorized to all this api.

    Do you have any insights to share, which makes this working?

  • Yasir Butt 162 posts 372 karma points
    1 day ago
    Yasir Butt
    0

    Hi,

    Here is my simple backoffice controller for my custom section.

        [ApiController]
    [BackOfficeRoute("api/v{version:apiVersion}/[controller]")]
    [Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
    [MapToApi("ich")]
    public class BackOfficeControllerBase : ControllerBase
    { }
    

    You need some other configuration to make it work.

    public class IchBackofficeApiOperationSecurityFilter : BackOfficeSecurityRequirementsOperationFilterBase
     {
         protected override string ApiName => "ich";
     }
    

    Your swagger config if you need it

     internal class ConfigureBackofficeSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
     {
         public void Configure(SwaggerGenOptions options)
         {
             options.SwaggerDoc(
                 "ich",
                new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Title = "Ich Backoffice Api",
                    Version = "1.0",
                    Description = "Ich backoffice Api"
                });
             options.OperationFilter<IchBackofficeApiOperationSecurityFilter>();
              var schemaHelper = new SwashbuckleSchemaHelper();
              options.CustomSchemaIds(type => schemaHelper.GetSchemaId(type));
             //options.OperationFilter<TimeBackOfficeSecurityRequirementsOperationFilter>();
         }
     }
    

    And a composer

    public class IchApiComposer : IComposer
     {
         public void Compose(IUmbracoBuilder builder)
         {
             builder.Services.ConfigureOptions<ConfigureBackofficeSwaggerGenOptions>();
         }
     }
    

    Hope it will help you.

    /Yasir

Please Sign in or register to post replies

Write your reply to:

Draft