Do the access levels and modifiers (private, sealed, etc) serve a security purpose in C#?

后端 未结 5 883
一生所求
一生所求 2021-01-11 17:08

I\'ve seen that you can manipulate private and internal members using reflection. I\'ve also seen it said that a \'sealed\' class is more secure that one that isn\'t.

5条回答
  •  一生所求
    2021-01-11 17:51

    Access modifiers aren't about security, but good design. Proper access levels for classes and methods drives/enforces good design principles. Reflection should, ideally, only be used when the convenience of using it provides more utility than the cost of violating (if there is one) best design practices. Sealing classes only serves the purpose of preventing developers from extending your class and "breaking" it's functionality. There are different opinions on the utility of sealing classes, but since I do TDD and it's hard to mock a sealed class, I avoid it as much as possible.

    If you want security, you need to follow coding practices that prevent the bad guys from getting in and/or protect confidential information from inspection even if a break in occurs. Intrusion prevention, intrusion detection, encryption, auditing, etc. are some of the tools that you need to employ to secure your application. Setting up restrictive access modifiers and sealing classes has little to do with application security, IMO.

提交回复
热议问题