Search code examples
asp.net-mvc-3entity-frameworkc#-4.0membership-provider

Status code or unauthorization message not receive while calling simple jsonresult method via jquery $.ajax


I am working on MVC3 application where membership with EF implemented.Here Membership is applied on all action method of each controller with [Authorize] attribute. But in not applied on JsonResult method, Here Actionresult method just used to show page while jsonresult methods are used for validations by making async calls.

For eg. In View page there is dropdown and when change its option, its validated by async call. So when session expire. and user changing the option from dropdown then async call made by $.ajax method which couldn't return code like 403.So I require how can I get this code.


Solution

  • If I understand your question correctly, you:

    • protect most methods on your controller with [Authorize]
    • have JsonResult methods that you use for validation that are not protected, and
    • want to return a 403 from the Json methods if the user's session expires?

    I don't think I understand why you don't just [Authorize] on the Json methods, but you could do this:

    if (!User.Identity.IsAuthenticated)
        return new HttpStatusCodeResult(System.Net.HttpStatusCode.Forbidden);
    

    Cheers.