install transport Agent (Exchange) programmatically - c#

Is it possible to install a Transport Agent to Exchange Server within a C# programm?
Normally you create your Agent .dll, then you need to open Exchange Management Shell and execute the following commands:
Install-TransportAgent -Name "Agent Name" -TransportAgentFactory "Factory.Class.Name" -AssemblyPath "C:\Path\to\agent.dll"
and
enable-transportagent -Identity "Agent Name"
and setting the priority:
Set-TransportAgent -Identity "Agent Name" -Priority 3
How can I install the transport agent from within a C# application (either calling a PowerShell command or directly using the .NET Framework?

I found a solution using PowerShell directly from C# and calling the corresponding cmdlets:
Note: The full code is available here: https://github.com/Pro/dkim-exchange/blob/master/Src/Configuration.DkimSigner/Exchange/ExchangeServer.cs#L111 and https://github.com/Pro/dkim-exchange/blob/master/Src/Configuration.DkimSigner/Exchange/PowershellHelper.cs#L29
/// <summary>
/// Installs the transport agent by calling the corresponding PowerShell commands (Install-TransportAgent and Enable-TransportAgent).
/// The priority of the agent is set to the highest one.
/// You need to restart the MSExchangeTransport service after install.
///
/// Throws ExchangeHelperException on error.
/// </summary>
public static void installTransportAgent()
{
using (Runspace runspace = RunspaceFactory.CreateRunspace(getPSConnectionInfo()))
{
runspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
int currPriority = 0;
Collection<PSObject> results;
if (!isAgentInstalled())
{
// Install-TransportAgent -Name "Exchange DkimSigner" -TransportAgentFactory "Exchange.DkimSigner.DkimSigningRoutingAgentFactory" -AssemblyPath "$EXDIR\ExchangeDkimSigner.dll"
powershell.AddCommand("Install-TransportAgent");
powershell.AddParameter("Name", AGENT_NAME);
powershell.AddParameter("TransportAgentFactory", "Exchange.DkimSigner.DkimSigningRoutingAgentFactory");
powershell.AddParameter("AssemblyPath", System.IO.Path.Combine(AGENT_DIR, "ExchangeDkimSigner.dll"));
results = invokePS(powershell, "Error installing Transport Agent");
if (results.Count == 1)
{
currPriority = Int32.Parse(results[0].Properties["Priority"].Value.ToString());
}
powershell.Commands.Clear();
// Enable-TransportAgent -Identity "Exchange DkimSigner"
powershell.AddCommand("Enable-TransportAgent");
powershell.AddParameter("Identity", AGENT_NAME);
invokePS(powershell, "Error enabling Transport Agent");
}
powershell.Commands.Clear();
// Determine current maximum priority
powershell.AddCommand("Get-TransportAgent");
results = invokePS(powershell, "Error getting list of Transport Agents");
int maxPrio = 0;
foreach (PSObject result in results)
{
if (!result.Properties["Identity"].Value.ToString().Equals(AGENT_NAME)){
maxPrio = Math.Max(maxPrio, Int32.Parse(result.Properties["Priority"].Value.ToString()));
}
}
powershell.Commands.Clear();
if (currPriority != maxPrio + 1)
{
//Set-TransportAgent -Identity "Exchange DkimSigner" -Priority 3
powershell.AddCommand("Set-TransportAgent");
powershell.AddParameter("Identity", AGENT_NAME);
powershell.AddParameter("Priority", maxPrio + 1);
results = invokePS(powershell, "Error setting priority of Transport Agent");
}
}
}
}
/// <summary>
/// Checks if the last powerShell command failed with errors.
/// If yes, this method will throw an ExchangeHelperException to notify the callee.
/// </summary>
/// <param name="powerShell">PowerShell to check for errors</param>
/// <param name="errorPrependMessage">String prepended to the exception message</param>
private static Collection<PSObject> invokePS(PowerShell powerShell, string errorPrependMessage)
{
Collection<PSObject> results = null;
try
{
results = powerShell.Invoke();
}
catch (System.Management.Automation.RemoteException e)
{
if (errorPrependMessage.Length > 0)
throw new ExchangeHelperException("Error getting list of Transport Agents:\n" + e.Message, e);
else
throw e;
}
if (powerShell.Streams.Error.Count > 0)
{
string errors = errorPrependMessage;
if (errorPrependMessage.Length > 0 && !errorPrependMessage.EndsWith(":"))
errors += ":";
foreach (ErrorRecord error in powerShell.Streams.Error)
{
if (errors.Length > 0)
errors += "\n";
errors += error.ToString();
}
throw new ExchangeHelperException(errors);
}
return results;
}

Yes, Its possible to install\uninstall Exchange Transport agent using C#. In fact, i have done it.
What i did is, I called exchange cmdlets using PowerShell, and installed the agent on exchange hub server. I also had to stop\start MS Exchange Transport Agent service using C#. Besides, i also had to create the folder, where i had to place agent files, and also grant Network Service read\write permission on that folder.
Regards,
Laeeq Qazi

I have written a class which allows you to run exchange commands either locally or through a remote shell.
The following commands are available in this class:
GetAgentInfo : Receive the transport agent information by passing the NAme as parameter
InstallAgent : Install a transport agent by passing Name, FactoryNAme and Assembly path
EnableAgent : Enable a transport agent
UninstallAgent : Uninstall a transportagent
RestartTransportService : Restart the Microsoft Transport Service
It also verifies what version of Exchange is installed to load the correct SnapIn
Here is the code writte in C#:
/// <summary>
/// This class is used to connect to either an remote or the local exchange powershell and allows you to execute several exchange cmdlets easiely
/// </summary>
public class ExchangeShell : IDisposable
{
/// <summary>
/// registry key to verify the installed exchange version, see <see cref="verifyExchangeVersion"/> method for more info
/// </summary>
private string EXCHANGE_KEY = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{4934D1EA-BE46-48B1-8847-F1AF20E892C1}";
private ExchangeVersionEnum m_ExchangeVersion;
/// <summary>
/// getter to receive the current exchange version (local host only)
/// </summary>
public ExchangeVersionEnum ExchangeVersion { get { return m_ExchangeVersion; } }
public enum ExchangeVersionEnum {
Unknown = 0,
v2010 = 1,
v2013 = 2,
}
public string Host { get; private set; }
/// <summary>
/// stores the powershell runspaces for either local or any other remote connection
/// </summary>
private Dictionary<string, Runspace> m_runspaces = new Dictionary<string, Runspace>();
/// <summary>
/// get the current runspace being used for the cmdlets - only for internal purposes
/// </summary>
private Runspace currentRunspace {
get
{
if (m_runspaces.ContainsKey(this.Host))
return m_runspaces[this.Host];
else
throw new Exception("No Runspace found for host '" + this.Host + "'. Use SetRemoteHost first");
}
}
/// <summary>
/// Call the constructor to either open a local exchange shell or force open the local shell as remote connection (primary used to bypass "Microsoft.Exchange.Net" assembly load failures)
/// </summary>
/// <param name="forceRemoteShell"></param>
public ExchangeShell(bool forceRemoteShell = false)
{
if (!forceRemoteShell)
{
this.m_ExchangeVersion = this.verifyExchangeVersion();
if (this.m_ExchangeVersion == ExchangeVersionEnum.Unknown) throw new Exception("Unable to verify Exchange version");
this.SetLocalHost();
}
else
{
// Use empty hostname to connect to localhost via http://computername.domain/[...]
this.SetRemoteHost("");
}
}
/// <summary>
/// Constructor to open a remote exchange shell
/// TODO: display authentication prompt for different login credentials
/// </summary>
/// <param name="hostName">host of the remote powershell</param>
/// <param name="authenticationPrompt">not yet implemented</param>
public ExchangeShell(string hostName, bool authenticationPrompt = false)
{
// TODO: Implement prompt for authenication different then default
this.SetRemoteHost(hostName);
}
/// <summary>
/// private function to verify the exchange version (local only)
/// </summary>
/// <returns></returns>
private ExchangeVersionEnum verifyExchangeVersion()
{
var hklm = Microsoft.Win32.Registry.LocalMachine;
var exchangeInstall = hklm.OpenSubKey(EXCHANGE_KEY);
if (exchangeInstall != null)
{
var exchangeVersionKey = exchangeInstall.GetValue("DisplayVersion").ToString();
if (exchangeVersionKey.StartsWith("14."))
return ExchangeVersionEnum.v2010;
else if (exchangeVersionKey.StartsWith("15."))
return ExchangeVersionEnum.v2013;
}
return ExchangeVersionEnum.Unknown;
}
/// <summary>
/// set the current runspace to local.
/// Every command will be executed on the local machine
/// </summary>
public void SetLocalHost()
{
if (!this.m_runspaces.ContainsKey("localhost"))
{
RunspaceConfiguration rc = RunspaceConfiguration.Create();
PSSnapInException psSnapInException = null;
switch (this.m_ExchangeVersion)
{
case ExchangeVersionEnum.v2010:
rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out psSnapInException);
break;
case ExchangeVersionEnum.v2013:
rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.SnapIn", out psSnapInException);
break;
}
if (psSnapInException != null)
throw psSnapInException;
var runspace = RunspaceFactory.CreateRunspace(rc);
runspace.Open();
this.m_runspaces.Add("localhost", runspace);
}
this.Host = "localhost";
}
/// <summary>
/// Setup a runspace for a remote host
/// After calling this method, currentRunspace is being used to execute the commands
/// </summary>
/// <param name="hostName"></param>
public void SetRemoteHost(string hostName = null)
{
if (String.IsNullOrEmpty(hostName))
hostName = Environment.MachineName + "." + Environment.UserDomainName + ".local";
hostName = hostName.ToLower();
if (!this.m_runspaces.ContainsKey(hostName))
{
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://" + hostName + "/PowerShell/"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", PSCredential.Empty);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
var runspace = RunspaceFactory.CreateRunspace(connectionInfo);
// THIS CAUSES AN ERROR WHEN USING IT IN INSTALLER
runspace.Open();
this.m_runspaces.Add(hostName, runspace);
}
this.Host = hostName;
}
/// <summary>
/// Get Transport agent info
/// </summary>
/// <param name="Name">name of the transport agent</param>
/// <returns></returns>
public PSObject GetAgentInfo(string Name)
{
PSObject result = null;
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = this.currentRunspace;
ps.AddCommand("Get-TransportAgent");
ps.AddParameter("Identity", Name);
var res = ps.Invoke();
if (res != null && res.Count > 0)
result = res[0];
}
return result;
}
/// <summary>
/// get a list of exchange server available in the environment
/// </summary>
/// <returns>collection of powershell objects containing of all available exchange server</returns>
public ICollection<PSObject> GetExchangeServer()
{
ICollection<PSObject> result;
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = this.currentRunspace;
ps.AddCommand("Get-ExchangeServer");
result = ps.Invoke();
}
return result;
}
/// <summary>
/// Install a transport agent
/// </summary>
/// <param name="Name">name of the transportagent</param>
/// <param name="AgentFactory">factory name of the transport agent</param>
/// <param name="AssemblyPath">file path of the transport agent assembly</param>
/// <param name="enable">if true, enable it after successfully installed</param>
/// <returns>if true everything went ok, elsewise false</returns>
public bool InstallAgent(string Name, string AgentFactory, string AssemblyPath, bool enable = false)
{
bool success = false;
if (!System.IO.File.Exists(AssemblyPath))
throw new Exception("Assembly '"+AssemblyPath+"' for TransportAgent '"+ Name +"' not found");
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = this.currentRunspace;
ps.AddCommand("Install-TransportAgent");
ps.AddParameter("Name", Name);
ps.AddParameter("TransportAgentFactory", AgentFactory);
ps.AddParameter("AssemblyPath", AssemblyPath);
var result = ps.Invoke();
if (result.Count > 0)
{
if (enable)
success = this.EnableAgent(Name);
else
success = true;
}
}
return success;
}
/// <summary>
/// Enable a transport agent
/// </summary>
/// <param name="Name">name of the transport agent</param>
/// <returns>if true everything went ok, elsewise false</returns>
public bool EnableAgent(string Name){
bool success = false;
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = this.currentRunspace;
ps.AddCommand("Enable-TransportAgent");
ps.AddParameter("Identity", Name);
var result = ps.Invoke();
if (result.Count <= 0) success = true;
}
return success;
}
/// <summary>
/// removes a transport agent
/// </summary>
/// <param name="Name">name of the transport agent</param>
/// <returns>if true everything went ok, elsewise false</returns>
public bool UninstallAgent(string Name)
{
bool success = false;
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = this.currentRunspace;
ps.AddCommand("Uninstall-TransportAgent");
ps.AddParameter("Identity", Name);
ps.AddParameter("Confirm", false);
var result = ps.Invoke();
if (result.Count <= 0)success = true;
}
return success;
}
/// <summary>
/// restart exchange transport agent service
/// A RESTART OF THIS SERVICE REQUIRED WHEN INSTALLING A NEW AGENT
/// </summary>
/// <returns></returns>
public bool RestartTransportService()
{
bool success = false;
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = this.currentRunspace;
ps.AddCommand("Restart-Service");
ps.AddParameter("Name", "MSExchangeTransport");
var result = ps.Invoke();
if (result.Count <= 0) success = true;
}
return success;
}
public void Dispose()
{
if (this.m_runspaces.Count > 0)
{
foreach (var rs in this.m_runspaces.Values)
{
rs.Close();
}
this.m_runspaces.Clear();
}
}
}
Usage:
// Local
ExchangeShell shell = new ExchangeShell();
var localAgentInfo = shell.GetAgentInfo("YourAgentName");
// continue with a remote call
shell.SetRemoteHost("anotherexchangeserver.your.domain");
var remoteAgentInfo = shell.GetAgentInfo("YourAgentName");
// close all connections
shell.Dispose();

Related

Get FQDN using C# bypassing alias in hosts file

In a domain environment i have several clients which reference a server via an alias in the local hosts file. I need to obtain the real hostname of the server. When using Net.Dns.GetHostEntry or Net.Dns.Resolve only the alias name from the hosts file is returned.
var addr = System.Net.Dns.GetHostAddresses("FileServer")[0]; // assume at least one entry
var fqdn = System.Net.Dns.GetHostEntry(addr).HostName;
The reverse lookup using NSLOOKUP works as expected. Is it possible to force the Dns methods to ignore the hosts file?
You can do this calling NSLOOKUP on C#
Here is a method for c#.
/// <summary>
/// Get HostName from IPv4
/// </summary>
/// <param name="ipv4"></param>
/// <returns></returns>
public static string NslookupIP(string ipv4, string dnsServerIpv4)
{
if (IPAddress.TryParse(ipv4, out IPAddress ipaddress))
{
if (ipaddress.AddressFamily != AddressFamily.InterNetwork)
{
throw new Exception("ipv4 is no a valid IPv4");
}
}
else
{
throw new Exception("ipv4 is no a valid IPv4");
}
Process pProcess = new Process();
pProcess.StartInfo.FileName = "nslookup";
pProcess.StartInfo.Arguments = $"{ipv4}";
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string cmdOutput = pProcess.StandardOutput.ReadToEnd();
string pattern = #"^Name:\s*(?<host>(.*))$";
foreach (Match m in Regex.Matches(cmdOutput, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))
{
return m.Groups["host"].Value;
}
return "";
}

C# Dynamic Assembly with assembly information

In my code, I create a DLL dynamically. The following code is what I use to create my DLL:
/// <summary>
/// See CodeDom example hon how its used
/// </summary>
/// <param name="source_code"></param>
/// <param name="assemblies"></param>
/// <param name="generate_executable"></param>
/// <param name="compiler_options"></param>
/// <param name="output"></param>
/// <returns></returns>
public static Tuple<Assembly, string> CompileSource(string source_code, List<string> assemblies, bool generate_executable = false, string compiler_options = "/optimize", string output_assembly = "")
{
CompilerParameters compilerParams = null;
CSharpCodeProvider provider = null;
CompilerResults result = null;
string errors = string.Empty;
try
{
provider = new CSharpCodeProvider();
compilerParams = new CompilerParameters();
foreach (var entry in assemblies)
{
compilerParams.ReferencedAssemblies.Add(entry);
}
if (output_assembly != string.Empty)
compilerParams.OutputAssembly = output_assembly;
compilerParams.GenerateExecutable = generate_executable;
compilerParams.GenerateInMemory = false;
compilerParams.IncludeDebugInformation = true;
compilerParams.CompilerOptions = compiler_options;
result = provider.CompileAssemblyFromSource(compilerParams, source_code);
foreach (CompilerError error in result.Errors)
{
errors += String.Format("{0}({1},{2}: error {3}: {4}", error.FileName, error.Line, error.Column, error.ErrorNumber, error.ErrorText);
}
}
catch (Exception err)
{
}
return Tuple.Create(result.CompiledAssembly, errors);
}
What I would like to do is add Assembly Information like the company, product, title, etc.
Looking at this site and site to add it but I'm not sure how to do it.
The CompilerParameters doesn't seem to give an option to add these attributes.
Anyone have a suggestion if it can be done on how to add this information?
Thanks

Windows Azure Pack Authentication Using Node.js

I am trying to customize Windows Azure Pack using REST API. To authenticate the API, I need the token. I am able run the SampleAuthApplication (Code below), but that is in .NET . I want to implement the same thing in Node.js
Here is the Program.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
using System;
using System.IdentityModel.Protocols.WSTrust;
using System.IdentityModel.Tokens;
using System.Net;
using System.Net.Security;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Text;
namespace RDFEAPICallsv2
{
class Program
{
static void Main(string[] args)
{
//// Replace these values
string EnvironmentToUse = "https://wap";
string domainName = "anant.ad";
string userName = "anant/administrator";
string password = "pass#word";
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(delegate { return true; });
//// for Admin Authentication
string windowsAuthSiteEndPoint = EnvironmentToUse + ":30072";
string token = GetWindowsAuthToken(windowsAuthSiteEndPoint, domainName, userName, password, true);
//// for Tenant Authentication
string tenantServiceEndpoint = EnvironmentToUse + ":30071";
//string tenantToken = GetAspAuthToken(tenantServiceEndpoint, userName, password);
// Use the tokens obtained above in the Http client to make the calls
//var httpClient = new HttpClient();
//httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
public static string GetWindowsAuthToken(string windowsAuthSiteEndpoint, string domainName, string userName, string password, bool shouldImpersonate)
{
string token = null;
Impersonation.Impersonate(domainName, userName, password, () => token = GetWindowsToken(windowsAuthSiteEndpoint));
return token;
}
private static string GetWindowsToken(string windowsAuthSiteEndPoint)
{
var identityProviderEndpoint = new EndpointAddress(new Uri(windowsAuthSiteEndPoint + "/wstrust/issue/windowstransport"));
var identityProviderBinding = new WS2007HttpBinding(SecurityMode.Transport);
identityProviderBinding.Security.Message.EstablishSecurityContext = false;
identityProviderBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
identityProviderBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
var trustChannelFactory = new WSTrustChannelFactory(identityProviderBinding, identityProviderEndpoint)
{
TrustVersion = TrustVersion.WSTrust13,
};
trustChannelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None };
var channel = trustChannelFactory.CreateChannel();
var rst = new RequestSecurityToken(RequestTypes.Issue)
{
AppliesTo = new EndpointReference("http://azureservices/AdminSite"),
KeyType = KeyTypes.Bearer,
};
RequestSecurityTokenResponse rstr = null;
SecurityToken token = null;
token = channel.Issue(rst, out rstr);
var tokenString = (token as GenericXmlSecurityToken).TokenXml.InnerText;
var jwtString = Encoding.UTF8.GetString(Convert.FromBase64String(tokenString));
return jwtString;
}
static string GetAspAuthToken(string authSiteEndPoint, string userName, string password)
{
var identityProviderEndpoint = new EndpointAddress(new Uri(authSiteEndPoint + "/wstrust/issue/usernamemixed"));
var identityProviderBinding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential);
identityProviderBinding.Security.Message.EstablishSecurityContext = false;
identityProviderBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
identityProviderBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
var trustChannelFactory = new WSTrustChannelFactory(identityProviderBinding, identityProviderEndpoint)
{
TrustVersion = TrustVersion.WSTrust13,
};
//This line is only if we're using self-signed certs in the installation
trustChannelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None };
trustChannelFactory.Credentials.SupportInteractive = false;
trustChannelFactory.Credentials.UserName.UserName = userName;
trustChannelFactory.Credentials.UserName.Password = password;
var channel = trustChannelFactory.CreateChannel();
var rst = new RequestSecurityToken(RequestTypes.Issue)
{
AppliesTo = new EndpointReference("http://azureservices/TenantSite"),
TokenType = "urn:ietf:params:oauth:token-type:jwt",
KeyType = KeyTypes.Bearer,
};
RequestSecurityTokenResponse rstr = null;
SecurityToken token = null;
token = channel.Issue(rst, out rstr);
var tokenString = (token as GenericXmlSecurityToken).TokenXml.InnerText;
var jwtString = Encoding.UTF8.GetString(Convert.FromBase64String(tokenString));
return jwtString;
}
}
}
This is Impersonation.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using Microsoft.Win32.SafeHandles;
namespace RDFEAPICallsv2
{
public static class Impersonation
{
public enum LogonType
{
/// <summary>
/// This logon type is intended for users who will be interactively using the computer, such as a user being logged on
/// by a terminal server, remote shell, or similar process.
/// This logon type has the additional expense of caching logon information for disconnected operations;
/// therefore, it is inappropriate for some client/server applications,
/// such as a mail server.
/// </summary>
LOGON32_LOGON_INTERACTIVE = 2,
/// <summary>
/// This logon type is intended for high performance servers to authenticate plaintext passwords.
/// The LogonUser function does not cache credentials for this logon type.
/// </summary>
LOGON32_LOGON_NETWORK = 3,
/// <summary>
/// This logon type is intended for batch servers, where processes may be executing on behalf of a user without
/// their direct intervention. This type is also for higher performance servers that process many plaintext
/// authentication attempts at a time, such as mail or Web servers.
/// The LogonUser function does not cache credentials for this logon type.
/// </summary>
LOGON32_LOGON_BATCH = 4,
/// <summary>
/// Indicates a service-type logon. The account provided must have the service privilege enabled.
/// </summary>
LOGON32_LOGON_SERVICE = 5,
/// <summary>
/// This logon type is for GINA DLLs that log on users who will be interactively using the computer.
/// This logon type can generate a unique audit record that shows when the workstation was unlocked.
/// </summary>
LOGON32_LOGON_UNLOCK = 7,
/// <summary>
/// This logon type preserves the name and password in the authentication package, which allows the server to make
/// connections to other network servers while impersonating the client. A server can accept plaintext credentials
/// from a client, call LogonUser, verify that the user can access the system across the network, and still
/// communicate with other servers.
/// NOTE: Windows NT: This value is not supported.
/// </summary>
LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
/// <summary>
/// This logon type allows the caller to clone its current token and specify new credentials for outbound connections.
/// The new logon session has the same local identifier but uses different credentials for other network connections.
/// NOTE: This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
/// NOTE: Windows NT: This value is not supported.
/// </summary>
LOGON32_LOGON_NEW_CREDENTIALS = 9,
}
public enum LogonProvider
{
/// <summary>
/// Use the standard logon provider for the system.
/// The default security provider is negotiate, unless you pass NULL for the domain name and the user name
/// is not in UPN format. In this case, the default provider is NTLM.
/// NOTE: Windows 2000/NT: The default security provider is NTLM.
/// </summary>
LOGON32_PROVIDER_DEFAULT = 0,
/// <summary>
/// Win NT 3.5
/// </summary>
LOGON32_PROVIDER_WINNT35 = 1,
/// <summary>
/// Win NT 4.0
/// </summary>
LOGON32_PROVIDER_WINNT40 = 2,
/// <summary>
/// Win NT 5.0
/// </summary>
LOGON32_PROVIDER_WINNT50 = 3
}
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool LogonUser(
string username,
string domain,
string password,
LogonType logonType,
LogonProvider logonProvider,
out SafeTokenHandle token);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr handle);
public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeTokenHandle()
: base(true)
{
}
protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
}
public static void Impersonate(string domain, string username, string password, Action handler)
{
SafeTokenHandle safeHandle;
var result = LogonUser(
username,
domain == null ? "." : domain,
password,
LogonType.LOGON32_LOGON_NEW_CREDENTIALS,
LogonProvider.LOGON32_PROVIDER_WINNT50,
out safeHandle);
if (!result)
{
int ret = Marshal.GetLastWin32Error();
throw new Win32Exception(ret);
}
using (safeHandle)
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeHandle.DangerousGetHandle()))
{
handler();
}
}
}
}
identityProviderBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
According to your WCF code, seems that this sample used the windows authentication. If you wanna use node.js to achieve to this result, you maybe need to use the passport.js(https://github.com/auth0/passport-windowsauth) implement the windows authentication.
Also, you can use other idp to authentication your application. Please refer to the official document :https://msdn.microsoft.com/en-us/library/dn508295.aspx

How to use tesseract 3.02 trained data in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I can able to get the proper OCR output using newly trained tessedata (version 3.02) through command prompt but I want same output in C# code with DLL ref.I have tried with tessnet2_32.dll reference but It is throwing exception so How to use or access the tesseract 3.02 version trained tessedata using DLL reference through C# code?
To access or use tesseract 3.02 trained data we have to create separate wrapper class like below.
using System;
using System.IO;
using System.Diagnostics;
using System.Drawing;
/// <summary>
/// Summary description for TesseractOCR
/// </summary>
///
namespace tesseractThree
{
public class TesseractOCR
{
public TesseractOCR()
{
//
// TODO: Add constructor logic here
//
}
private string commandpath;
private string outpath;
private string tmppath;
public TesseractOCR(string commandpath)
{
this.commandpath = commandpath;
tmppath = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\out.tif";
outpath = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\out.txt";
}
public string analyze(string filename,string lang,bool noLine)
{
string args = filename + " " + outpath.Replace(".txt", "");
ProcessStartInfo startinfo;
if (noLine == true)
{
startinfo = new ProcessStartInfo(commandpath, args + " -l " + lang + " -psm 6");
}
else
{
startinfo = new ProcessStartInfo(commandpath, args + " -l " + lang);
}
startinfo.CreateNoWindow = true;
startinfo.UseShellExecute = false;
Process.Start(startinfo).WaitForExit();
string ret = "";
using (StreamReader r = new StreamReader(outpath))
{
string content = r.ReadToEnd();
ret = content;
}
File.Delete(outpath);
return ret;
}
public string OCRFromBitmap(Bitmap bmp,string lang,bool noLine)
{
bmp.Save(tmppath, System.Drawing.Imaging.ImageFormat.Tiff);
string ret = analyze(tmppath,lang,noLine);
File.Delete(tmppath);
return ret;
}
/* public string OCRFromFile(string filename)
{
return analyze(filename);
}*/
}
}
//Usage of this class
string lang = "enc";
Bitmap b = new Bitmap(#"D:\Image\enc.test_font.exp0.tif");
TesseractOCR ocr = new TesseractOCR(#"C:\Program Files\Tesseract-OCR\tesseract.exe");
string result = ocr.OCRFromBitmap(b, lang,true);
Label1.Text = result;
OR Refer below link for more details.
https://gist.github.com/yatt/915443
That was for Tesseract 2.04. You'll need a .NET wrapper compatible with 3.02 version.
Using the tesseractengine3.dll we can use tesseract v3.02 trained data like below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using tesseract;
using System.Drawing;
using System.IO;
public enum TesseractEngineMode : int
{
/// <summary>
/// Run Tesseract only - fastest
/// </summary>
TESSERACT_ONLY = 0,
/// <summary>
/// Run Cube only - better accuracy, but slower
/// </summary>
CUBE_ONLY = 1,
/// <summary>
/// Run both and combine results - best accuracy
/// </summary>
TESSERACT_CUBE_COMBINED = 2,
/// <summary>
/// Specify this mode when calling init_*(),
/// to indicate that any of the above modes
/// should be automatically inferred from the
/// variables in the language-specific config,
/// command-line configs, or if not specified
/// in any of the above should be set to the
/// default OEM_TESSERACT_ONLY.
/// </summary>
DEFAULT = 3
}
public enum TesseractPageSegMode : int
{
/// <summary>
/// Fully automatic page segmentation
/// </summary>
PSM_AUTO = 0,
/// <summary>
/// Assume a single column of text of variable sizes
/// </summary>
PSM_SINGLE_COLUMN = 1,
/// <summary>
/// Assume a single uniform block of text (Default)
/// </summary>
PSM_SINGLE_BLOCK = 2,
/// <summary>
/// Treat the image as a single text line
/// </summary>
PSM_SINGLE_LINE = 3,
/// <summary>
/// Treat the image as a single word
/// </summary>
PSM_SINGLE_WORD = 4,
/// <summary>
/// Treat the image as a single character
/// </summary>
PSM_SINGLE_CHAR = 5
}
public partial class importDLL : System.Web.UI.Page
{
private TesseractProcessor m_tesseract = null;
//private const string m_path = #"..\..\data\";
private const string m_path = #"D:\tessdata-3.02\";
private const string m_lang = "eng";
protected void Page_Load(object sender, EventArgs e)
{
var image = System.Drawing.Image.FromFile(#"D:\Image\Capture1T.tif");
m_tesseract = new TesseractProcessor();
bool succeed = m_tesseract.Init(m_path, m_lang, (int)TesseractEngineMode.DEFAULT);
if (!succeed)
{
}
m_tesseract.SetVariable("tessedit_pageseg_mode", ((int)TesseractPageSegMode.PSM_SINGLE_LINE).ToString());
m_tesseract.Clear();
m_tesseract.ClearAdaptiveClassifier();
string outValue= m_tesseract.Apply(image);
Response.Write(outValue);
}
}

Retrieve Access Information Google gdata-api C#

I am attempting to retrieve the most recent date that my users have logged into any Google app service such as Gmail (or had this done for them via mobile device/imap/pop etc). I am an administrator of the domain on Google apps for Education and using the C# gdata-api.
I would like to recycle student usernames if they have been gone for longer than a year without signing into their gmail apps and require the last accessed date to accomplish this.
Please let me know if this is possible
Thank you.
I used the gmail reporting API. I forget where I got this from but I stole it from somewhere. If you know where, please put a comment link.
/// <summary>
/// This contains the logic for constructing and submitting a report
/// request to the Google Apps reporting service and reading the response.
///
/// The description of the web service protocol can be found at:
///
/// http://code.google.com/apis/apps/reporting/google_apps_reporting_api.html
///
/// Example usage:
/// Get the latest accounts report to standard output.
/// client.getReport("accounts", null, null);
/// Get the accounts report for May 15, 2007 and save it to out.txt.
/// client.getReport("accounts", "2007-05-15", "out.txt");
/// </summary>
public class ReportsManager
{
/// <summary>
/// URL to POST to obtain an authentication token
/// </summary>
private const string CLIENT_LOGIN_URL =
"https://www.google.com/accounts/ClientLogin";
/// <summary>
/// URL to POST to retrive resports from the API
/// </summary>
private const string REPORTING_URL =
"https://www.google.com/hosted/services/v1.0/reports/ReportingData";
/// <summary>
/// Date format of the API
/// </summary>
private const string DATE_FORMAT = "yyyy-MM-dd";
/// <summary>
/// Hour of the day when the API data gets published
/// </summary>
private const int PUBLISH_HOUR_OF_DAY = 13; // Publish hour + 1 hour;
/// <summary>
/// Time diference to UTC
/// </summary>
private const int PUBLISH_TIME_DIFERENCE_TO_UTC = -8;
/// <summary>
/// Email command-line argument
/// </summary>
private const string EMAIL_ARG = "email";
/// <summary>
/// Password command-line argument
/// </summary>
private const string PASSWORD_ARG = "password";
/// <summary>
/// Domain command-line argument
/// </summary>
private const string DOMAIN_ARG = "domain";
/// <summary>
/// Report command-line argument
/// </summary>
private const string REPORT_ARG = "report";
/// <summary>
/// Date command-line argument
/// </summary>
private const string DATE_ARG = "date";
/// <summary>
/// Output File command-line argument
/// </summary>
private const string OUT_FILE_ARG = "out";
/// <summary>
/// Message for command-line usage
/// </summary>
private const string USAGE = "Usage: " +
"ReportingAPI --" + EMAIL_ARG + " <email> --" +
PASSWORD_ARG + " <password> [ --" +
DOMAIN_ARG + " <domain> ] --" +
REPORT_ARG + " <report name> [ --" +
DATE_ARG + " <YYYY-MM-DD> ] [ --" +
OUT_FILE_ARG + " <file name> ]";
/// <summary>
/// List of command-line arguments
/// </summary>
private static string[] PROPERTY_NAMES = new String[] {EMAIL_ARG,
PASSWORD_ARG, DOMAIN_ARG, REPORT_ARG, DATE_ARG, OUT_FILE_ARG};
/// <summary>
/// List of required command-line arguments
/// </summary>
private static string[] REQUIRED_PROPERTY_NAMES = new String[] {
EMAIL_ARG, PASSWORD_ARG, REPORT_ARG};
/// <summary>
/// Google Apps Domain
/// </summary>
public string domain = null;
/// <summary>
/// Email address of an Administrator account
/// </summary>
public string email = null;
/// <summary>
/// Password of the Administrator account
/// </summary>
public string password = null;
/// <summary>
/// Identifies the type of account
/// </summary>
private string accountType = "HOSTED";
/// <summary>
/// Identifies the Google service
/// </summary>
private string service = "apps";
/// <summary>
/// Contains a token value that Google uses to authorize
/// access to the requested report data
/// </summary>
private string token = null;
/// <summary>
/// Default constructor
/// </summary>
public ReportsManager(string username, string password, string domain)
{
this.email = username + "#" + domain;
this.password = password;
this.domain = domain;
}
/// <summary>
/// Retrieves the Authentication Token
/// </summary>
/// <returns>Returns the authentication token.</returns>
public string GetToken()
{
return this.token;
}
/// <summary>
/// Logs in the user and initializes the Token
/// </summary>
public void ClientLogin()
{
string token = null;
UTF8Encoding encoding = new UTF8Encoding();
string postData = "Email=" + System.Web.HttpUtility.UrlEncode(this.email) +
"&Passwd=" + System.Web.HttpUtility.UrlEncode(this.password) +
"&accountType=" + System.Web.HttpUtility.UrlEncode(this.accountType) +
"&service=" + System.Web.HttpUtility.UrlEncode(this.service);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(CLIENT_LOGIN_URL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream inputStream = request.GetRequestStream();
inputStream.Write(data, 0, data.Length);
inputStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseStr = (new StreamReader(
response.GetResponseStream())).ReadToEnd();
char[] tokenizer = { '\r', '\n' };
string[] parts = responseStr.Split(tokenizer);
foreach (string part in parts)
{
if (part.StartsWith("SID="))
{
token = part.Substring(4);
break;
}
}
this.token = token;
}
/// <summary>
/// Creates a XML request for the Report
/// </summary>
/// <param name="reportName">The name of the Report: activity,
/// disk_space, email_clients, quota_limit_accounts,
/// summary, suspended_account</param>
/// <param name="date">Date of the Report</param>
/// <returns>Thx XML request as a string</returns>
public string createRequestXML(string reportName, string date)
{
if (this.domain == null)
{
this.domain = getAdminEmailDomain();
}
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml += "<rest xmlns=\"google:accounts:rest:protocol\"";
xml += " xmlns:xsi=\"";
xml += "http://www.w3.org/2001/XMLSchema-instance\">";
xml += "<type>Report</type>";
xml += "<token>" + this.GetToken() + "</token>";
xml += "<domain>" + this.domain + "</domain>";
xml += "<date>" + date + "</date>";
xml += "<reportType>daily</reportType>";
xml += "<reportName>" + reportName + "</reportName>";
xml += "</rest>";
return xml;
}
/// <summary>
/// Get the domain of the admin's email address.
/// </summary>
/// <returns>the domain, otherwise returns null</returns>
public string getAdminEmailDomain()
{
if (this.email != null)
{
int atIndex = this.email.IndexOf('#');
if (atIndex > 0 && atIndex + 1 < this.email.Length)
{
return this.email.Substring(atIndex + 1);
}
}
else
{
throw new ArgumentNullException("Invalid Email");
}
return null;
}
public enum ReportType
{
accounts,
activity,
disk_space,
email_clients,
quota_limit_accounts,
summary,
suspended_account
}
/// <summary>
/// Get the reports by reportName for a Date, and writes the report
/// at filename /// or to the console if filename is null.
/// </summary>
/// <param name="reportName">The name of the Report: activity,
/// disk_space, email_clients, quota_limit_accounts,summary,
/// suspended_account</param>
/// <param name="date">Date of the Report,
/// null date gets latest date available</param>
private Dictionary<string, ArrayList> getReport(string reportName, string date)
{
if (date == null)
{
date = getLatestReportDate().ToString(DATE_FORMAT);
}
else
{
try
{
date = System.Convert.ToDateTime(date).ToString
(DATE_FORMAT);
}
catch
{
throw new ArgumentException("Invalid Date");
}
}
string xml = createRequestXML(reportName, date);
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(REPORTING_URL);
request.Method = "POST";
UTF8Encoding encoding = new UTF8Encoding();
byte[] postBuffer = encoding.GetBytes(xml);
request.ContentLength = postBuffer.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBuffer, 0, postBuffer.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(
response.GetResponseStream());
String firstLine = null;
String lineBuffer = String.Empty;
if (reader.Peek() >= 0)
{
firstLine = reader.ReadLine();
checkError(firstLine, reader);
}
Dictionary<string, ArrayList> csv = new Dictionary<string, ArrayList>();
string[] headers = null;
if (firstLine != null)
{
headers = firstLine.Split(',');
foreach (string header in headers)
{
csv.Add(header, new ArrayList());
}
}
if (headers != null)
{
while ((lineBuffer = reader.ReadLine()) != null)
{
string[] dataLine = lineBuffer.Split(',');
for (int i = 0; i < csv.Keys.Count; i++)
{
csv[headers[i]].Add(dataLine[i]);
}
}
}
reader.Close();
return csv;
}
/// <summary>
/// Get the reports by reportName for a Date, and writes the report
/// at filename or to the console if filename is null.
/// </summary>
/// <param name="reportName">The name of the Report: activity,
/// disk_space, email_clients, quota_limit_accounts,summary,
/// suspended_account</param>
/// <param name="date">
/// Date of the Report, null date gets latest date available</param>
public Dictionary<string, ArrayList> getReport(ReportType reportType, DateTime date)
{
string reportName = Enum.GetName(typeof(ReportType), reportType);
return this.getReport(reportName, date.ToString(DATE_FORMAT));
}
/// <summary>
/// Checks for errors on the Http Response, errors are on XML format.
/// When the response is xml throws an Exception with the xml text.
/// </summary>
/// <param name="firstLine">
/// First line of the StreamReader from the Http Response</param>
/// <param name="reader">StreamReader from the Http Response</param>
private void checkError(string firstLine, StreamReader reader)
{
if (firstLine.Trim().StartsWith("<?xml"))
{
String xmlText = firstLine;
while (reader.Peek() >= 0)
{
xmlText += reader.ReadLine();
}
throw new Exception(xmlText);
}
}
/// <summary>
/// Get latest available report date,
/// based on report service time zone.
/// Reports for the current date are available after 12:00 PST8PDT
/// the following day.
/// </summary>
/// <returns>Lastest date available</returns>
public DateTime getLatestReportDate()
{
if (DateTime.UtcNow.AddHours(PUBLISH_TIME_DIFERENCE_TO_UTC).Hour
< PUBLISH_HOUR_OF_DAY)
{
return DateTime.Now.AddDays(-2);
}
else
{
return DateTime.Now.AddDays(-1);
}
}
/// <summary>
/// Gets the properties from the command-line arguments.
/// </summary>
/// <param name="args">command-line arguments</param>
/// <returns>Properties Hashtable</returns>
private static Hashtable getProperties(string[] args)
{
Hashtable properties = new Hashtable();
for (int i = 0; i < args.Length; i++)
{
bool found = false;
for (int j = 0; j < PROPERTY_NAMES.Length; j++)
{
if (args[i].Equals("--" + PROPERTY_NAMES[j]))
{
found = true;
if (i + 1 < args.Length)
{
properties.Add(PROPERTY_NAMES[j], args[i + 1]);
i++;
break;
}
else
{
throw new ArgumentException("Missing value for " +
"command-line parameter " + args[i]);
}
}
}
if (!found)
{
throw new ArgumentException(
"Unrecognized parameter " + args[i]);
}
}
for (int i = 0; i < REQUIRED_PROPERTY_NAMES.Length; i++)
{
if (properties[REQUIRED_PROPERTY_NAMES[i]] == null)
{
throw new ArgumentException("Missing value for " +
"command-line parameter " + REQUIRED_PROPERTY_NAMES[i]);
}
}
return properties;
}
}
To use it:
ReportsManager reports = new ReportsManager("*", "*", "*");
reports.ClientLogin();
Dictionary<string, ArrayList> accountReport = reports.getReport(ReportsManager.ReportType.accounts, DateTime.Today);
int count = accountReport["account_name"].Count;
Hashtable usersLastLoggedIn = new Hashtable();
for (int i = 0; i < count; i++)
{
DateTime lastLogged = DateTime.Parse(accountReport["last_login_time"][i].ToString());
DateTime lastWebMail = DateTime.Parse(accountReport["last_web_mail_time"][i].ToString());
DateTime lastPop = DateTime.Parse(accountReport["last_pop_time"][i].ToString());
if (lastWebMail > lastLogged) { lastLogged = lastWebMail; }
if (lastPop > lastLogged) { lastLogged = lastPop; }
usersLastLoggedIn.Add(accountReport["account_name"][i].ToString().Replace('#' + ConfigurationManager.AppSettings["domain"], string.Empty), lastLogged);
}

Categories