views:

236

answers:

2

I have the following:

interface IDefectRepository { /* ... */ }
class MyDefectRepository : IDefectRepository
{
    public MyDefectRepository(string url, string userName, string password)
    {
        // ...
    }

    // ...
}

I'm using <parameters> to pass the constructor parameters from Web.config. Is there any way that I can store the password encrypted in the Web.config file? Other suggestions?

+1  A: 

Try inheriting MyDefectRepositoryWithEncryptedPasswords from MyDefectRepository. In the MyDefectRepositoryWithEncryptedPasswords constructor decrypt the password and pass it to the MyDefectRepository constructor, like so:

class MyDefectRepositoryWithEncryptedPasswords : MyDefectRepository
{
    public MyDefectRepositoryWithEncryptedPasswords(string url, string userName, string encryptedPassword)
        : base(url, userName, Decrypt(encryptedPassword))
    {
    }

    public static string Decrypt(string encrypted)
    {
        // Do whatever...
    }
}

Anyway, I don't think you should store encrypted passwords with two-way encryption methods. You should use some sort of hashing (of the cryptographic kind) and compare the hashes. That would require changing your constructor to receive not the password, but its hash.

Martinho Fernandes
+1 Never store recoverable passwords if all you ever need to do is authenticate a password. Store cryptographic hashes of the password instead.
Arnold Spence
Can't do that: it's the other end that needs the password, and I need to provide it. Think of it as MyRemoteDefectRepository.
Roger Lipscombe
@Roger: I thought of that and that's why I suggested the subclass/wrapper solution in the first place. Just don't forget to use an secure channel to transmit the password.
Martinho Fernandes
+1  A: 

You could inject the password via a ISubDependencyResolver (sample1, sample2) which would get the password from an encrypted section in your web.config.

Mauricio Scheffer
I like this idea. Quite simple, elegant, and most important - transparent to the upper (or inner, depends on your architecture) layers.
Krzysztof Koźmic