Merge pull request #14 from Crylia/feature/login

Feature/login
This commit is contained in:
Kievits Rene
2021-08-06 10:42:28 +02:00
committed by GitHub
14 changed files with 94 additions and 61 deletions

Binary file not shown.

View File

@@ -6,3 +6,4 @@ C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard Socket\obj\Debug
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard Socket\obj\Debug\netcoreapp3.1\Server Dashboard Socket.csproj.CoreCompileInputs.cache
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard Socket\obj\Debug\netcoreapp3.1\Server Dashboard Socket.dll
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard Socket\obj\Debug\netcoreapp3.1\Server Dashboard Socket.pdb
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard Socket\obj\Debug\netcoreapp3.1\Server Dashboard Socket.csprojAssemblyReference.cache

View File

@@ -35,71 +35,64 @@ namespace Server_Dashboard {
}
}
public static bool CheckCookie(string cookie, string username) {
string valid = "False";
ConnectToDatabase(con => {
string query = "EXEC CheckUserCookie @Cookie = @cookie, @UserName = @username, @Valid = @valid OUTPUT";
using (SqlCommand com = new SqlCommand(query, con)) {
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@valid", SqlDbType.NVarChar, 250);
com.Parameters["@valid"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
valid = Convert.ToString(com.Parameters["@Valid"].Value);
}
});
return Convert.ToBoolean(valid);
}
public static bool DeleteCookie(string username) {
string valid = "False";
ConnectToDatabase(con => {
string query = "EXEC DeleteUserCookie @Username = @username, @ResponseMessage = @response OUTPUT";
using (SqlCommand com = new SqlCommand(query, con)) {
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@response", SqlDbType.NVarChar, 250);
com.Parameters["@response"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
valid = Convert.ToString(com.Parameters["@ResponseMessage"].Value);
}
});
return Convert.ToBoolean(valid);
}
public static bool AddCookie(string cookie, string username) {
string valid = "False";
ConnectToDatabase(con => {
string query = "EXEC AddCookieToUser @Cookie = @cookie, @UserName = @username, @ResponseMessage = @response OUTPUT";
using (SqlCommand com = new SqlCommand(query, con)) {
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@response", SqlDbType.NVarChar, 250);
com.Parameters["@response"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
valid = Convert.ToString(com.Parameters["@ResponseMessage"].Value);
}
});
return Convert.ToBoolean(valid);
}
#region Private methods
/// <summary>
/// Opens a database connection
/// </summary>
/// <param name="callback">Callback type SqlConnection</param>
/// <returns></returns>
private static SqlConnection ConnectToDatabase(Action<SqlConnection> callback) {
public static int CheckCookie(string cookie, string username) {
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString)) {
try {
con.Open();
callback(con);
con.Close();
string query = "EXEC CheckUserCookie @Cookie = @cookie, @UserName = @username, @Valid = @valid OUTPUT";
using (SqlCommand com = new SqlCommand(query, con)) {
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@valid", SqlDbType.Bit);
com.Parameters["@valid"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
return Convert.ToInt32(com.Parameters["@Valid"].Value);
}
} catch (SqlException ex) {
return null;
return ex.Number;
} finally {
con.Close();
}
}
}
public static void DeleteCookie(string username) {
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString)) {
try {
con.Open();
string query = "EXEC DeleteUserCookie @Username = @username, @ResponseMessage = @response OUTPUT";
using (SqlCommand com = new SqlCommand(query, con)) {
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@response", SqlDbType.NVarChar, 250);
com.Parameters["@response"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
}
} catch (SqlException ex) {
} finally {
con.Close();
}
}
}
public static int AddCookie(string cookie, string username) {
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString)) {
try {
con.Open();
string query = "EXEC AddCookieToUser @Cookie = @cookie, @UserName = @username, @ResponseMessage = @response OUTPUT";
using (SqlCommand com = new SqlCommand(query, con)) {
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@response", SqlDbType.NVarChar, 250);
com.Parameters["@response"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
return Convert.ToInt32(com.Parameters["@ResponseMessage"].Value);
}
} catch (SqlException ex) {
return ex.Number;
} finally {
con.Close();
}
}
return null;
}
#endregion
}
}

View File

@@ -58,5 +58,17 @@ namespace Server_Dashboard.Properties {
this["RememberMe"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string Password {
get {
return ((string)(this["Password"]));
}
set {
this["Password"] = value;
}
}
}
}

View File

@@ -11,5 +11,8 @@
<Setting Name="RememberMe" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="Password" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View File

@@ -60,6 +60,7 @@ namespace Server_Dashboard {
Username = Settings.Default.Username;
RememberUser = Settings.Default.RememberMe;
}
AutoLoginAsync();
}
public ICommand LoginCommand { get; set; }
@@ -81,6 +82,7 @@ namespace Server_Dashboard {
Settings.Default.Cookies = null;
Settings.Default.Username = "";
Settings.Default.RememberMe = false;
Settings.Default.Password = "";
Settings.Default.Save();
DatabaseHandler.DeleteCookie(Username);
}
@@ -89,6 +91,7 @@ namespace Server_Dashboard {
Settings.Default.Cookies = guid;
Settings.Default.Username = Username;
Settings.Default.RememberMe = true;
Settings.Default.Password = "*****";
Settings.Default.Save();
DatabaseHandler.AddCookie(Username, guid);
}
@@ -117,5 +120,19 @@ namespace Server_Dashboard {
}
ErrorText = "";
}
//TODO: Add autologin function that locks the UI untill the user hits the abort button or the login completes
/*private async void AutoLoginAsync() {
if (Settings.Default.RememberMe && !String.IsNullOrEmpty(Settings.Default.Username) && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
Loading = "Visible";
int result = await Task.Run(() => DatabaseHandler.CheckCookie(Settings.Default.Cookies, Username));
Loading = "Hidden";
if (result == 1) {
DashboardWindow window = new DashboardWindow();
window.Show();
Close?.Invoke();
return;
}
}
}*/
}
}

View File

@@ -1,4 +1,11 @@
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\obj\Debug\netcoreapp3.1\GeneratedInternalTypeHelper.g.i.cs
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\App.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\Dashboard\CRUD Popup\CreateModulePopup.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\DoubleRoundProgressBar\DoubleRoundProgressBar.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\HalfRoundProgressBar\HalfRoundProgressBar.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\LoadingIndicator\LoadingIndicator.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\ServerModules\ServerModule.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\LoginWindow.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Views\DashboardPages\MainDashboardPage.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Views\DashboardWindow.xaml;;