none
LogOut in LightSwitch 2013 - Silverlight version RRS feed

  • Question

  • How can I enable logout functionality in LightSwitch 2013 Silverlight application? I found one option (http://goo.gl/pgqbMh), but does not seem straightforward! Does LightSwitch not have this functionality by default? Is there any better option?
    Tuesday, February 10, 2015 9:25 PM

Answers

  • Here's how I did it the simplest way I could find.  It takes from some of the tutorials you've seen mentioned:

    Go to your YourProject.server project, right-click and click "Add--> New Item"

    Select "Web Form", name it "SLLogOut.aspx"

    Replace what is in the newly created aspx file with the following:

    <%@ Page Language="VB" %>
    
    <script runat="server">
        Protected Overrides Sub OnLoad(e As EventArgs)
            FormsAuthentication.SignOut()
            Session.Abandon()
    
            ' clear authentication cookie
            Dim cookie1 As New HttpCookie(FormsAuthentication.FormsCookieName, "")
            cookie1.Expires = DateTime.Now.AddYears(-1)
            Response.Cookies.Add(cookie1)
    
            ' clear session cookie 
            Dim cookie2 As New HttpCookie("ASP.NET_SessionId", "")
            cookie2.Expires = DateTime.Now.AddYears(-1)
            Response.Cookies.Add(cookie2)
    
            Response.Redirect("DesktopClient/default.htm")
        End Sub
    </script>
    
    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Log out</title>
    </head>
    <body>
    </body>
    </html>

    NOTE: Take note of the "Redirect".  You may need to modify the URL depending on the name of your program.

    In the solution explorer, next to "SLLogOut.aspx", click the arrow to expand it.  

    Delete both "SLLogOut.aspx.designer.vb" and delete "SLLogOut.aspx.vb"

    Create a button on the screen of your choice.  For its execute code, use the following:

                Dispatchers.Main.Invoke(Sub()
                                            System.Windows.Browser.HtmlPage.Window.Navigate(New Uri("../SLLogOut.aspx", UriKind.Relative))
                                        End Sub)

    If there's even a chance that you will ever use this app out-of-browser, then you should add can_execute code to disable this button.  You can find that code easy enough in the other tutorials related to logout buttons in the SL web client.  I'm just giving you the simplest way I could find to accomplish it.  Tested in both chrome and IE.


    • Edited by StefanVon Wednesday, February 11, 2015 3:44 PM
    • Proposed as answer by StefanVon Friday, February 13, 2015 2:05 AM
    • Marked as answer by Arijit D Friday, February 13, 2015 2:55 PM
    Wednesday, February 11, 2015 3:43 PM

All replies

  • You can follow the steps Matt mentioned to create a logout function. If you want to have this function by default, how about submit this request to Visual Studio Uservoice site via LightSwitch portal, this site is for suggestions and ideas.
    Wednesday, February 11, 2015 11:40 AM
  • Here's how I did it the simplest way I could find.  It takes from some of the tutorials you've seen mentioned:

    Go to your YourProject.server project, right-click and click "Add--> New Item"

    Select "Web Form", name it "SLLogOut.aspx"

    Replace what is in the newly created aspx file with the following:

    <%@ Page Language="VB" %>
    
    <script runat="server">
        Protected Overrides Sub OnLoad(e As EventArgs)
            FormsAuthentication.SignOut()
            Session.Abandon()
    
            ' clear authentication cookie
            Dim cookie1 As New HttpCookie(FormsAuthentication.FormsCookieName, "")
            cookie1.Expires = DateTime.Now.AddYears(-1)
            Response.Cookies.Add(cookie1)
    
            ' clear session cookie 
            Dim cookie2 As New HttpCookie("ASP.NET_SessionId", "")
            cookie2.Expires = DateTime.Now.AddYears(-1)
            Response.Cookies.Add(cookie2)
    
            Response.Redirect("DesktopClient/default.htm")
        End Sub
    </script>
    
    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Log out</title>
    </head>
    <body>
    </body>
    </html>

    NOTE: Take note of the "Redirect".  You may need to modify the URL depending on the name of your program.

    In the solution explorer, next to "SLLogOut.aspx", click the arrow to expand it.  

    Delete both "SLLogOut.aspx.designer.vb" and delete "SLLogOut.aspx.vb"

    Create a button on the screen of your choice.  For its execute code, use the following:

                Dispatchers.Main.Invoke(Sub()
                                            System.Windows.Browser.HtmlPage.Window.Navigate(New Uri("../SLLogOut.aspx", UriKind.Relative))
                                        End Sub)

    If there's even a chance that you will ever use this app out-of-browser, then you should add can_execute code to disable this button.  You can find that code easy enough in the other tutorials related to logout buttons in the SL web client.  I'm just giving you the simplest way I could find to accomplish it.  Tested in both chrome and IE.


    • Edited by StefanVon Wednesday, February 11, 2015 3:44 PM
    • Proposed as answer by StefanVon Friday, February 13, 2015 2:05 AM
    • Marked as answer by Arijit D Friday, February 13, 2015 2:55 PM
    Wednesday, February 11, 2015 3:43 PM
  • Thanks a lot Stefan! I did something similar by reusing the 'LogOut.aspx' page that comes with 'server' project... my understanding is this page is being used for the HTMLClient log out functionality. It was not working properly because of a URL typo in my code, after seeing your sample I fixed that. But, I also see that the LogOut.aspx does not kill the cookies that your sample does (and also recommended here - http://goo.gl/84WjIf). So, I ended up using your code exactly as is!

    My next question, how do I place the logout link in the top of the page like the way 'Change Password' shows up as below image, instead of adding button in each screen? Is there any master page concept in LightSwitch?

     

    Thanks again!

    Thursday, February 12, 2015 8:57 PM
  • Thanks, I just posted a suggestion there!
    Thursday, February 12, 2015 8:57 PM
  • Glad I could help!  I've seen the same question asked about putting a link in the ribbon next to "change password",but I've not seen a solution. If you found my post helpful, please mark it as the answer. Thank you! 
    Friday, February 13, 2015 2:05 AM
  • Hi Arijit

    You have to develop your own custom lightswitch shell to implement the logout link.

    Walkthrough: Creating a Shell Extension

    Regards


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Friday, February 13, 2015 5:27 AM
  • Thanks Rashmi, will try the Shell Extension route.
    Friday, February 13, 2015 4:58 PM