R. James R. James - 1 year ago 96
C# Question

Encrypting and decrypting SQL table data

I am new to MVC and am building a program where I need to display a table that shows password information to the user (I previously built this program using web forms).
The password information is stored in a SQL DB and encrypted. I need to decrypt the password information when a user accesses the View with the password table.
I have my Decrypt function (from my old web forms program) but am unsure how to apply it using MVC.
Here is my ViewModel:

public class passwordViewModel
public List<passwordTable> passwordType { get; set; }
public List<passwordTable> passwordUsername { get; set; }
public List<passwordTable> passwordActual { get; set; }
public List<passwordTable> passwordNotes { get; set; }
public List<privateDomain> netBIOS { get; set; }
public List<privateDomain> FQDN { get; set; }

My Controller code:

var password = (from r in db.passwordTables
orderby r.companyName
where r.companyName == companyName
select r).ToList();
var pwDecrypt = password.ToList().Select(u => Decrypt(u.passwordActual));
var domain = (from y in db.privateDomains
orderby y.companyName
where y.companyName == companyName
select y).ToList();
var view = new passwordViewModel()
passwordActual = pwDecrypt,
passwordNotes = password,
passwordType = password,
passwordUsername = password,
netBIOS = domain,
FQDN = domain
return View(view);

I am trying to Decrypt the password in the controller at the line:

var pwDecrypt = password.ToList().Select(u => Decrypt(u.passwordActual));

But am receiving an error here:

passwordActual = pwDecrypt,

With an error message:

Cannot implicitly convert type
'system.collections.generic.ienumerable to

Any feedback would be appreciated.

Answer Source

Seems obvious:

Cannot implicitly convert type 'system.collections.generic.ienumerable to 'System.Collections.Generic.List

As the code:

var pwDecrypt = password.ToList().Select(u => Decrypt(u.passwordActual));



Return Value

Type: System.Collections.Generic.IEnumerable

pwDecrypt is assigned the type IEnumerable<> and passwordActual is the type List<>.

A simple fix would be

passwordActual = pwDecrypt.ToList(),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download