69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
namespace Xylem.Common.Ui.GenesisToolBox.Ctls
|
|
{
|
|
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
public partial class LoginForm : UserControl
|
|
{
|
|
private readonly Action<string, string, object> submittedCallback;
|
|
|
|
public LoginForm()
|
|
{
|
|
this.InitializeComponent();
|
|
|
|
this.Dock = DockStyle.Fill;
|
|
}
|
|
|
|
public LoginForm(Action<string, string, object> submittedCallback) : this()
|
|
=> this.submittedCallback = submittedCallback;
|
|
|
|
private void LoginButtonClick(object sender, EventArgs e)
|
|
{
|
|
this.ErrorMessage.Text = default(string);
|
|
this.LoginErrorProvider.Clear();
|
|
|
|
var isValid = true;
|
|
|
|
if (string.IsNullOrWhiteSpace(this.Username.Text))
|
|
{
|
|
isValid = false;
|
|
|
|
this.LoginErrorProvider.SetError(this.Username, "Username is required.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(this.Password.Text))
|
|
{
|
|
isValid = false;
|
|
|
|
this.LoginErrorProvider.SetError(this.Password, "Password is required.");
|
|
}
|
|
|
|
if (isValid)
|
|
{
|
|
this.submittedCallback?.Invoke(this.Username.Text, this.Password.Text.ComputeBase64Hash(), this);
|
|
}
|
|
}
|
|
|
|
internal void ShowError(string errorMessage)
|
|
=> this.ErrorMessage.Text = errorMessage;
|
|
}
|
|
|
|
internal static class PasswordExtensions
|
|
{
|
|
internal static string ComputeBase64Hash(this string plainText)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(plainText))
|
|
{
|
|
return plainText;
|
|
}
|
|
|
|
var buffer = Encoding.UTF8.GetBytes(plainText);
|
|
var hash = SHA256.Create().ComputeHash(buffer);
|
|
|
|
return Convert.ToBase64String(hash);
|
|
}
|
|
}
|
|
}
|