62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
|
|
using System;
|
|||
|
|
using System.Security;
|
|||
|
|
using System.Runtime.InteropServices;
|
|||
|
|
using DBToolControls;
|
|||
|
|
using System.Xml.Linq;
|
|||
|
|
|
|||
|
|
namespace DBToolControls {
|
|||
|
|
public class User : IDisposable{
|
|||
|
|
public string Username { get; private set; }
|
|||
|
|
private SecureString password;
|
|||
|
|
|
|||
|
|
public int PasswordLength {
|
|||
|
|
get { return password.Length; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string PasswordAsInsecureString {
|
|||
|
|
get {
|
|||
|
|
IntPtr passwordBSTR = default(IntPtr);
|
|||
|
|
try {
|
|||
|
|
passwordBSTR = Marshal.SecureStringToBSTR(password);
|
|||
|
|
return Marshal.PtrToStringBSTR(passwordBSTR);
|
|||
|
|
} catch {
|
|||
|
|
return "";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Constructors
|
|||
|
|
|
|||
|
|
public User(string username) {
|
|||
|
|
this.Username = username;
|
|||
|
|
this.password = new SecureString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public User(string username, SecureString password) {
|
|||
|
|
this.Username = username;
|
|||
|
|
this.password = password;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public User(string username, string encryptedPassword, string encryptionKey) {
|
|||
|
|
this.Username = username;
|
|||
|
|
this.password = EncryptionHelper.DecryptStringAsSecureString(encryptionKey, encryptedPassword);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Methods
|
|||
|
|
|
|||
|
|
public string EncryptedPassword(string encryptionKey) {
|
|||
|
|
return EncryptionHelper.EncryptString(encryptionKey, this.PasswordAsInsecureString);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Dispose() {
|
|||
|
|
password.Dispose();
|
|||
|
|
GC.SuppressFinalize(this);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public XElement ToXElement(string pw) {
|
|||
|
|
return new XElement("User",
|
|||
|
|
new XElement("Username", Username),
|
|||
|
|
PasswordLength > 0 ? new XElement("Password", EncryptedPassword(pw)) : null);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|