This commit is contained in:
Meik
2026-03-05 09:56:57 +01:00
parent 838e6b1ee1
commit 4013fa8e32
827 changed files with 743038 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
using C4IT.Logging;
using static C4IT.Logging.cLogManager;
using Microsoft.Web.WebView2.Core;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using C4IT.Matrix42.WebClient;
using C4IT_CustomerPanel.libs;
namespace C4IT_CustomerPanel.forms
{
public partial class CPM42FormsAuthentication : Window
{
private Uri _logonUrl = null;
private string _M42Server;
public string AccessToken { get; private set; }
public event EventHandler AuthenticationCompleted;
public CPM42FormsAuthentication()
{
InitializeComponent();
}
private void OnClose(object sender, RoutedEventArgs e)
{
this.Close();
}
private void StartForm()
{
var methodInfo = MethodBase.GetCurrentMethod();
LogMethodBegin(methodInfo);
try
{
LogEntry("Erstelle Uri.", LogLevels.Debug);
_M42Server = MainWindow.MainInstance.ConfigSettings.GetMatrixServer(false);
_logonUrl = new Uri($"https://{_M42Server}/m42Services/api/sts/authorize?client_id=ServiceStore.NewUX&scope=urn:matrix42NewUX&response_type=token&autoLogin=true");
WebLogon.Source = _logonUrl;
}
catch (Exception ex)
{
LogEntry($"Erstellte Uri: {_logonUrl}", LogLevels.Debug);
LogException(ex);
}
finally
{
LogMethodEnd(methodInfo);
}
}
private async void WebLogon_Loaded(object sender, RoutedEventArgs e)
{
var methodInfo = MethodBase.GetCurrentMethod();
LogMethodBegin(methodInfo);
try
{
// set the data path for the WebView2 component
var _webViewConfigPath = cLogManagerFile.GetDefaultPath(false, SubFolder: "WebViewData");
_webViewConfigPath = System.IO.Path.GetDirectoryName(_webViewConfigPath);
CultureInfo culture = new CultureInfo("en-US");
var webEnv = await CoreWebView2Environment.CreateAsync(userDataFolder: _webViewConfigPath, options: new CoreWebView2EnvironmentOptions() { Language = culture.TextInfo.CultureName, AllowSingleSignOnUsingOSPrimaryAccount = true });
var _co = webEnv.CreateCoreWebView2ControllerOptions();
await WebLogon.EnsureCoreWebView2Async(webEnv, _co);
LogEntry($"CreateCoreWebView2ControllerOptions Result: {_co}", LogLevels.Debug);
LogEntry($"WebLogon.EnsureCoreWebView2Async Result: {webEnv}", LogLevels.Debug);
LogEntry($"webEnv.FailureReportFolderPath: {webEnv.FailureReportFolderPath}", LogLevels.Debug);
// start the logon page
StartForm();
}
catch (Exception ex)
{
LogEntry($"Ausnahme in {methodInfo.Name}: {ex.Message}", LogLevels.Debug);
LogException(ex);
}
finally
{
LogMethodEnd(methodInfo);
}
}
private string GetAccessToken(string _frag)
{
var _arrParameters = _frag.Split('&');
string _AccessToken = null;
foreach (var _arrParam in _arrParameters)
{
var _items = _arrParam.Split('=');
if(_items.Length == 2 && _items[0] == "#access_token")
_AccessToken = _items[1];
}
return _AccessToken;
}
private void WebLogon_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
{
var CM = MethodBase.GetCurrentMethod();
LogMethodBegin(CM);
try
{
var _u = e.Uri;
Debug.WriteLine($"nav.start: {_u}");
var _uri = new Uri(_u);
var _host = _uri.Host.ToLowerInvariant();
var _path = _uri.LocalPath.ToLowerInvariant();
var _frag = _uri.Fragment;
var _query = _uri.Query;
string M42Code = null;
string M42State = null;
if (_frag == null)
_frag = "";
if (_host == _M42Server.ToLowerInvariant() && _path == "/wm/" && _query.Contains("code="))
{
var _tmpQuery = _query;
if (_tmpQuery.StartsWith("?"))
_tmpQuery = _tmpQuery.Remove(0, 1);
var _arrItems = _tmpQuery.Split('&');
foreach (var item in _arrItems)
{
var _props = item.Split('=');
if (_props.Length == 2)
{
switch (_props[0])
{
case "code":
M42Code = _props[1];
break;
case "state":
M42State = _props[1];
break;
}
}
}
WebLogon.Stop();
WebLogon.CoreWebView2.Navigate(_logonUrl.AbsoluteUri);
}
if (_frag != String.Empty)
{
AccessToken = GetAccessToken(_frag);
MainWindow.MainInstance._userIsAuthenticated = true;
if (MainWindow.MainInstance._initializedUiComponents)
{
MainWindow.MainInstance.InitTimer();
MainWindow.MainInstance.InitializeOrUpdateTrayMenu();
MainWindow.MainInstance.RefreshIcon.Visibility = Visibility.Visible;
}
AuthenticationCompleted?.Invoke(this, EventArgs.Empty);
this.Close();
}
}
catch (Exception E)
{
LogException(E);
}
finally
{
LogMethodEnd(CM);
}
}
private void Close_Text_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
AuthenticationCompleted?.Invoke(this, EventArgs.Empty);
this.Close();
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
}
}