Jack
07/29/2024, 10:20 AMcsharp
namespace Test.Web.Controllers.Api
{
[ApiController]
[ApiVersion("1.0")]
[MapToApi("test-v1")]
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
[Route("api/v{version:apiVersion}/test")]
public class LogoutController : Controller
{
private readonly INotificationService _notificationService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly IHttpContextAccessor _httpContextAccessor;
public LogoutController(IBackOfficeSecurityAccessor backOfficeSecurityAccessor, INotificationService notificationService, IHttpContextAccessor httpContextAccessor)
{
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_notificationService = notificationService;
_httpContextAccessor = httpContextAccessor;
}
[HttpPost("logout")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
public IActionResult Logout()
{
IUser? user = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser;
if (user == null)
{
return Unauthorized();
}
return Ok();
}
}
}
The policy
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
Does not seem to be related/assigned to my role, what am i missing ?Jack
07/29/2024, 10:22 AMcsharp
IUser? user = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser;
is always null, which i cant figure why is, as i have been following the guidelines described here:
https://docs.umbraco.com/umbraco-cms/reference/custom-swagger-apiD_Inventor
07/30/2024, 6:17 AM/umbraco
and logged in there or did you use the authorize button in swagger?Jack
07/30/2024, 6:33 AMumbraco-swagger
and client_secret is left blank.Jack
07/30/2024, 6:52 AMJack
07/30/2024, 7:01 AMJack
07/30/2024, 7:03 AMD_Inventor
07/30/2024, 9:19 AMUseAuthentication
and UseAuthorization
, I would highly advise against adding those, because Umbraco does it for you and adding it yourself may actually break your application.D_Inventor
07/30/2024, 9:23 AMumbraco-swagger
as the client id? I don't see it in a quick glance, it just says to press the authorize button. You could try leaving it empty instead. It should lead you to the backoffice login and through there, you should obtain a token.Jack
07/30/2024, 10:07 AMD_Inventor
07/30/2024, 10:30 AMJack
07/30/2024, 11:37 AMJack
07/30/2024, 12:00 PMD_Inventor
07/30/2024, 12:03 PMJack
07/30/2024, 12:04 PMcsharp
public class MyBackOfficeSecurityRequirementsOperationFilter : BackOfficeSecurityRequirementsOperationFilterBase
{
protected override string ApiName => "test";
}
public class MyConfigureSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
{
public void Configure(SwaggerGenOptions options)
{
options.SwaggerDoc("test-v1", new OpenApiInfo { Title = "Test v1", Version = "1.0" });
options.OperationFilter<MyBackOfficeSecurityRequirementsOperationFilter>();
}
}
public class MyComposer : IComposer
{
public void Compose(IUmbracoBuilder builder) => builder.Services.ConfigureOptions<MyConfigureSwaggerGenOptions>();
}
D_Inventor
07/30/2024, 12:05 PMManagementApiControllerBase
as base class. So perhaps it is required to use that base classJack
07/30/2024, 12:08 PMJack
07/30/2024, 12:14 PMD_Inventor
07/30/2024, 12:26 PMMyBackOfficeSecurityRequirementsOperationFilter
that you use "test" as the api name, while you use "test-v1" everywhere elseD_Inventor
07/30/2024, 12:26 PMJack
07/30/2024, 12:52 PMcsharp
[MapToApi("test-v1")]
[VersionedApiBackOfficeRoute("api/test")]
[ApiExplorerSettings(GroupName = "test API")]
public class LogoutController : ManagementApiControllerBase
The first produce an endpoint that looks like this ``/umbraco/management/api/v1/api/test/logout`` (i should properly remove the api part of the route for a nicer looking endpoint).
csharp
[ApiController]
[ApiVersion("1.0")]
[MapToApi("test-v1")]
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
[Route("api/v{version:apiVersion}/test")]
public class LogoutController : Controller
The second produce an endpoint that looks like this ``/api/v1/test/logout``Jack
07/30/2024, 12:53 PMJack
07/30/2024, 12:57 PMcsharp
protected override string ApiName => "test";
To this
csharp
protected override string ApiName => "test-v1";
Did the trick.D_Inventor
07/30/2024, 1:01 PMJack
07/30/2024, 1:15 PMD_Inventor
07/30/2024, 1:32 PMJack
07/30/2024, 1:41 PMJack
07/30/2024, 1:50 PMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by