[CLOSED] Authorize, redirect, Direct method call

Page 1 of 2 12 LastLast
  1. #1

    [CLOSED] Authorize, redirect, Direct method call

    I have the following scenario:
    A direct method call in the view calling a controller method on the server that returns a partial view. The partial view is presented as an Ext.Net window. The controller method is marked with [Authorize] attribute. When the session expires and the user is logged out, what happens now is that you just get a flicker on the screen for the Loading mask and then nothing. You still see the same page.
    I would like to redirect the user to the login page in this scenario.
    How can I do this please?
    Last edited by Dimitris; Feb 19, 2015 at 3:25 PM. Reason: [CLOSED]
  2. #2

    According to the Authorize attribute documentation a 401 status code is returned in case the user is not authorized and a redirection to the login page is performed if the site is configured to use forms authentication.

    Your ajax call probably gets back the status code as a response, which causes the redirection to be short of ... consumed. I think you need to check for the http status code in your failure callback handler and redirect to the login page by setting the window.location yourself. This is best described in the following example (courtesy of @Daniil):


    Hope it helps.

    Attention members: the above answer can be misleading. The question is better answered here.
    Last edited by Dimitris; Feb 21, 2015 at 12:45 PM.
  3. #3
    Hi Dimitris
    I got to test out the solution you have provided and I think it should be good but I can't get the Failure event fire on my button direct event.
    The custom attribute part works fine and I can debug it. So there is a response sent back to the browser.
    I have even tried with just a generic javascript message but it does now get fired:

    X.Button().Text("Edit").Icon(Icon.UserEdit).IconAlign(IconAlign.Top).Width(50).DirectEvents(de =>
                        de.Click.Url = Url.Action("EditClient");
                        de.Click.EventMask.ShowMask = true;
                        de.Click.ExtraParams.Add(new Parameter()
                            Name = "selection",
                            Value = "App.Clientsgrid.getSelectionSubmit().getSelectionModelField().getValue()",
                            Mode = ParameterMode.Raw
                        de.Click.Failure = @"Ext.Msg.show({ 
                                    title   : 'Error', 
                                    msg     : 'Error during uploading', 
                                    minWidth: 200, 
                                    modal   : true, 
                                    icon    : Ext.Msg.ERROR, 
                                    buttons : Ext.Msg.OK 
    Can you help out please?
  4. #4
    My project uses your Examples MVC project as a base and so the main.js file is what I edit.
    I have tried doing this, but still the failure function does not get hit.

    tab = App.ExampleTabs.add(new Ext.panel.Panel({
            id   : id,        
            tbar: [{ xtype: 'label', id: id + 'L', margins: '0 0 0 10', text: title },
                text    : "Refresh",
                handler : function () {
                iconCls : "#PageRefresh"
            title    : title,
            tabTip   : tabTip,
            hideMode : "offsets",        
            loader : {            
                renderer : "frame",
                url: hostName + url,
                failure: function () {
                    $(document).ajaxError(function (e, xhr) {
                        if (xhr.status == 401) {
                            var response = $.parseJSON(xhr.responseText);
                            window.top.location = response.LogOnUrl;
    How do you trigger the failure function?
  5. #5
    Hello registrator and sorry for the delayed answer,

    First things first. Let's take a look back at your first post. I have created a small example to illustrate three main points:
    1. A DirectMethod request with a failure callback handler.
    2. An Authorize attribute decorating a controller method that returns a partial view which in turn defines a Window component
    3. Auto redirect to a login page as soon as Forms authentication is configured in the web.config. Please, note that in my tests the request's failure handler is called if no authentication configuration exists in the web.config. Otherwise, you are automatically redirected to the login page, which I think we both agree is the expected behavior.

    Partial View (WindowContent.cshtml):

        .Html("<b>Only Administrators are authorized to this content.</b>")


    public class HomeController : Controller
        public ActionResult Index()
            return View();
        public ActionResult WindowContent()
            return new Ext.Net.MVC.PartialViewResult();


        var X = Html.X();
        .Text("Click me")
        .Handler(@"var loadmask = new Ext.LoadMask(Ext.getBody(), { });
                url: '/Home/WindowContent',            
                cleanRequest: true,
                success: function(result) {
                failure: function(error, response) {
                    if (response.status == 401) {
                        Ext.Msg.alert('Error', response.statusText);


        <roleManager enabled="true" />
        <authentication mode="Forms">
          <forms loginUrl="Account/Login" />

    I do hope it helps.

    If you have any kind of trouble solving this specific issue please feel free to ask. Please, do note that any other questions should be asked in separate threads. We very much appreciate it (and our members, in the long run) if only one question is treated per thread.
  6. #6
    Hi Dimitris

    Your help is very much appreciated. And the example you have created. Although you are calling a direct method in a different way I beleive this functionality should work when you call a direct method with
    .DirectEvents(de =>

    When I transform your code to mine in the exact way, I don't get the failure back but success! When logged out, when not in the role... getting back success as a result.
    I don't understand why?

    Thank you
  7. #7
    Do you mean you can see the Window from the partial view popping up even if you are not authorized?

    If so, then you must review and test the authorization/authentication settings of your project. Are Membership and Authorization correctly setup in your project? This is a huge subject with numerous references both in the official ASP.NET site and in StackOverflow.com. For example, here is a tutorial: http://www.asp.net/mvc/overview/olde...c-store-part-7
  8. #8
    No, the window does not pop up but setting the brake point in debugger on success and fail functions, it always stops on success. Never goes into failure function. Do you know why it can happen?
  9. #9
    Probably because of this?

    In case this is not what you are dealing with please provide a full sample to work with. Your sample should include Controller, Model, View and/or PartialView snippets. I need to be able to copy and paste and run it as is without serious modifications.
  10. #10
    I'll read it. I just skimmed through it and it doesn't seem like it's referring to me because I use
    using Microsoft.Owin.Security;
    in an MVC5 solution. Perhaps that could be the issue, I guess I should have mentioned it earlier. Sorry if it make a big difference in your answers.
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: May 28, 2013, 2:51 PM
  2. [CLOSED] Direct Event/Method Call Priority
    By bayoglu in forum 2.x Legacy Premium Help
    Replies: 5
    Last Post: Feb 08, 2013, 2:04 PM
  3. [CLOSED] how to call direct method
    By tactime10 in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Oct 04, 2012, 8:38 AM
  4. [CLOSED] Can I use Response object in a direct method call?
    By feanor91 in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jun 12, 2012, 11:50 AM
  5. how to call direct method in asp.net mvc3
    By waqasde in forum 2.x Help
    Replies: 1
    Last Post: Mar 16, 2012, 9:26 AM

Posting Permissions