Skip to content

Commit

Permalink
Check user
Browse files Browse the repository at this point in the history
  • Loading branch information
dagolav committed Feb 26, 2024
1 parent 6280652 commit b233a2e
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Gui/SettingsDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

using Serilog;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Geonorge.MassivNedlasting.Gui
Expand All @@ -12,6 +16,7 @@ public partial class SettingsDialog
{
private AppSettings _appSettings;
private List<string> _configFiles;
private static readonly HttpClient Client = new HttpClient() { Timeout = TimeSpan.FromMilliseconds(30000) };

public SettingsDialog()
{
Expand All @@ -29,9 +34,36 @@ private void BtnDialogOk_Click(object sender, RoutedEventArgs e)
{
_appSettings.Password = ProtectionService.CreateProtectedPassword(txtPassword.Password);
_appSettings.Username = txtUsername.Text;
ApplicationService.WriteToAppSettingsFile(_appSettings);

Close();
if(!string.IsNullOrEmpty(_appSettings.Username) && !string.IsNullOrEmpty(_appSettings.Password))
{
var urlValidateUser = "https://nedlasting.geonorge.no/api/download/validate-user";

var byteArray = Encoding.ASCII.GetBytes(_appSettings.Username + ":" + ProtectionService.GetUnprotectedPassword(_appSettings.Password));
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

using (var response = Client.GetAsync(urlValidateUser, HttpCompletionOption.ResponseHeadersRead))
{

if (!response.Result.IsSuccessStatusCode && response.Result.StatusCode != System.Net.HttpStatusCode.NotFound)
{
var message = response.Result.Content.ReadAsStringAsync().Result;
Log.Error(message);
Console.WriteLine(message);
MessageBox.Show(message);
}
else {
ApplicationService.WriteToAppSettingsFile(_appSettings);
Close();
}

}
}
else
{
ApplicationService.WriteToAppSettingsFile(_appSettings);
Close();
}
}

private void ButtonEditConfig_OnClick(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit b233a2e

Please sign in to comment.