The work reported in this paper aims at developing an algorithm for identifying the worst contingencies to be used in a security-constrained optimal power flow problem. A bilevel max-min optimization model is proposed to find the worst contingency scenario while solving an ac optimal power flow problem and considering corrective actions. The objective is to minimize the total cost including generation and load shedding cost while satisfying all relevant constraints. The upper-level problem allows representing contingencies using binary variables. We formulate a second-order cone problem in the lower-level to represent the ac power flow equations. Duality is used to merge the upper- and lower-level problems into a single-level one. The resulting mixed-integer second-order conic problem can be efficiently solved. Three case studies are presented as applications of this novel technique.