[OPEN] [#1765] Problem send date directmethod

  1. #1

    [OPEN] [#1765] Problem send date directmethod

    Hi,
    I am migrating to version 5.2, and I realized that the dates sent via direchtmethod are converted to utc.
    My browser timezone is +2.
    See my example, if I open the page on 17-04-2020, the datefield show 17-04-2020 but on the server I receive 16-04-2020 22:00:00.
    In version 4 I was given the date shown.
    Thank you

    <%@ Page Language="C#" %>
    
    
    <script runat="server">
    
    
        [DirectMethod]
        public void ShowDate(DateTime dt)
        {
    
    
            X.Msg.Show(new MessageBoxConfig
            {
                Title = "Title",
                Message = dt.ToString(),
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR
            });
        }
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !X.IsAjaxRequest)
            {
                var date = DateTime.Now;
    
    
                DateField1.SelectedDate = DateTime.Now;
            }
        }
    
    
    
    
    </script>
    
    
    <!DOCTYPE html>
    
    
    <html>
    <head runat="server">
        <title></title>
        
        <script type="text/javascript">
            var btnClick = function () {
    
    
                App.direct.ShowDate(App.DateField1.getValue());
    
    
            }
        </script>
    
    
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
    
           <ext:DateField
                        ID="DateField1"
                        runat="server"
                        FieldLabel="Date">
                        <CustomConfig>
                            <ext:ConfigItem Name="endDateField" Value="DateField2" Mode="Value" />
                        </CustomConfig>
                    </ext:DateField>
    
    
            
            <ext:Button ID="btn" runat="server" Text="Send">
                <Listeners>
                    <Click Handler="btnClick();" />
                </Listeners>
            </ext:Button>
        </form>
    </body>
    </html>
    Last edited by fabricio.murta; Nov 12, 2020 at 10:01 PM. Reason: closed by mistake, checked wrong issue (1605).
  2. #2
    Hello Jimmy!

    This happens because of a breaking change we logged under #1605. Due to some undisclosed reports of XSS vulnerabilities in Ext JS's implementation of JSON encoding and decoding algorithms, Sencha turned the default value to use browser's native implementation.

    Turns out that when this is subject to the JSON stringify call, it "flattens" the date's time zone, converting the displayed date/time to GMT.

    One option to revert this behavior would be to set back Ext JS to use its own implementation of JSON converter methods. This is possible, at least to some extent, with:

    <ext:ResourceManager runat="server">
        <Listeners>
            <DocumentReady Handler="Ext.USE_NATIVE_JSON = false" />
        </Listeners>
    </ext:ResourceManager>
    The limitation here is, this setting won't be in effect until the page is loaded. Unfortunately there's no way to inject scripts between Ext JS scripts and the page's specific Ext JS code (so that first thing after reading Ext JS file would be to unset that variable). Using resourcePlaceHolder won't help either as you can add code either right after and right before Ext.NET resources are added.

    The implication of the limitation above is, if a direct method is called before the page loads, it will still use the native json approach. That is possible, for example, if a store has a "loader" or "proxy" and will only pull data from server after it is initialized -- the first time it loads data from the server will likely use the native implementation of JSON even with the code above.

    One another alternative would be, handle the time zone shift by assuming all DateTime objects received by the server are in UTC. This is far from elegant and probably will require you to subtract the time zone offset of every DateTime object received from direct methods; and maybe direct events, or even resolving DateTime components and store data fields from post back and ajax calls.

    Maybe the best approach here, that will also allow the least opening to whatever XSS issue this setting avoided by being changed is to replace the browser's prototype to stringify JavaScript Date() objects using Ext JS's code, that "truncates" the time zone information.

    To attain such a behavior, you can change your test case by just adding the following block to the head section of the page:

    <script type="text/javascript">
        Date.prototype.toJSON = function () {
            var ejs_encoded = Ext.JSON.encodeDate(this);
            return ejs_encoded.substring(1, ejs_encoded.length - 1);
        }
    </script>
    <ext:ResourcePlaceHolder runat="server" />
    You'll then have Ext JS serializer running for Date objects when "native JSON" (that you are just overriding here) is applied, conserving any other native JSON processing, native.

    The ResourcePlaceHolder right after the override script block ensures any Ext JS related code (and page's init script with your components' definitions) will only be included in the page after the Date conversion prototype is properly overridden.

    Neither option above seems reasonable for a generic Ext.NET fix, some more thought should be put in that issue before we can offer a proper fix. We've logged it under #1765 in our issues tracker and will post an update here as soon as we implement a definitive fix for that.
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. [CLOSED] send object[] using DirectMethod
    By rbtceo in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jun 12, 2014, 6:05 AM
  2. [CLOSED] send store data items to directmethod
    By snow_cap in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 29, 2013, 10:07 PM
  3. [OPEN] [#129] Can Ajax requests send If-Modified-Since headers
    By anup in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Jan 18, 2013, 4:26 AM
  4. [CLOSED] formID with directMethod fails to send dates
    By aisi_it_admin in forum 2.x Legacy Premium Help
    Replies: 7
    Last Post: Jan 01, 2013, 1:59 PM
  5. Replies: 0
    Last Post: Nov 02, 2011, 8:34 AM

Posting Permissions