locked
Winforms Client SignalR login RRS feed

  • Question

  • Hi,

    I have a LS application with forms authentication.

    With my winforms client SignalR works fine, but I cannot use the ServerApplicationConext, because I do not know how to login via SignalR.

    Thanks.

    Markus

    Monday, June 3, 2013 7:10 AM

Answers

  • This may help:

    Forms Authentication with SignalR Using Web Forms and Windows Forms

    http://openlightgroup.net/Blog/TabId/58/PostId/180/signalrforms.aspx

    However when SignalR needs to talk to LightSwitch (and therefore the ServerApplicationContext, it should communicate first to the HTML JavaScript page (setup a JavaScript listener handler). JavaScript on the LightSwitch page can then make a call to a .ashx file handler (or WebAPI handler). That second call will be by the user and therefore should be authenticated).


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Monday, June 3, 2013 12:32 PM
  • In case you need additional information here - 

    You should also be able to get SignalR to work with a LightSwitch Silverlight Client, FYI. 

    And I do have an example of basic LightSwitch / SignalR integration (for the HTML Client, though Silverlight Client should be very similar)

    Thx - Matt S


    R. Matt Sampson

    Monday, June 3, 2013 3:01 PM
  • I went a different route and reproduced the problem with:

    1)LightSwitch Silverlight Client (Update 3)

    2) LightSwitch Server

    3) SignalR on Client and Server

    4) Forms authentication enabled

    5) Published

    So I think I'm seeing what you are reporting now. The currently authenticated user doesn't appear present on the HTTPContext, so the call to create a new ServerApplicationContext() fails.  The HTML Client works wonderfully here, btw- FWIW.

    First - I'm going to follow up on my side about this issue some more with others and the expected behavior. 

    Secondly - you may want to check out Matt Evan's blog about the ServerApplicationContext . Specifically the section around authentication AND the comments in the blog. You can skip authentication (which worked for me as a possible workaround) when you create the ServerApplicationContext. You could of course do your own validation then on the server to decide if you think if the user is actually the correctly authenticated user or not. I encourage you again to read through the comments on the blog, and the blurb about authentication in the blog.  Also, it sounds to me like your client is just really never correctly authenticated to our lightswitch server - I suspect your Forms Auth Cookie is possibly not on the request header before you send out the request from the client to the server. You may want to examine Michael Washington's blog again about Winforms' and SignalR on how he did that in his example.

    HTH - Matt S


    R. Matt Sampson


    Friday, June 7, 2013 3:06 PM

All replies

  • This may help:

    Forms Authentication with SignalR Using Web Forms and Windows Forms

    http://openlightgroup.net/Blog/TabId/58/PostId/180/signalrforms.aspx

    However when SignalR needs to talk to LightSwitch (and therefore the ServerApplicationContext, it should communicate first to the HTML JavaScript page (setup a JavaScript listener handler). JavaScript on the LightSwitch page can then make a call to a .ashx file handler (or WebAPI handler). That second call will be by the user and therefore should be authenticated).


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Monday, June 3, 2013 12:32 PM
  • In case you need additional information here - 

    You should also be able to get SignalR to work with a LightSwitch Silverlight Client, FYI. 

    And I do have an example of basic LightSwitch / SignalR integration (for the HTML Client, though Silverlight Client should be very similar)

    Thx - Matt S


    R. Matt Sampson

    Monday, June 3, 2013 3:01 PM
  • Thanks Matt.

    SignalR is running without any problem in the silverlight client.

    With Winforms Client it also works very well in debug mode, the only problem is the authentication if I publish the application. In debug modus the testuser is always loged in. So it is no problem.

    It seems that I have to get a cookie from lightswitch and add it to my hubconnection, but I do not now how to get the cookie.

    First I tried to create a LoginUser function in MyHub SignalR on Server and used the code:

    // Create cookie and return it
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                    strUserName,
                    DateTime.Now,
                    DateTime.Now.AddDays(30),
                    false,
                    "SignalR",
                    FormsAuthentication.FormsCookiePath);
                // Encrypt the ticket.
                string encTicket = FormsAuthentication.Encrypt(ticket);

    to create a cookie and return it to the client.

    The code creates a cookie and returns it, but after this I cannot do odata calls anymore.

    I do not think that this is the right code for lightswitch to create a cookie on server side.

    Maybe someone could get me in the right direction.

    Thanks.

    Monday, June 3, 2013 3:09 PM
  • Can you straighten me out on what kind of client you are using? Are you using a LightSwitch Silverlight Client? (I'm not familiar with a LightSwitch Winforms client...)

    R. Matt Sampson

    Monday, June 3, 2013 3:58 PM
  • Hi,

    I just use a winforms application with odata to query my data and I use SignalR for Update, Insert and Delete notifications for my winforms application.

    Thanks.

    Markus

    Monday, June 3, 2013 4:02 PM
  • So you have a winforms client application, that is querying an Odata Service.  Which part of this was developed with LightSwitch?  Just the Server/OData Service side?

    Not sure, but this blog may be a good starting place for you....

    Creating a custom login screen


    R. Matt Sampson


    Tuesday, June 4, 2013 2:21 PM
  • Hi,

    yes, just the Server/Odata Service side is developed with Lightswitch.

    I already read this blog post, but it did not help me ...

    And I do not find many things about how LS authentication works in detail ...

    But I think I have to use a workaround for my problem, because I think there is no easy way to use the ServerApplicationContext with SignalR out of Browser.

    I already tried the same with WebApi, there is also no easy way to do this.

    Thanks.

    Tuesday, June 4, 2013 2:42 PM
  • hi!

    i've tried with signalR and SL OOB app, didnt run well, SignalR has worked, but serverapplicationcontext didn't let me to perform any operation with data.. 

    Kivito


    Nobody expects the Spanish Inquisition! (M.P.F.C.)

    Tuesday, June 4, 2013 2:51 PM
  • Hi Kivito,

    did you try serverapplicationcontext from SL OOB app with LS running on your dev machine in debug mode? For me it works in debug mode, because Test User is always logged in.

    It is a little bit frustrating, because serverapplicationcontext with SignalR and WebApi would be very nice in my app ...

    Thanks.

    Tuesday, June 4, 2013 2:58 PM

  • i've tried locally and on IIS, same result - access is denied.. same code in command table pattern worked well, so i didn't waste time with that, i'm gonna play with it when i find time in simpler test project.. just to notice that i tried this in ctp4 version (following Paul van Bladel example) and it worked, so i was also confused..

    and I agree, approach with signalR was "cleaner"..

    Kivito


    Nobody expects the Spanish Inquisition! (M.P.F.C.)

    Tuesday, June 4, 2013 3:09 PM
  • I'd be happy to investigate this if anyone has a project that they can share out with me. Just reply if you are interested and I can give you my email address.

    In the meantime, I'll do my best to investigate this on my side. I definitely like SignalR and LightSwitch, so I'd like to see this scenario work.

    Seems to me that all that is needed to reproduce this problem is basically a LightSwitch app with Forms Authentication and SignalR.


    R. Matt Sampson

    Tuesday, June 4, 2013 3:25 PM
  • Hi,

    there is a downloadable example: (also with a winforms client)

    http://blog.pragmaswitch.com/?p=809

    You only have to update and change a little bit code ... then the project should work.

    Markus

    Tuesday, June 4, 2013 3:58 PM
  • Hey Dragonalw,

    I did check that project out.

    That project appears to use a Silverlight Client (not a winforms client).

    That project worked fine for me on F5 scenarios (with and without Forms Auth enabled...)

    Thx - Matt S


    R. Matt Sampson

    Thursday, June 6, 2013 3:41 PM
  • Hi,

    thanks for the reply.

    Sorry it was the false link ... Here is the one with the winforms project in it:

    http://blog.pragmaswitch.com/?p=678

    With F5 scenario you mean in debug mode on your local dev machine? If you debug your project with F5 you will be always loged in as Test User, so authentication will never be a problem ...

    The problem is you if you publish the application ... 

    Thanks.

    Markus

    Thursday, June 6, 2013 3:52 PM
  • I went a different route and reproduced the problem with:

    1)LightSwitch Silverlight Client (Update 3)

    2) LightSwitch Server

    3) SignalR on Client and Server

    4) Forms authentication enabled

    5) Published

    So I think I'm seeing what you are reporting now. The currently authenticated user doesn't appear present on the HTTPContext, so the call to create a new ServerApplicationContext() fails.  The HTML Client works wonderfully here, btw- FWIW.

    First - I'm going to follow up on my side about this issue some more with others and the expected behavior. 

    Secondly - you may want to check out Matt Evan's blog about the ServerApplicationContext . Specifically the section around authentication AND the comments in the blog. You can skip authentication (which worked for me as a possible workaround) when you create the ServerApplicationContext. You could of course do your own validation then on the server to decide if you think if the user is actually the correctly authenticated user or not. I encourage you again to read through the comments on the blog, and the blurb about authentication in the blog.  Also, it sounds to me like your client is just really never correctly authenticated to our lightswitch server - I suspect your Forms Auth Cookie is possibly not on the request header before you send out the request from the client to the server. You may want to examine Michael Washington's blog again about Winforms' and SignalR on how he did that in his example.

    HTH - Matt S


    R. Matt Sampson


    Friday, June 7, 2013 3:06 PM
  • nice post :)

    Friday, October 10, 2014 8:12 AM