There are financial and legal reasons why a company might not hire you if you were in this situation. There is more hassle for the company, and additional expense. They may or may not think any particular candidate is worth the additional hassle and expense. So it is a perfectly legitimate question to ask ( again, assuming it is asked in an appropritate manner ).
This is complicated a little for transferable sponsored visas like the H1B. Someone already on an H1B might reply ‘yes’ to the work authorization question, but with the caveat that you would need to take on their sponsorship. Technically, their answer should be ‘no’. They aren’t authorized to work for anyone other than their sponsoring employer, in a specific location, with a specific job title. As I say, the point is that if the answer is ‘yes’, not ‘yes but’, or ‘no’, then you’re not permitted to discriminate. Asking someone with work authorization questions about their visa type should be as much of an interview no-no as asking a candidate if she’s pregnant.
Only exception is certain jobs can place citizenship requirements because of security clearance requirements.