[RESOLVED}Umbraco 14 API controller
# help-with-umbraco
m
Hey friends I have noticed UmbracoApiController has now been depreciated, so i am trying to use the example from the docs using Microsoft.AspNetCore.Mvc; namespace UmbracoDocs.Samples; [ApiController] [Route("/api/shop/products")] public class ProductsController : Controller { [HttpGet] public IActionResult GetAll() => Ok(new[] { "Table", "Chair", "Desk", "Computer" }); } hitting the endpoint with postman - https://localhost:44325/api/shop/products/getall but i seem to just get a html reponse, im not sure what im doing wrong here? can anyone help? thanks!
m
😕
s
Think you need to tell postman that you expect json back
m
hey sebastiaan super cool you got back to me! feels like meeting a celebrity lol.
ok it must be a me problem, do i need to register anything in the program.cs? the docs dont mention but i just wanted to ask
Nothing to configure, this is just plain old .net!
m
Thanks so much!
a
You can also convince ASP.NET that it should always return JSON using something like:
Copy code
csharp
return Json(data);
Given that your controller extends ASP.NET's
Controller
class, you'll have acces to a bunch of different methods - with the
Json
method being one of them.
m
Sweet!! thanks so much everyone. glad i come across the discord!
just branching off of this as i dont want to create another Q, i see in the docs that the controller can be restricted to logged in members only etc. But is there a way to restrict the use of this public api to only logged in back office users? or do i need to create a custom attribute to do this
forgive me if i dont need to @ you two, not sure if you get notifications lol @Sebastiaan @Anders Bjerner
s
Only way I've found to have backoffice user auth is to have it under the
/umbraco
route. But I really don't know a lot about auth https://github.com/nul800sebastiaan/Cultiv.Hangfire/blob/bellissima/Cultiv.Hangfire/UmbracoBuilderExtensions.cs#L39
My advice, only @ someone if it's very urgent and nobody else can help
m
duly noted 🙂 forgive me this time around 🙂
s
all good!
m
oh interesting, so starting the route with "/umbraco" restricts it to just backoffice members
s
Not necesserily.. But again, I am not great at all of this stuff so I won't be able to give you conclusive answers.. best start a new thread 🙂
m
ill give it a shot anyway!, thanks sebastiaan youre a top g!
a
Umbraco 14 handles authentication very differently from previous versions of Umbraco. In previous versions, your controller should extend the
UmbracoAuthorizedApiController
class, and then users would only be able to access your controller if already authenticated with the backoffice. The authentication was cookie based, so you didn't need to pass any extra parameters for the authentication to work. Umbraco 14 instead uses OAuth 2.0 for authentication, meaning you should sent a bearer token via the
Authorization
header for each request. Umbraco 14 has also dropped support for
UmbracoApiController
and
UmbracoAuthorizedApiController
, so you can instead extend ASP.NET's
Controller
class directly instead. I haven't read up on all the authentication options, but say your controller should only be accessible to backoffice users with access to the content section, you can add this attribute to the controller: https://github.com/skybrud/Skybrud.Umbraco.Redirects/blob/v14/main/src/Skybrud.Umbraco.Redirects/Controllers/Api/BackOffice/RedirectsController.cs#L31 The frontend for the backoffice would then have to obtain the bearer token and pass it on to the controller. I don't think the logic around this is documented though - or I just didn't manage to find it. Bu you're welcome to peak a bit around in my code for inspiration: https://github.com/skybrud/Skybrud.Umbraco.Redirects/blob/v14/main/src/Skybrud.Umbraco.Redirects/wwwroot/EntryPoint.js#L9-L12 https://github.com/skybrud/Skybrud.Umbraco.Redirects/blob/v14/main/src/Skybrud.Umbraco.Redirects/wwwroot/RedirectsAuth.js https://github.com/skybrud/Skybrud.Umbraco.Redirects/blob/v14/main/src/Skybrud.Umbraco.Redirects/wwwroot/RedirectsService.js It is however worth mentioning that (at least for now) I have opted to use Vanilla JS, although Umbraco recommends using TypeScript. So there might be better examples out there somewhere.
m
thought so, ok thanks so much man! ill have to come back to this message as i have to move onto a different task 🙄 then come back to this. Thanks so much for the very detailed response! and to look at your code 🙂
b
How solved?
58 Views