コード例 #1
0
        internal static void RenewIfExpired(NSMessageHandler nsMessageHandler, SSOTicketType renew)
        {
            CheckCleanup();

            if (nsMessageHandler == null || nsMessageHandler.Credentials == null)
            {
                return;
            }

            string authUser     = nsMessageHandler.Credentials.Account;
            string authPassword = nsMessageHandler.Credentials.Password;

            if (String.IsNullOrEmpty(authUser) || String.IsNullOrEmpty(authPassword))
            {
                return;
            }

            string      sha256key = MSNTicket.ComputeSHA(authUser, authPassword);
            MSNTicket   ticket    = GetFromCacheOrCreateNewWithLock(sha256key, nsMessageHandler.Credentials);
            ExpiryState es        = ticket.Expired(renew);

            if (es == ExpiryState.NotExpired)
            {
                nsMessageHandler.MSNTicket = ticket;
            }
            else if (es == ExpiryState.Expired || es == ExpiryState.WillExpireSoon)
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Re-new ticket: " + renew, "SingleSignOnManager");

                SingleSignOn sso = new SingleSignOn(nsMessageHandler, ticket.Policy);

                sso.AddAuths(renew);

                if (es == ExpiryState.WillExpireSoon)
                {
                    nsMessageHandler.MSNTicket = ticket;

                    // The ticket is in cache but it will expire soon.
                    // Do ASYNC call.
                    sso.Authenticate(ticket,
                                     delegate(object sender, EventArgs e)
                    {
                        AddToCacheWithLock(ticket);
                    },
                                     delegate(object sender, ExceptionEventArgs e)
                    {
                        DeleteFromCacheWithLock(sha256key);
                    }
                                     );
                }
                else
                {
                    // The ticket expired but we need this ticket absolutely.
                    // Do SYNC call.
                    AuthenticateRetryAndUpdateCacheSync(sso, ticket, sha256key, 3);

                    nsMessageHandler.MSNTicket = ticket;
                }
            }
        }
コード例 #2
0
        internal static void Authenticate(
            NSMessageHandler nsMessageHandler,
            string policy,
            EventHandler onSuccess,
            EventHandler <ExceptionEventArgs> onError)
        {
            CheckCleanup();

            if (nsMessageHandler == null || nsMessageHandler.Credentials == null)
            {
                return;
            }

            string authUser     = nsMessageHandler.Credentials.Account;
            string authPassword = nsMessageHandler.Credentials.Password;

            if (String.IsNullOrEmpty(authUser) || String.IsNullOrEmpty(authPassword))
            {
                return;
            }

            string    sha256key = MSNTicket.ComputeSHA(authUser, authPassword);
            MSNTicket ticket    = GetFromCacheOrCreateNewWithLock(sha256key, nsMessageHandler.Credentials);

            SSOTicketType[] ssos           = (SSOTicketType[])Enum.GetValues(typeof(SSOTicketType));
            SSOTicketType   expiredtickets = SSOTicketType.None;

            foreach (SSOTicketType ssot in ssos)
            {
                if (ExpiryState.NotExpired != ticket.Expired(ssot))
                {
                    expiredtickets |= ssot;
                }
            }

            if (expiredtickets == SSOTicketType.None)
            {
                nsMessageHandler.MSNTicket = ticket;

                if (onSuccess != null)
                {
                    onSuccess(nsMessageHandler, EventArgs.Empty);
                }
            }
            else
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Request new tickets: " + expiredtickets, "SingleSignOnManager");

                SingleSignOn sso = new SingleSignOn(nsMessageHandler, policy);
                sso.AddAuths(expiredtickets);

                // ASYNC
                if (onSuccess != null && onError != null)
                {
                    try
                    {
                        sso.Authenticate(ticket,
                                         delegate(object sender, EventArgs e)
                        {
                            try
                            {
                                AddToCacheWithLock(ticket);

                                // Check Credentials again. Owner may sign off while SSOing.
                                if (nsMessageHandler.Credentials != null &&
                                    nsMessageHandler.Credentials.Account == authUser &&
                                    nsMessageHandler.Credentials.Password == authPassword &&
                                    nsMessageHandler.IsSignedIn == false)
                                {
                                    NSMessageProcessor nsmp = nsMessageHandler.MessageProcessor as NSMessageProcessor;

                                    if (nsmp != null && nsmp.Connected)
                                    {
                                        nsMessageHandler.MSNTicket = ticket;

                                        onSuccess(nsMessageHandler, e);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                DeleteFromCacheWithLock(sha256key);
                                onError(nsMessageHandler, new ExceptionEventArgs(ex));
                            }
                        },
                                         delegate(object sender, ExceptionEventArgs e)
                        {
                            DeleteFromCacheWithLock(sha256key);
                            onError(nsMessageHandler, e);
                        });
                    }
                    catch (Exception error)
                    {
                        DeleteFromCacheWithLock(sha256key);
                        onError(nsMessageHandler, new ExceptionEventArgs(error));
                    }
                }
                else
                {
                    // SYNC
                    AuthenticateRetryAndUpdateCacheSync(sso, ticket, sha256key, 3);

                    nsMessageHandler.MSNTicket = ticket;
                }
            }
        }