locked
Application_LoggedIn() equivalent in the HTML client RRS feed

  • Question

  • I think I may have asked this before but is there an equivalent way of running code in an Application_Logged in event in the HTML client? I want to record date and times of log in's from our users.

    Thanks.

    Tuesday, December 15, 2015 11:20 AM

Answers

  • There is some setup involved but it is not that difficult to implement.  Here are the basic steps:

    - Create a POCO that will contain the data you want to log.  I'm assuming you will log this to a database table that is part of the LS model.  If that is not correct, the implementation will be a little different.

    - Create a web api controller that uses LS Server Application Context to get the user and create a new entry in the AppLog table, set the property values you want to log (i.e. username, fullName, LoginDateTime, etc.  You can use a Get or a Post depending on whether you care about http status codes.  A Get is easier to configure but both work.  Test your web api in fiddler or browser tools and make sure it works.  You will also need a Global.asax and possibly a WebApiConfig file to set up the api routing.  I am assuming you know how to do that.

    - Go to the Scripts folder of the html client project.  Add a new javascript file.  Name it myModule.js or whatever you want.  Drag a reference to the script into the Default.htm such that it is below all of the default scripts but before the document ready function. 

    - Create a js function that runs when the script loads and creates your namespace.  You can execute an ajax call to the web api from here that will log the user.

    var myModule = this.myModule || {}; (function ($, module, undefined) { var user = {}; var logUser = function () { $.ajax({ type: 'GET', url: '../api/Users/LogUser/', success: function (data) { // do something on success // you could return an object with the user data user = data; }, error: function (jqXHR, textStatus, errorThrown) { // do something on error } }); }; // execute logUser at startup (function () { logUser(); })(); // expose the function and the user object so they can be called in screen code if you want module.logUser = logUser;

    module.user = user; })(jQuery, myModule);

    I use a module like this that has all of my reusable javascript code, including any code I would call from a render or post_render method.  It is very handy because you can reduce most of your actual screen code to a one-liner.  For example in a screen render function you can do: myModule.renderCustomMenuTile(element, contentItem); instead of writing that same code in a bunch of different screens.




    • Edited by Hessc Friday, December 18, 2015 7:33 PM
    • Proposed as answer by Angie Xu Thursday, December 24, 2015 11:40 AM
    • Marked as answer by Angie Xu Thursday, December 24, 2015 11:40 AM
    Wednesday, December 16, 2015 1:40 PM

All replies

  • Not really.  I do this by adding a separate javascript module that loads and runs from default.htm.  In there I hit a web api to get details about the user, including whether they are authenticated.  You could do the same and handle your logging there.  It is not as convenient as Application_LoggedIn() but it works.
    Tuesday, December 15, 2015 3:24 PM
  • Thanks, thats a bummer.

    Is it difficult to implement? We really would like to have an audit of last login's.

    Tuesday, December 15, 2015 3:34 PM
  • There is some setup involved but it is not that difficult to implement.  Here are the basic steps:

    - Create a POCO that will contain the data you want to log.  I'm assuming you will log this to a database table that is part of the LS model.  If that is not correct, the implementation will be a little different.

    - Create a web api controller that uses LS Server Application Context to get the user and create a new entry in the AppLog table, set the property values you want to log (i.e. username, fullName, LoginDateTime, etc.  You can use a Get or a Post depending on whether you care about http status codes.  A Get is easier to configure but both work.  Test your web api in fiddler or browser tools and make sure it works.  You will also need a Global.asax and possibly a WebApiConfig file to set up the api routing.  I am assuming you know how to do that.

    - Go to the Scripts folder of the html client project.  Add a new javascript file.  Name it myModule.js or whatever you want.  Drag a reference to the script into the Default.htm such that it is below all of the default scripts but before the document ready function. 

    - Create a js function that runs when the script loads and creates your namespace.  You can execute an ajax call to the web api from here that will log the user.

    var myModule = this.myModule || {}; (function ($, module, undefined) { var user = {}; var logUser = function () { $.ajax({ type: 'GET', url: '../api/Users/LogUser/', success: function (data) { // do something on success // you could return an object with the user data user = data; }, error: function (jqXHR, textStatus, errorThrown) { // do something on error } }); }; // execute logUser at startup (function () { logUser(); })(); // expose the function and the user object so they can be called in screen code if you want module.logUser = logUser;

    module.user = user; })(jQuery, myModule);

    I use a module like this that has all of my reusable javascript code, including any code I would call from a render or post_render method.  It is very handy because you can reduce most of your actual screen code to a one-liner.  For example in a screen render function you can do: myModule.renderCustomMenuTile(element, contentItem); instead of writing that same code in a bunch of different screens.




    • Edited by Hessc Friday, December 18, 2015 7:33 PM
    • Proposed as answer by Angie Xu Thursday, December 24, 2015 11:40 AM
    • Marked as answer by Angie Xu Thursday, December 24, 2015 11:40 AM
    Wednesday, December 16, 2015 1:40 PM