/// <summary> /// Remove o ticket. /// </summary> /// <param name="ticketId"></param> /// <returns></returns> private bool RemoveTicket(string ticketId) { if (string.IsNullOrEmpty(ticketId) || !TicketId.IsLegit(ticketId)) { return(false); } lock (_tickets) return(_tickets.Remove(ticketId)); }
/// <summary> /// Renova o ticket. /// </summary> /// <param name="ticketId"></param> /// <param name="ticket"></param> /// <returns>Identifica se a renovação foi feita com sucesso.</returns> private bool RenewTicket(string ticketId, System.Web.Security.FormsAuthenticationTicket ticket) { if (string.IsNullOrEmpty(ticketId) || !TicketId.IsLegit(ticketId)) { return(false); } lock (_tickets) if (_tickets.ContainsKey(ticketId)) { _tickets[ticketId] = ticket; } else if (!ticket.Expired) { _tickets.Add(ticketId, ticket); } else { return(false); } return(true); }
/// <summary> /// Método acionado quando for finalizada uma requisição. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnEndRequest(object sender, EventArgs e) { var application = (System.Web.HttpApplication)sender; var response = application.Context.Response; if (response.Cookies.AllKeys.Contains(System.Web.Security.FormsAuthentication.FormsCookieName)) { var ticketText = response.Cookies[System.Web.Security.FormsAuthentication.FormsCookieName].Value; var oldTicket = application.Context.Items[FormsAuthenticationTicketKey] as System.Web.Security.FormsAuthenticationTicket; System.Web.Security.FormsAuthenticationTicket ticket = null; if (!string.IsNullOrEmpty(ticketText) && ticketText != "NoCookie") { try { ticket = System.Web.Security.FormsAuthentication.Decrypt(ticketText); } catch (Exception) { } } if (oldTicket != null && (string.IsNullOrEmpty(ticketText) || ticketText == "NoCookie")) { response.Cookies.Add(new System.Web.HttpCookie(TicketIdCookieName, null) { Expires = DateTime.MinValue }); RemoveTicket(GetTicketId(application.Context.Request.Cookies)); var token = application.Context.Request.Cookies[TokenCookieName]; if (token != null) { var tokenProvider = Colosoft.Security.Tokens.Provider; if (tokenProvider != null) { tokenProvider.Close(token.Value); } } if (Logout != null) { Logout(this, new LogoutUserInfoEventArgs(GetTicketId(application.Context.Request.Cookies), oldTicket.Name, oldTicket.UserData, oldTicket.IssueDate, DateTime.Now)); } } else if (oldTicket == null && ticket != null && !ticket.Expired) { var id = TicketId.Create(ref _randgen); response.Cookies.Add(new System.Web.HttpCookie(TicketIdCookieName, id) { Expires = ticket.Expiration }); string token = application.Context.Items[FormsAuthenticationTicketKey] as string; response.Cookies.Add(new System.Web.HttpCookie(TokenCookieName, token) { Expires = ticket.Expiration }); if (Authenticated != null) { Authenticated(this, new AuthenticatedUserInfoEventArgs(id, ticket.Name, ticket.UserData, ticket.IssueDate, ticket.Expiration)); } } else if (oldTicket != null && ticket != null && !ticket.Expired) { var ticketIdCookie = application.Context.Request.Cookies[TicketIdCookieName]; string id = null; if (ticketIdCookie != null && !string.IsNullOrEmpty(ticketIdCookie.Value)) { id = ticketIdCookie.Value; } else { id = TicketId.Create(ref _randgen); } response.Cookies.Add(new System.Web.HttpCookie(TicketIdCookieName, id) { Expires = ticket.Expiration }); var token = application.Context.Request.Cookies[TokenCookieName]; if (token != null) { var tokenProvider = Colosoft.Security.Tokens.Provider; if (tokenProvider != null) { tokenProvider.Ping(token.Value); } } if (TicketUpdated != null) { TicketUpdated(this, new AuthenticatedUserInfoEventArgs(id, ticket.Name, ticket.UserData, ticket.IssueDate, ticket.Expiration)); } } if (ticket != null) { RenewTicket(GetTicketId(application.Context.Request.Cookies), ticket); } } }