ConnectionString encryption decryption

G

Guest

Hello,

I have a ASP.NET web application problem involving the data encryption and
decryption assembly(DLL) used on the connection string value that is set in
the webconfig file.
The problem occurs in the application when you instantiate a new instance of
the class as shown below:

---Dim dp As DPAPIComp.DataProtectorComp = New DPAPIComp.DataProtectorComp---
where DPAPIComp is the name of the namespace referenced to in the library
and DataProtectorComp is the class. This class contains the Encrypt and
Decrypt function used to encrypt and decrypt the connection string pass to
it.

Below is the code responsing to the call above. (in the form of an assembly)

using System;
using System.EnterpriseServices;
using System.Security.Principal;
using System.Runtime.InteropServices;
using DataProtection;

namespace DPAPIComp
{
public class DataProtectorComp: ServicedComponent
{
public byte[] Encrypt(byte[] plainText)
{
DataProtector dp = new DataProtector( DataProtector.Store.USE_USER_STORE );
byte[] cipherText = null;
try
{
cipherText = dp.Encrypt(plainText, null);
}
catch(Exception ex)
{
throw new Exception("Exception encrypting. " + ex.Message);
}
return cipherText;

}
public byte[] Decrypt(byte[] cipherText)
{
DataProtector dp = new DataProtector( DataProtector.Store.USE_USER_STORE );
byte[] plainText = null;

try
{
plainText = dp.Decrypt(cipherText,null);
}
catch(Exception ex)
{
throw new Exception("Exception decrypting. " + ex.Message);
}
return plainText;
}

public DataProtectorComp()
{

}
}
}
--------------------------------------------

The error message as it passes this line in the debugger is "Access is
denied".
Source: "mscorlib"
StackTrace: " at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode,
IntPtr errorInfo)
at System.EnterpriseServices.Thunk.Proxy.CoCreateObject(Type serverType,
Boolean bQuerySCInfo, Boolean& bIsAnotherProcess, String& uri)
at
System.EnterpriseServices.ServicedComponentProxyAttribute.CreateInstance(Type
serverType)
at
System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(Type
serverType, Object[] props, Boolean bNewObj)
at BoardroomBookings.DBConnection.GetConnectionString(String Name,
String& CnnStr) in C:\Inetpub\wwwroot\BoardroomBookings\DBConnection.vb:line
14" String

--------------------------------------------
I have tried placing the DLL in the GAC and giving it a strong name and that
still give the same error message. The DPAPI solution was built successfully.
Would this indicate that there is some sort of permission being applied to
the encryption/decryption call because I cannot connect to the server.
How do I get it to instantiate this probably and be able to connect to the
database.

Cheers,

Please note the same version works on the server but does not work on my
local workstation. Does this tell you something?
 
G

Guest

the stack trace suggests that you are going through COM interop and the fact
that you have derived from ServicedComponent also suggests that you are
running in COM+.

I can't see any reason why you would want to do that since there is no
reason for this code to be using any COM+ services - but that's obviously a
seperate discussion.

At a guess it looks like you have some security applied to the COM+ package
which is causing the 'access denied'. Although you'd have to supply more info
about the how all the bits are configured.

Regards
Niroo T [MSFT]

Jase H said:
Hello,

I have a ASP.NET web application problem involving the data encryption and
decryption assembly(DLL) used on the connection string value that is set in
the webconfig file.
The problem occurs in the application when you instantiate a new instance of
the class as shown below:

---Dim dp As DPAPIComp.DataProtectorComp = New DPAPIComp.DataProtectorComp---
where DPAPIComp is the name of the namespace referenced to in the library
and DataProtectorComp is the class. This class contains the Encrypt and
Decrypt function used to encrypt and decrypt the connection string pass to
it.

Below is the code responsing to the call above. (in the form of an assembly)

using System;
using System.EnterpriseServices;
using System.Security.Principal;
using System.Runtime.InteropServices;
using DataProtection;

namespace DPAPIComp
{
public class DataProtectorComp: ServicedComponent
{
public byte[] Encrypt(byte[] plainText)
{
DataProtector dp = new DataProtector( DataProtector.Store.USE_USER_STORE );
byte[] cipherText = null;
try
{
cipherText = dp.Encrypt(plainText, null);
}
catch(Exception ex)
{
throw new Exception("Exception encrypting. " + ex.Message);
}
return cipherText;

}
public byte[] Decrypt(byte[] cipherText)
{
DataProtector dp = new DataProtector( DataProtector.Store.USE_USER_STORE );
byte[] plainText = null;

try
{
plainText = dp.Decrypt(cipherText,null);
}
catch(Exception ex)
{
throw new Exception("Exception decrypting. " + ex.Message);
}
return plainText;
}

public DataProtectorComp()
{

}
}
}
--------------------------------------------

The error message as it passes this line in the debugger is "Access is
denied".
Source: "mscorlib"
StackTrace: " at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode,
IntPtr errorInfo)
at System.EnterpriseServices.Thunk.Proxy.CoCreateObject(Type serverType,
Boolean bQuerySCInfo, Boolean& bIsAnotherProcess, String& uri)
at
System.EnterpriseServices.ServicedComponentProxyAttribute.CreateInstance(Type
serverType)
at
System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(Type
serverType, Object[] props, Boolean bNewObj)
at BoardroomBookings.DBConnection.GetConnectionString(String Name,
String& CnnStr) in C:\Inetpub\wwwroot\BoardroomBookings\DBConnection.vb:line
14" String

--------------------------------------------
I have tried placing the DLL in the GAC and giving it a strong name and that
still give the same error message. The DPAPI solution was built successfully.
Would this indicate that there is some sort of permission being applied to
the encryption/decryption call because I cannot connect to the server.
How do I get it to instantiate this probably and be able to connect to the
database.

Cheers,

Please note the same version works on the server but does not work on my
local workstation. Does this tell you something?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top