Base solution for your next web application
Open Closed

AbpAuthorize - No user logged in #393


User avatar
0
cosmic created

Hi, I found a very interesting thing.

When I'll use [AbpAuthorize] attribute on any application service and then I'll call this service direct through the JavaScript, like this:

abp.services.app.machine.getStatusAsync({
    id: machineId
}).done(function (data) {
    // some code
});

Then the data.result will be always "No user logged in!". But user is logged in and he has full permissions.

But, when I'll call through the MVC controller the same app service still with the [AbpAuthorize] attribute, like this: JavaScript

$.get(url, { id: machineId }).done(function (response) {
    abp.message.info(response.result);
});

MVC Controller

public async Task<JsonResult> GetStatus(int id)
{
    var input = new GetMachineStatusInput
    {
        Id = id
    };
    var output = await _machineAppService.GetStatusAsync(input);

    return Json(new MvcAjaxResponse(output.Response.Status), JsonRequestBehavior.AllowGet);
}

Then the expected result will be returned.

Why this is happening? How I can resolve this?


5 Answer(s)
  • User Avatar
    0
    ddnils created

    Hi, I am not sure about this, but have you tried [Abp.WebApi.Authorization.AbpApiAuthorize] ?

  • User Avatar
    0
    cosmic created

    Hi, [Abp.WebApi.Authorization.AbpApiAuthorize] attribute is for usage with Web API, not for application services. See: [http://aspnetboilerplate.com/Pages/Documents/Authorization])

  • User Avatar
    0
    hikalkan created
    Support Team

    Interesting.. How did you inject MachineAppService to your controller? Did you inject the app service interface or implementation?

  • User Avatar
    0
    cosmic created

    Here is my machine controller:

    public class MachinesController : AdminControllerBase
    {
        private readonly IMachineAppService _machineAppService;
    
        public MachinesController(IMachineAppService machineAppService)
        {
            _machineAppService = machineAppService;
        }
        
        public async Task<JsonResult> GetStatus(int id)
        {
            var input = new GetMachineStatusInput
            {
                Id = id
            };
            var output = await _machineAppService.GetStatusAsync(input);
        
            return Json(new MvcAjaxResponse(output.Response.Status), JsonRequestBehavior.AllowGet);
        }
    }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I could not understand the reason. Because, I have used [AbpAuthorize] for many app services and they are just working. I want to help you but I can not repeat the problem.

    BTW, why don't you use abp.ajax instead of $.get?