locked
Log off from a Ligthswitch Silverlight Browser application RRS feed

  • Question

  • Hi all. I've run into the following problem:

    My users have a shared PC where 2 or 3 people use the LS app at different times of the day. In a LS desktop application, when the user closes the app, the next time he opens it he'll be prompted for credentials. When I changed the app type to a browser silverlight app, it just prompted for credentials the first time, and now every time they open the page, the system will automatically log them on.

    Is there a way to tell LS to not remember the user for the sliverlight browser app? Or better yet, is there an option to include a log off button?

    I'm working on LS for VS2013.

    Thanks a lot.


    :. HUGONNE .: Please mark as answer if the post helps. Coding tips - http://hugonne.blogspot.com/

    Saturday, August 23, 2014 2:41 PM

Answers

  • Hi Hugonne, follow this steps to add a logout button:

    1. Enable Forms Authentication in your SL browser application

    2. In your home screen create a button, let's call it "LogOut"

    3. Add a System.Windows.Browser reference into your Client project

    4. Write the code below for "LogOut" button "Execute":

    using System;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Framework.Client;
    using Microsoft.LightSwitch.Presentation;
    using Microsoft.LightSwitch.Presentation.Extensions;
    using Microsoft.LightSwitch.Security;
    using Microsoft.LightSwitch.Threading;
    using System.Windows.Browser;
    namespace LightSwitchApplication
    {
        public partial class Welcome
        {
            partial void LogOut_Execute()
            {
                
                Dispatchers.Main.Invoke(() =>
                {
                    HtmlPage.Window.Navigate(new Uri(@"../LogOff.aspx", UriKind.Relative));
                    
                });
    
            }
    
            partial void LogOut_CanExecute(ref bool result)
            {
                
                bool res = false;
    
                Dispatchers.Main.Invoke(() =>
                {
                    res = (System.Windows.Application.Current.IsRunningOutOfBrowser == false)
                        && (Application.Current.User.AuthenticationType == AuthenticationType.Forms);
                });
    
                result = res;
            }
        }
    }

    5. Add an ASP.NET form to the Server Project and call it Logoff.aspx. Paste this code to Logoff.aspx code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace LightSwitchApplication
    {
        public partial class LogOff : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                FormsAuthentication.SignOut();
                Session.Abandon();
    
                // clear authentication cookie
                HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                cookie1.Expires = DateTime.Now.AddYears(-1);
                Response.Cookies.Add(cookie1);
    
                // clear session cookie 
                HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
                cookie2.Expires = DateTime.Now.AddYears(-1);
                Response.Cookies.Add(cookie2);
    
                Response.Redirect("Client/default.htm");
            }
        }
    }

    HTH.


    Nicolás Lope de Barrios
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.


    Sunday, August 24, 2014 9:34 PM