ASP.NET MVC: how to allow users or client IP?

In an ASP.NET MVC application, I would like certain controllers to be available only to authorized users. AuthorizeAttribute is great for this. However, I would also like these controllers to provide access to specific IP numbers, even if the remote user is unauthorized. Should I override AuthorizeAttribute to provide this function or is there a better solution?

+4
source share
2 answers

Your instincts are correct - the extension of the authorization attribute makes the most sense in this case.

+2
source

Keep in mind that if you add both AuthorizeAttribute with users and your own ClientIPAuthorizeAttribute with the client IP address, unauthorized users at the specified IP address will not be allowed ...

This is because individual AuthorizeAttribute cannot communicate with each other to make a logical OR . Subclassification is the easiest and cleanest way to achieve what you want.

+3
source

All Articles