commit a6ed8484a5794ea38097c48ae2804ef79cf7468f Author: Gerardo Date: Thu Sep 12 08:48:32 2024 +0200 version inicial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6235804 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vs/* +/Notifica/bin/* +/MigrationBackup/* +/Notifica/obj/* diff --git a/Notifica.sln b/Notifica.sln new file mode 100644 index 0000000..132e6ba --- /dev/null +++ b/Notifica.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35013.160 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notifica", "Notifica\Notifica.csproj", "{F4DFCA6B-D27D-4F2C-9ED6-39751A005E45}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F4DFCA6B-D27D-4F2C-9ED6-39751A005E45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4DFCA6B-D27D-4F2C-9ED6-39751A005E45}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4DFCA6B-D27D-4F2C-9ED6-39751A005E45}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4DFCA6B-D27D-4F2C-9ED6-39751A005E45}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B8E9237F-F36B-43F0-A550-8EB9021E6295} + EndGlobalSection +EndGlobal diff --git a/Notifica/App.config b/Notifica/App.config new file mode 100644 index 0000000..1582e85 --- /dev/null +++ b/Notifica/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Notifica/Notifica.cs b/Notifica/Notifica.cs new file mode 100644 index 0000000..6a966a0 --- /dev/null +++ b/Notifica/Notifica.cs @@ -0,0 +1,26 @@ +using Microsoft.Toolkit.Uwp.Notifications; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace Notifica +{ + internal class Notifica : impersonalizacionTarea + { + public string msg { get; set; } + public void tareaImpersonalizada() + { + MessageBox.Show(msg); + /* + new ToastContentBuilder() + .AddArgument("action", "viewConversation") + .AddArgument("conversationId", 9813) + .AddText("Aviso") + .AddText(msg) + .Show();*/ + } + } +} diff --git a/Notifica/Notifica.csproj b/Notifica/Notifica.csproj new file mode 100644 index 0000000..bfa6d87 --- /dev/null +++ b/Notifica/Notifica.csproj @@ -0,0 +1,70 @@ + + + + + Debug + AnyCPU + {F4DFCA6B-D27D-4F2C-9ED6-39751A005E45} + Exe + Notifica + Notifica + v4.8 + 512 + true + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + solologo_ico.ico + + + + + + + + + + + + + + + + + + + + + + + + + 7.1.3 + + + + + + + \ No newline at end of file diff --git a/Notifica/Notifica.csproj.user b/Notifica/Notifica.csproj.user new file mode 100644 index 0000000..005eff1 --- /dev/null +++ b/Notifica/Notifica.csproj.user @@ -0,0 +1,6 @@ + + + + "Notificacion de prueba" + + \ No newline at end of file diff --git a/Notifica/Program.cs b/Notifica/Program.cs new file mode 100644 index 0000000..c486c7d --- /dev/null +++ b/Notifica/Program.cs @@ -0,0 +1,49 @@ +using Microsoft.Toolkit.Uwp.Notifications; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notifica +{ + internal class Program + { + static void Main(string[] args) + { + if (args.Count() <= 0) + return; + var Notificador = new impersonalizacionExe(); + var Notificacion = new Notifica(); + Notificador.Tarea = Notificacion; + + Notificador.Usuario=ConfigurationManager.AppSettings["usuario"]; + Notificador.Dominio = ConfigurationManager.AppSettings["dominio"]; + Notificador.Clave = ConfigurationManager.AppSettings["clave"]; + foreach (string arg in args) + { + Notificacion.msg = arg; + string res=Notificador.runImpersonal(); + + var sw = new StreamWriter("F:\\desa\\v2022\\Notifica\\Notifica\\bin\\Release\\Sample.txt"); + if (res != "") + sw.WriteLine(res); + else + sw.WriteLine("ok"); + sw.Close(); + + + //string mens = ConfigurationManager.AppSettings["MensajeAviso"]; + /*new ToastContentBuilder() + .AddArgument("action", "viewConversation") + .AddArgument("conversationId", 9813) + .AddText("Aviso") + .AddText(arg) + .Show();*/ + // Not seeing the Show() method? Make sure you have version 7.0, and if you're using .NET 6 (or later), then your TFM must be net6.0-windows10.0.17763.0 or greater + } + } + } +} diff --git a/Notifica/Properties/AssemblyInfo.cs b/Notifica/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c5bf8c9 --- /dev/null +++ b/Notifica/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// La información general de un ensamblado se controla mediante el siguiente +// conjunto de atributos. Cambie estos valores de atributo para modificar la información +// asociada a un ensamblado. +[assembly: AssemblyTitle("Notifica")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Notifica")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Si establece ComVisible en false, los tipos de este ensamblado no estarán visibles +// para los componentes COM. Si es necesario obtener acceso a un tipo en este ensamblado desde +// COM, establezca el atributo ComVisible en true en este tipo. +[assembly: ComVisible(false)] + +// El siguiente GUID sirve como id. de typelib si este proyecto se expone a COM. +[assembly: Guid("f4dfca6b-d27d-4f2c-9ed6-39751a005e45")] + +// La información de versión de un ensamblado consta de los cuatro valores siguientes: +// +// Versión principal +// Versión secundaria +// Número de compilación +// Revisión +// +// Puede especificar todos los valores o usar los valores predeterminados de número de compilación y de revisión +// utilizando el carácter "*", como se muestra a continuación: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Notifica/impersonalizacionExe.cs b/Notifica/impersonalizacionExe.cs new file mode 100644 index 0000000..0d43a41 --- /dev/null +++ b/Notifica/impersonalizacionExe.cs @@ -0,0 +1,111 @@ +using System; +using System.Runtime.InteropServices; +using System.Security.Principal; +using System.Security.Permissions; +using Microsoft.Win32.SafeHandles; +using System.Runtime.ConstrainedExecution; +using System.Security; +using Windows.System; +using System.Runtime.Remoting.Messaging; + + +namespace Notifica +{ + + + public interface impersonalizacionTarea + { + void tareaImpersonalizada(); + } + + public class impersonalizacionExe + { + public string Usuario { get; set; } + public string Dominio { get; set; } + + public string Clave { get; set; } + + public impersonalizacionTarea Tarea { get; set; } + + [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, + int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + public extern static bool CloseHandle(IntPtr handle); + + private void run() + { + Tarea.tareaImpersonalizada(); + } + //si va bien devuelve "" + [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] + public string runImpersonal() + { + SafeTokenHandle safeTokenHandle; + var res = ""; + try + { + + const int LOGON32_PROVIDER_DEFAULT = 0; + //This parameter causes LogonUser to create a primary token. + const int LOGON32_LOGON_INTERACTIVE = 2; + + // Call LogonUser to obtain a handle to an access token. + bool returnValue = LogonUser(Usuario, Dominio, Clave, + LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, + out safeTokenHandle); + + + if (false == returnValue) + { + int ret = Marshal.GetLastWin32Error(); + res=string.Format("LogonUser failed with error code : {0}", ret); + } + using (safeTokenHandle) + { + // Check the identity. + /*Console.WriteLine("Before impersonation: " + + WindowsIdentity.GetCurrent().Name);*/ + // Use the token handle returned by LogonUser. + using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle())) + { + using (WindowsImpersonationContext impersonatedUser = newId.Impersonate()) + { + res = res + "identidad: " + WindowsIdentity.GetCurrent().Name; + run(); + // Check the identity. + /*Console.WriteLine("After impersonation: " + + WindowsIdentity.GetCurrent().Name);*/ + } + } + // Releasing the context object stops the impersonation + // Check the identity. + } + } + catch (Exception ex) + { + res=res+"excepcion:"+ex.Message; + } + return res; + } + } + public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid + { + private SafeTokenHandle() + : base(true) + { + } + + [DllImport("kernel32.dll")] + [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] + [SuppressUnmanagedCodeSecurity] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool CloseHandle(IntPtr handle); + + protected override bool ReleaseHandle() + { + return CloseHandle(handle); + } + } +} \ No newline at end of file diff --git a/Notifica/solologo_ico.ico b/Notifica/solologo_ico.ico new file mode 100644 index 0000000..925e34b Binary files /dev/null and b/Notifica/solologo_ico.ico differ