Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is HostProtectionAttribute and why do we use it?

Tags:

c#

clr

msdn

I would like to learn more about the HostProtectionAttribute in C#. Certainly, I read the MSDN documentations this and this but I couldn't figure out why we use it. In the documentation there are terms like "host programming model" which made the topic even more confusing for me.

Suppose that we have a method that has this attribute and what happens if I delete it?

like image 848
Barış Akkurt Avatar asked Dec 28 '16 07:12

Barış Akkurt


1 Answers

As the documentation spells out below;

Given these attributes, SQL Server specifies a list of HPAs that are disallowed in the hosted environment through code access security (CAS). The CAS requirements are specified by one of three SQL Server permission sets: SAFE, EXTERNAL_ACCESS, or UNSAFE. One of these three security levels is specified when the assembly is registered on the server, using the CREATE ASSEMBLY statement. Code executing within the SAFE or EXTERNAL_ACCESS permission sets must avoid certain types or members that have the System.Security.Permissions.HostProtectionAttribute attribute applied.

The HostProtectionAttribute is not a security permission as much , in that **it identifies specific code constructs, either types or methods, that the host may disallow

I couldn't figure out why we use it.

We use it to help us write predictable code in the host environment because

it identifies specific code constructs, either types or methods, that the host may disallow

Suppose that we have a method that has this attribute and what happens if I delete it?

When you delete it, in case the method contains types or certain code constructs that maybe disallowed by the host, you will never know and as a result it may result in unpredictable behavior of your code in the host environment such as SQL server. I hope that makes it more helpful.

like image 78
Chris N. Tyler Avatar answered Nov 02 '22 22:11

Chris N. Tyler