2

How do I add Log Out Button like change password button at the top right corner? If the answer is I can't, how do I add in screen command bar?

Chris Cook
  • 2,821
  • 1
  • 20
  • 32
Aung Thiha
  • 1,195
  • 3
  • 13
  • 23

2 Answers2

0

Whilst I've not tackled the Silverlight side of things for a while (having focused more on the HTML 5 LightSwitch route) probably the easiest approach in the Desktop client type is to implement a quit option (which in essence brute-forces the Logout).

If you're using a button you'd do this with the following code: -

partial void Quit_Execute()
{
    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
    {
        System.Windows.Application.Current.MainWindow.Close();
    });
}

Or, if you'd rather have a Quit menu option, add a 'place-holder' screen to your project (a New Data Screen will do) and write the following code in the screen's Run method (located in the DesktopClient\UserCode\Application.cs class file): -

partial void Quit_Run(ref bool handled)
{
    // Set handled to 'true' to stop further processing.
    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
    {
        System.Windows.Application.Current.MainWindow.Close();
    });
    handled = true;
}

The above approach could also be extended by using the following code to immediately re-load the desktop client and automatically re-display the login screen: -

partial void Quit_Run(ref bool handled)
{
    // Set handled to 'true' to stop further processing.
    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
    {
        System.Windows.Application.Current.MainWindow.Close();
        dynamic shell = System.Runtime.InteropServices.Automation.AutomationFactory.CreateObject("Shell.Application");
        shell.ShellExecute(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs) + "\\" + System.Windows.Deployment.Current.OutOfBrowserSettings.ShortName.Split('.')[0] + ".appref-ms");
    });
    handled = true;
}

I suspect the above is the closest possible approach to achieving your requirements.

The reason I say this is that, having previously delved into the possibility of re-displaying the login screen, the best I could achieve was the following incomplete approach: -

public partial class Application
{
    System.Windows.Controls.Frame _rootFrame;
    Microsoft.LightSwitch.Runtime.Shell.Internal.Implementation.LoginPage _loginPage;

    partial void Application_Initialize()
    {
        Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
        {
            _rootFrame = ((System.Windows.Controls.Page)((System.Windows.Controls.ContentPresenter)System.Windows.Application.Current.RootVisual).Content).Content as System.Windows.Controls.Frame;
            if (_rootFrame != null)
            {
                _rootFrame.Navigated += new System.Windows.Navigation.NavigatedEventHandler(RootFrame_Navigated);
            }
        });
    }

    void RootFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        if (e.Content is Microsoft.LightSwitch.Runtime.Shell.Internal.Implementation.LoginPage)
        {
            _loginPage = (Microsoft.LightSwitch.Runtime.Shell.Internal.Implementation.LoginPage)e.Content;
            _rootFrame.Navigated -= RootFrame_Navigated;
        }
    }

    partial void Quit_Run(ref bool handled)
    {
        // Set handled to 'true' to stop further processing.
        Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
        {
            if (System.Runtime.InteropServices.Automation.AutomationFactory.IsAvailable == true) 
            {
                var loginViewModel = (Microsoft.LightSwitch.Runtime.Shell.ViewModels.Implementation.Login.LoginViewModel)_loginPage.DataContext;
                loginViewModel.UserName = "";
                loginViewModel.Password = "";
                _rootFrame.Content = _loginPage;
            }
        });
        handled = true;
    }
}

Unfortunately, whilst the above code (implemented in the UserCode\Application.cs file) does re-display the login screen and allow the username and password to be blanked via its view model, the screen isn't functional and I wasn't able to determine how to successfully re-activate it.

Maybe someone with a more in-depth knowledge of this area would be able to successfully implement this type of approach (if anyone would like to try, I'll happily provide a bit more background regarding why the screen isn't functional).

Chris Cook
  • 2,821
  • 1
  • 20
  • 32
  • It exists the application. What I want is to log the user out so that user can log in with different user account without exiting the application. thanks for answering. – Aung Thiha Mar 26 '15 at 07:46
  • I've updated my answer to include details of a further option, which may be an acceptable compromise, alongside some additional details regarding the issues involved in trying to address this requirement – Chris Cook Apr 04 '15 at 22:26
  • @AungThiha Did the extended (quit and then immediately re-load to display the login screen) approach covered in my edit above do the trick? – Chris Cook Jan 02 '16 at 03:11
0

Here's how I did it the simplest way I could find.

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.

Stefan
  • 3
  • 2
  • I can't use your solution in my desktop application. Your solution is for HTML application if I may not mistaken. – Aung Thiha Mar 31 '15 at 17:33
  • It applies to the silverlight Lightswitch Desktop Client run in a browser. Sorry, I do not know of a solution for client type: Desktop – Stefan Mar 31 '15 at 18:15