Search code examples
sql-serversql-server-2005.net-assemblyencryption-asymmetric

Updating an Assembly with an Asymmetric Key


I have previously created an assembly with VS 2010 and signed it with a key. I ran a script like this below:

USE master; 
GO  

CREATE ASYMMETRIC KEY SQLCLRPSBatch20Key FROM EXECUTABLE FILE = 'C:\Unsafe\MyDLL.dll';   
CREATE LOGIN SQLCLRPSBatch20Login FROM ASYMMETRIC KEY SQLCLRPSBatch20Key;  
GRANT UNSAFE ASSEMBLY TO SQLCLRPSBatch20Login;
GO

use MyDB;
GO

// create assembly script here to install unsafe assembly

Now do I need to do this and create a new key and login each time I drop the assembly on the DB and reinstall to update it?

When I do that, I get:

Msg 15396, Level 16, State 1, Line 2
An asymmetric key with name 'SQLCLRPSBatch20Key' already exists or this asymmetric key already has been added to the database.
Msg 15151, Level 16, State 1, Line 3
Cannot find the asymmetric key 'SQLCLRPSBatch20Key', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Line 4
Cannot find the login 'SQLCLRPSBatch20Login', because it does not exist or you do not have permission.

So I guess since the assembly was creating and signed the same way, I just have to install it. Is this correct?


Solution

  • No. The creating of a login (or, more precisely, a server principal) from the asymmetric key is a way to say "hey... someone who has the rights to do such a thing probably looked at your CLR code and gave it the thumbs-up". After the server principal is created, any assemblies signed with the key that backs it will be okay to run.