Ejemplo n.º 1
0
        /// <summary>
        /// Wraps the specified action so the <see cref="AuthenticationService"/> can complete
        /// processing of the operation before invoking the <paramref name="completeAction"/>.
        /// </summary>
        /// <typeparam name="T">The type of operation.</typeparam>
        /// <param name="completeAction">The action to invoke once the service finishes
        /// processing the operation. This parameter is optional.
        /// </param>
        /// <returns>An action that will complete processing of the operation before invoking
        /// the wrapped action.
        /// </returns>
        private Action <T> WrapCompleteAction <T>(Action <T> completeAction) where T : AuthenticationOperation
        {
            return(new Action <T>(ao =>
            {
                bool raiseUserChanged = false;
                bool raiseLoggedIn = false;
                bool raiseLoggedOut = false;

                // If the operation completed successfully, update the user and
                // determine which events should be raised
                if (!ao.IsCanceled && !ao.HasError && (ao.User != null))
                {
                    if (this._user != ao.User)
                    {
                        raiseLoggedIn =
                            // anonymous -> authenticated
                            (this._user == null) ||
                            (!this._user.Identity.IsAuthenticated && ao.User.Identity.IsAuthenticated) ||
                            // authenticated -> authenticated
                            (ao.User.Identity.IsAuthenticated && (this._user.Identity.Name != ao.User.Identity.Name));
                        raiseLoggedOut =
                            // authenticated -> anonymous
                            (this._user != null) &&
                            (this._user.Identity.IsAuthenticated && !ao.User.Identity.IsAuthenticated);

                        this._user = ao.User;
                        raiseUserChanged = true;
                    }
                }

                // Setting the operation to null indicates the service is no longer busy and
                // can process another operation
                this.Operation = null;

                // Invoke the wrapped action
                if (completeAction != null)
                {
                    try
                    {
                        completeAction.DynamicInvoke(ao);
                    }
                    catch (TargetInvocationException tie)
                    {
                        if (tie.InnerException != null)
                        {
                            throw tie.InnerException;
                        }
                        throw;
                    }
                }

                // Raise notification events as appropriate
                if (raiseUserChanged)
                {
                    this.RaisePropertyChanged(nameof(User));
                }
                this.RaisePropertyChanged(nameof(IsBusy));
                this.RaisePropertyChanged(AuthenticationService.GetBusyPropertyName(ao));

                if (raiseLoggedIn)
                {
                    this.OnLoggedIn(new AuthenticationEventArgs(ao.User));
                }
                if (raiseLoggedOut)
                {
                    this.OnLoggedOut(new AuthenticationEventArgs(ao.User));
                }
            }));
        }
Ejemplo n.º 2
0
 internal SaveUserOperation(AuthenticationService service, Action <SaveUserOperation> completeAction, object userState) :
     base(service, userState)
 {
     this._completeAction = completeAction;
 }
 internal LoginOperation(AuthenticationService service, LoginParameters loginParameters, Action <LoginOperation> completeAction, object userState) :
     base(service, userState)
 {
     this._loginParameters = loginParameters;
     this._completeAction  = completeAction;
 }