Base solution for your next web application
Open Closed

Anonymous Acess to certain API #12408


User avatar
0
asbbh created

In my application, I need to allow anonymous access to specific APIs.
I have tried to add the following code to the API controller located at ./src/Project1.Web.Core/Controllers/PublicUserController.cs, but it does not work.

// ./src/Project1.Web.Core/Controllers/PublicController.cs
namespace Mantle.PowerAut0mater.Web.Controllers
{
    [AbpAllowAnonymous]
    [DisableAuditing]
    public class PublicUserController : PowerAut0materControllerBase
    {
        private IPublicUserAppService _publicUserAppService;
        public PowerSchoolController(IPublicUserAppService publicUserAppService )
        {
            _publicUserAppService = publicUserAppService;
        }

        [HttpGet]
        [Route("custom/plugins")]
        public async Task<IActionResult> GetPlugins()
        {
            var plugins = await _publicUserAppService.GetAvaliablePlugin();
            return Ok(plugins.ToDictionary(k => k.Name, v => v));
        }
    }
}

I saw the post Attribute roles to anonymous access (Guest) [#1831](https://support.aspnetzero.com/QA/Questions/1831) https://support.aspnetzero.com/QA/Questions/1831/Attribute-roles-to-anonymous-access-Guest , but the link were not accessible I badly need to use some of the API for non-login user


1 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @asbbh

    When I tested the latest version of the code you provided, I observed that the "GetPlugins" method is called successfully for a user who has not logged in. If you encounter any error codes, could you share them with us? Also, please provide your project version. Does this controller successfully list the data when you call the relevant endpoint via URL? Additionally, could you specify how you are calling this controller?