1

My question is quite similar to this but I couldn't figure out how can I keep things DRY. By using the answer of this post, I can authenticate check a particular AJAX call but my application has several AJAX calls and if I go by this approach then I will have to keep a check on every AJAX call success callback. Which is not quite cool.

Can you suggest a nice way to take care of this and keep things DRY?

Javascript

function GetStuff(x) {
            if (!x.value) return;
            $.post('/website/GetStuff', { val: x.value, text: x.text },
            function (data) {
                $("#ddlStuff").empty().append($("<option>").attr("value", "0").text("Choose...")).removeAttr("disabled");
                $.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("#ddNewStuff")); });
            });
        }

C#

 [HttpPost]
        public JsonResult GetSutff(long val, string text)
        {
            List<string> data = new MyClass().GetDataFromDB(val, text);
            return Json(data);
        }

Since the controller where GetStuff action is defined has Authorise tag on top of it. Therefore, all calls made here will have to be authenticated. Now, if the session expires, then this action GetStuff returns home page's html (home page having return url - pretty standard stuff) and that causes an issue. Like this there are other view pages as well whose ajax post actions are authenticated and they also fail on client side when the session fails and html of home page gets returned.

I want the user to be redirected a session expired kinda page and want to know a nice and cool way of doing that. I hope I have made things clear. If not, let me know.

Thanks

Community
  • 1
  • 1
saarthak
  • 1,004
  • 2
  • 12
  • 26
  • Can you be more specific with what you want to achieve please? Provide some code examples. –  Oct 11 '11 at 09:06

1 Answers1

0

If you remove authentication annotation from your controller, you can have the following:

public JsonResult GetSutff(long val, string text) 
{ 
    if (Request.IsAuthenticated)
    { 
        List<string> data = new MyClass().GetDataFromDB(val, text); 
        return Json(data); 
    }else
    {
      // Return empty JSON
    }
}

Please let me know if you can find a different solution. Also I didn't check, but there should be a way to override Authentication per action method, rather than removing it from entire controller.