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; } } }
private static void AuthenticateRetryAndUpdateCacheSync(SingleSignOn sso, MSNTicket ticket, string sha256key, int retryCount) { // NO TICKET. WE NEED SYNC CALL! // DILEMMA: // 1 - We need this ticket (absolutely) // 2 - What we do if connection error occured! // ANSWER: Try 3 times if it is soft error. int retries = retryCount; do { try { sso.Authenticate(ticket); AddToCacheWithLock(ticket); break; } catch (AuthenticationException authExc) { DeleteFromCacheWithLock(sha256key); throw authExc; } catch (MSNPSharpException msnpExc) { if (msnpExc.InnerException == null) { DeleteFromCacheWithLock(sha256key); throw msnpExc; } WebException webExc = msnpExc.InnerException as WebException; if (webExc == null) { DeleteFromCacheWithLock(sha256key); throw msnpExc.InnerException; } // Handle soft errors switch (webExc.Status) { case WebExceptionStatus.ConnectionClosed: case WebExceptionStatus.KeepAliveFailure: case WebExceptionStatus.ReceiveFailure: case WebExceptionStatus.SendFailure: case WebExceptionStatus.Timeout: case WebExceptionStatus.UnknownError: { // Don't delete the ticket from cache retries--; break; } default: { DeleteFromCacheWithLock(sha256key); throw msnpExc.InnerException; } } } }while (retries > 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; } } }
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; } } }
private static void AuthenticateRetryAndUpdateCacheSync(SingleSignOn sso, MSNTicket ticket, string sha256key, int retryCount) { // NO TICKET. WE NEED SYNC CALL! // DILEMMA: // 1 - We need this ticket (absolutely) // 2 - What we do if connection error occured! // ANSWER: Try 3 times if it is soft error. int retries = retryCount; do { try { sso.Authenticate(ticket); AddToCacheWithLock(ticket); break; } catch (AuthenticationException authExc) { DeleteFromCacheWithLock(sha256key); throw authExc; } catch (MSNPSharpException msnpExc) { if (msnpExc.InnerException == null) { DeleteFromCacheWithLock(sha256key); throw msnpExc; } WebException webExc = msnpExc.InnerException as WebException; if (webExc == null) { DeleteFromCacheWithLock(sha256key); throw msnpExc.InnerException; } // Handle soft errors switch (webExc.Status) { case WebExceptionStatus.ConnectionClosed: case WebExceptionStatus.KeepAliveFailure: case WebExceptionStatus.ReceiveFailure: case WebExceptionStatus.SendFailure: case WebExceptionStatus.Timeout: case WebExceptionStatus.UnknownError: { // Don't delete the ticket from cache retries--; break; } default: { DeleteFromCacheWithLock(sha256key); throw msnpExc.InnerException; } } } } while (retries > 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; } } }