📄 loginform.cs
字号:
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Microsoft.Practices.Mobile.PasswordAuthentication;
using Microsoft.Practices.Mobile.Configuration;
using System.Security.Cryptography;
namespace MobileDevelopersHandbook
{
public partial class LoginForm : Form
{
private DataSet users;
private Dictionary<string, string> configSettings = null;
public LoginForm(Dictionary<string, string> settings)
{
InitializeComponent();
configSettings = settings;
}
private void LoginForm_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try
{
// Get the user details
users = new DataSet();
users.ReadXml(GetApplicationDirectory() + @"\Users.xml");
}
finally
{
Cursor.Current = Cursors.Default;
}
}
private void menuItemSubmit_Click(object sender, EventArgs e)
{
for(int rowidx = 0; rowidx < users.Tables[0].Rows.Count; rowidx++ )
{
DataRow userRow = users.Tables[0].Rows[rowidx];
if ((string)userRow["Name"] == textBoxUsername.Text)
{
Cursor.Current = Cursors.WaitCursor;
try
{
PasswordIdentity identity = AuthenticateUser((string)userRow["Token"]);
if (identity != null && identity.IsAuthenticated)
{
// Success!! Decrypt the config file
DecryptSettings(identity, (string)userRow["encKey"]);
//Close the modal dialog
this.DialogResult = DialogResult.OK;
}
}
finally
{
Cursor.Current = Cursors.Default;
}
}
}
// Show the login failed message
labelIncorrect.Visible = true;
}
private PasswordIdentity AuthenticateUser(string userToken)
{
using (RsaAesCryptographyProvider provider = new RsaAesCryptographyProvider("MobileDevelopersHandbook"))
{
// Create AuthenticationToken using existing token
AuthenticationToken token = new AuthenticationToken(userToken);
PasswordIdentity identity = token.Authenticate(textBoxUsername.Text, textBoxPassword.Text, provider);
// return result - caller checks Authenticated property to see authentication result
return identity;
}
}
private void DecryptSettings(PasswordIdentity identity, string userEncryptedConfigurationKey)
{
// Obtain the user's key
byte[] userKeyBytes = identity.CryptoKey;
// Create an instance of the CryptographyBlock class
SymmetricAlgorithm symmetric = Rijndael.Create();
CryptographyBlock block = new CryptographyBlock(symmetric, userKeyBytes);
// Get the user's encrypted configuration key
byte[] configKeyBytes = Convert.FromBase64String(userEncryptedConfigurationKey);
// Decrypt the key
byte[] configurationKey = block.Decrypt(configKeyBytes);
// Create a configuration provider to decrypt the config file
RijndaelConfigurationProvider configProvider = new RijndaelConfigurationProvider(configurationKey);
// Assign the new instance of the RijndaelConfigurationProvider class to the ConfigurationManager.
ConfigurationManager.ProtectedConfigurationProvider = configProvider;
// Finally, use the GetSection method of the ConfigurationManager to retrieve the section we want
String sectionName = "SystemSettings";
SystemSettingsSection configSection = ConfigurationManager.GetSection(sectionName) as SystemSettingsSection;
// Store the decrypted data in the settings collection
foreach (SystemSettingsItemElement item in configSection.SystemSettingsItems)
{
configSettings.Add(item.Name, item.Value);
}
}
private string GetApplicationDirectory()
{
return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
}
private void textBoxPassword_GotFocus(object sender, EventArgs e)
{
Microsoft.WindowsCE.Forms.InputModeEditor.SetInputMode(textBoxPassword, Microsoft.WindowsCE.Forms.InputMode.Numeric);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -