locked
How can I control the amount of users that access my LightSwitch web application (silverlight) RRS feed

  • Question

  • I want to limit to 3 simultaneous users
    Thursday, May 16, 2013 8:18 PM

Answers

  • Browser based will be hard as you need to fire an event for the system to work. On a web browser they can just close the browser. In that case I would look at timing out the connections and removing them after a small period of time. Then inside your code each time the user wants to grab data you could randomly check that they are still in the logins table.
    • Marked as answer by Marden LR Monday, May 20, 2013 1:57 PM
    Sunday, May 19, 2013 9:18 PM

All replies

  • Make it so the application requires the user to log on and then put some code around the logon events to restrict the number there. Then each time the user does an action that requires authentication or permisions you could do the security check there as well.
    Thursday, May 16, 2013 10:28 PM
  • Hi Glenn,

    I wanted to do it this way,
    every time the User logs I record the name, date and time the log on a table,
    if he logs on to another place with the same User I record again
    up to a limit of 3 rows in this table.
    This scenario I can already do and check if the table contains up to 3 records.

    now I need to delete the record from this table when the User exit the system,
    either by clicking the button off or closing the browser in [x], the problem is that in
    a web application I do not have an event to tell me when closing the application
    this occurring.

    how could I implement this logic?

    thank you

    Friday, May 17, 2013 1:45 AM
  • Browser based will be hard as you need to fire an event for the system to work. On a web browser they can just close the browser. In that case I would look at timing out the connections and removing them after a small period of time. Then inside your code each time the user wants to grab data you could randomly check that they are still in the logins table.
    • Marked as answer by Marden LR Monday, May 20, 2013 1:57 PM
    Sunday, May 19, 2013 9:18 PM
  • Thanks, I'll try to implement a solution
    Monday, May 20, 2013 1:57 PM
  • A bit late to this thread, but how about a simple timer based callback to the server to register that you are logged in?

    - create a timer in Application.cs activated from the home screen

    - use the command-table patter (or similar) to ping the server every one minute or so to say "I'm logged in"

    - record the pings in a central table (one record per username with the datetime of the ping updated)

    - during login, you verify the number of users with active pings (i.e. had their ping record updated in the last 2 minutes or so) and reject the login if you have 3 or more active ping records

    You might have to play with the time settings and allow some grace to make it fail proof, but I think it could work.


    Regards, Xander

    Wednesday, May 22, 2013 1:54 AM
  • A bit late to this thread, but how about a simple timer based callback to the server to register that you are logged in?

    - create a timer in Application.cs activated from the home screen

    - use the command-table patter (or similar) to ping the server every one minute or so to say "I'm logged in"

    - record the pings in a central table (one record per username with the datetime of the ping updated)

    - during login, you verify the number of users with active pings (i.e. had their ping record updated in the last 2 minutes or so) and reject the login if you have 3 or more active ping records

    You might have to play with the time settings and allow some grace to make it fail proof, but I think it could work.


    Regards, Xander

    Hi novascape,
    very interesting your idea. thank you very much for your help.
    you have some code example of how to implement this idea.
    Wednesday, May 22, 2013 4:38 PM
  • Sorry about the delay in responding. I assume you mean a code example for the timer part as the command-table-pattern is well covered in other threads and blog posts and I don't have any code for the server side to maintain the logged-in user records, but you can easily figure that one out I would think.

    Here is the timer code you need to add to your Application.cs:

    using System.Threading; // include this for the Timer class

    public partial class Application
    {
    private Timer _timer;

    public void InitiatePingTimer() { if (_timer == null) { // ping interval int intervalInSeconds = 15; // change to a suitable interval _timer = new Timer(x => { if (!_timerIsActive) return; try { this.Details.Dispatcher.Invoke(() => { // insert your command-table-pattern calling code here to ping the server }); } catch (Exception) { _timerIsActive = false; throw; } }, null, intervalInSeconds * 1000, intervalInSeconds * 1000); // start in dueTime seconds and run every interval seconds } } }

    You can then initiate the above timer by making the following call in the _Activated() event of your home screen:

    partial void HomeScreen_Activated()
    {
    	// ensure ping to server operation is initiated (ok to call multiple times)
    	Application.InitiatePingTimer();
    }

    Hope this helps



    Regards, Xander

    Friday, May 24, 2013 12:13 AM
  • Just to add on, the methods already suggested are fine, but involve a lot of unneeded calls to the server, and if you are using the command pattern, you'll find you need to save the screen data to get the command to fire, which will then save any changes the user in making, even if they don't want to save just now. If they have any validation errors, they won't be able to save, and the command won't fire.

    However, if you're using either the HTML client or the VS2012 version of the Silverlight client, you can use SignalR which is built for exactly the kind of scenario you want. It manages the connections, sends messages around, lets you know when someone connects and disconnects and doesn't require the command table pattern.

    I only came across it recently myself, but I'm amazed how easy it is to use, and how well it works. It will work with just about anything, including plain HTML pages, MVC, .NET, Windows Phone, Silverlight 5 (not 4 unfortunately), and even console apps!

    There is a brilliant free ebook here which will get you started. I think you'll find that this will provide a much cleaner answer to your problem.

    Hope this helps.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Friday, May 24, 2013 1:42 PM
  • SignalR will indeed be superior. I have a developed a real-time chat application with this in recent times and it is amazing. It does however depend on the skill of the developer. A simple WebAPI is also an option, but the advantage of the command-table pattern is that you can do it out-of-the-box in LS as it were.

    However, in terms of the proposal above I should have made it clear that the timer is not part of any screen, it only gets activated from the home screen. It should also use it's own DataWorkspace to send the commands to the server so as to not interfere or save with any screen data. 

    We in fact use this mechanism on one of our large LS applications to ensure the session does not time out when users leave the application idling for a long time.

    Here is a sample of what the actual message sending code would look like (you could wrap this in a separate method for reuse of just insert it in the placeholder I indicated in the above code):

    using (var dws = new DataWorkspace())
    {
        var msg = dws.Main.ServerCommands.AddNew();
        msg.Command = (int)MyServerMessages.Ping;
        msg.Data = null;
        dws.Main.SaveChanges();
        var result = msg.Result;
        ...
    }

    The above assumes you have the following three properties on your ServerCommand entity: 

    Command - integer command value

    Data - string property to pass additional data

    Result - string property to return data

    You can get more complex by using binary Data and Result properties with a Silverlight Serializer to pass and receive more complex data, but that is probably outside of the scope of this thread. 


    Regards, Xander

    • Edited by novascape Friday, May 24, 2013 11:21 PM typo
    Friday, May 24, 2013 11:20 PM
  • Much thanks guys for all the tips.
    I am very interested in this SignalR, seems to be what I'm looking for.
    It would be of immense help someone who has already used. for example a chat application.
    could put examples of how to use SignalR with Lightswitch (silverlight) on a chat application.

    again thank you all for the help

    I am using visual studio 2012

    Monday, May 27, 2013 5:30 AM
  • My chat application does not work with LightSwitch or Silverlight. It actually works with pure Javascript on the external client side and .NET WinForms on the inside and is too big and complex an application to be of use as a starter example. In addition there are IP related issues around the code.

    The ebook mentioned by Mr Yossu, as well as the numerous SignalR examples included in the SignalR tests, as well as bloggs about in general on the internet would be far better for you to learn from.


    Regards, Xander

    Monday, May 27, 2013 5:35 AM
  • Just to echo Xander's words, I would start off with that ebook. I tried quite a lot of SignalR samples before I found that, and found they were fine for web pages, but didn't help for anything else. That ebook was unusual in that it explained how the library actually works, and gives samples in all sorts of situations.

    The majority of the book does focus on web page-based cases, but it's worth trying these out, as it gives you an idea of how SignalR works. When you're happy with that, the sections on using it with .NET, etc become a lot easier to understand.

    As far as using it with Lightswitch, the SL client in VS2012 is SL5, which is fine. You just connect to the hub when you LS app starts, and then send and receive messages like in any other SL or .NET app. This will all become clearer when you've read the book.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Monday, May 27, 2013 1:17 PM