[CLOSED] Authorize, redirect, Direct method call

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Hello,

    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):

    http://stackoverflow.com/questions/5...in-asp-net-mvc

    Hope it helps.

    Update:
    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 () {
                    Ext.getCmp(id).reload(true)
                },
                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.X().Window()
        .Title("Window1")
        .Html("<b>Only Administrators are authorized to this content.</b>")
        .Width(300)
        .Height(250)
    )

    Controller:

    [DirectController]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [DirectMethod]
        [Authorize(Roles="Administrators")]
        public ActionResult WindowContent()
        {
            return new Ext.Net.MVC.PartialViewResult();
        }
    }

    View:

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

    web.config:


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

    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 =>
    etc.

    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

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