I am using .NET DirectoryServices and DirectoryServices.AccountManagement in my C# CLR code. I want to publish to my SQL Server database as an Unsafe CLR stored procedure. In Visual Studio, I signed my CLR project. I want to avoid turning Trustworthy ON. How do I also sign the assemblies? I know this sounds pretty simple, but I have searched 4 hours on google without coming up with an answer. I thought I was pretty close when I downloaded Stairway to CLR, but couldn't find the answer there.
What Does Unsafe Mean? Unsafe is a C programming language (C#) keyword used to denote a section of code that is not managed by the Common Language Runtime (CLR) of the . NET Framework, or unmanaged code. Unsafe is used in the declaration of a type or member or to specify block code.
Unsafe code in C# is the part of the program that runs outside the control of the Common Language Runtime (CLR) of the . NET frameworks. The CLR is responsible for all of the background tasks that the programmer doesn't have to worry about like memory allocation and release, managing stack etc.
C# is typically slower than C++. There are runtime checks in managed code. These are what make it managed, after all. C++ doesn't have to check whether the bounds of an array have been exceeded for example.
The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.
First: Thank you for not going the easy route and enabling TRUSTWORTHY
. Seriously. It is not that much work to do this correctly, so I (and your employer) appreciate you doing more research, and when you couldn't get any farther, reaching out for help 😺.
If you are referring to the "Stairway to SQLCLR" series on SQL Server Central, I am the author of those articles. I am sorry if the information was not easily understood. I will outline the steps here:
Depending on what you are doing and the overall requirements, you have some options here:
[master]
, marked as SAFE
, to create the Asymmetric Key from it (you won't execute the code here so it doesn't need to be marked as UNSAFE
here).UNSAFE
), then I find it best to create a separate, empty Assembly, also signed with the same Strong Name Key file (i.e. the .pfx
file) used for the main Assembly. I then load that, empty yet signed, Assembly into [master]
, marked as SAFE
Once you have an Assembly created in [master]
that was signed with the same .pfx
file that was used to sign your main, UNSAFE
Assembly, then you can simply create, in [master]
, an Asymmetric Key from that Assembly.
Create a Login from that Asymmetric Key
Grant that Key-based Login the UNSAFE ASSEMBLY
permission
These steps work for SQL Server 2005 - 2016, and I provide instructions on how to automate this (Option 2 above -- using a separate, empty, signed Assembly) in Visual Studio in the Stairway to SQLCLR Level 7: Development and Security article. (btw, I do realize that the steps described in Level 7 are admittedly a bit much, but they do provide a means of automation using Visual Studio AND they can be simplified by using T4 templates -- which comes with VS -- but I haven't had time yet to write up the simplified approach -- hopefully soon, though).
IF, however, you need to account for SQL Server 2017 (or newer, presumably), then the steps above will not work as not even SAFE
Assemblies can be created anymore without being signed and having the corresponding signature-based Login that has the UNSAFE ASSEMBLY
permission. And, unfortunately, CREATE ASYMMETRIC KEY
does not allow for creating from a hex bytes string / VARBINARY
literal, so you need to use a Certificate which does allow for being created that way. Along those lines, I have two blog posts which detail, step-by-step, how to accomplish this, fully automated via Visual Studio (or even not using Visual Studio):
Whatever you do, do not get fooled into using the new "Trusted Assemblies" feature of SQL Server 2017 as there are numerous problems with it, as described here: SQLCLR vs. SQL Server 2017, Part 4: “Trusted Assemblies” – The Disappointment and one more post that I am working on now.
To help improve this needlessly painful process, please support my enhancement requests to improve CREATE ASYMMETRIC KEY
and SSDT:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With