Export (0) Print
Expand All

AuthenticationService.Logout Method (Action<LogoutOperation>, Object)

WCF RIA Services

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

Asynchronously logs out an authenticated user from the server with the specified callback method and user state.

Namespace:  System.ServiceModel.DomainServices.Client.ApplicationServices
Assembly:  System.ServiceModel.DomainServices.Client (in System.ServiceModel.DomainServices.Client.dll)

public LogoutOperation Logout(
	Action<LogoutOperation> completeAction,
	Object userState
)

Parameters

completeAction
Type: System.Action<LogoutOperation>
An action that will be invoked immediately after the operation completes and is called in all cases, including success, cancellation, and error. This parameter is optional.
userState
Type: System.Object
The state that will be set into UserState. This parameter is optional.

ExceptionCondition
InvalidOperationException

This method is called while another asynchronous operation is still being processed.

If this method returns normally, a LoggedOut event may be raised. Also, successful completion of this operation will update the User. To handle an operation error, MarkErrorAsHandled can be called from the operation completion callback or from a Completed event handler.

The following example shows a call to the Logout method with a callback method.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    SetControlVisibility(WebContext.Current.User.IsAuthenticated);
}

private void LoginButton_Click(object sender, RoutedEventArgs e)
{
    LoginParameters lp = new LoginParameters(UserName.Text, Password.Password);
    WebContext.Current.Authentication.Login(lp, this.LoginOperation_Completed, null);
    LoginButton.IsEnabled = false;
    LoginResult.Text = "";
}

private void LoginOperation_Completed(LoginOperation lo)
{
    if (lo.HasError)
    {
        LoginResult.Text = lo.Error.Message;
        LoginResult.Visibility = System.Windows.Visibility.Visible;
        lo.MarkErrorAsHandled();
    }
    else if (lo.LoginSuccess == false)
    {
        LoginResult.Text = "Login failed. Please check user name and password.";
        LoginResult.Visibility = System.Windows.Visibility.Visible;
    }
    else if (lo.LoginSuccess == true)
    {
        SetControlVisibility(true);
    }
    LoginButton.IsEnabled = true;
}

private void SetControlVisibility(bool isAuthenticated)
{
    if (isAuthenticated)
    {
        LoginBorder.Visibility = System.Windows.Visibility.Collapsed;
        WelcomeText.Text = "Welcome " + WebContext.Current.User.Name;
        WelcomeText.Visibility = System.Windows.Visibility.Visible;
        LogoutButton.Visibility = System.Windows.Visibility.Visible;
    }
    else
    {
        LoginBorder.Visibility = System.Windows.Visibility.Visible;
        WelcomeText.Visibility = System.Windows.Visibility.Collapsed;
        LogoutButton.Visibility = System.Windows.Visibility.Collapsed;
    }
}

private void LogoutButton_Click(object sender, RoutedEventArgs e)
{
    WebContext.Current.Authentication.Logout(this.LogoutOperation_Completed, null);
}

private void LogoutOperation_Completed(LogoutOperation lo)
{

    if (!lo.HasError)
    {
        SetControlVisibility(false);
    }
    else
    {
        ErrorWindow ew = new ErrorWindow("Logout failed.", "Please try logging out again.");
        ew.Show();
        lo.MarkErrorAsHandled();
    }
}


Show:
© 2014 Microsoft