Advertisements

Archive

Archive for the ‘Misc’ Category

C# Error while calling API – Could not create SSL/TLS secure channel

Other day, we were getting following error while calling SSL enables 3rd party API from C# console.

The request was aborted: Could not create SSL/TLS secure channel

Fix:

  • Add below statement before making your API call, which validates the SSL certification.

ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};

  • Include following namespaces

using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

🙂

 

Advertisements
Categories: Misc Tags: , ,

Post a File and Data to SandBlast API using Postman and generate C# code

We got a requirement to use SandBlast Threat Extraction  process, for cleansing the files.

SandBlast Threat Extraction, is a solution by ‘Check Point’ company,  which removes exploitable content, including active content and embedded objects, reconstructs files to eliminate potential threats.

‘Threat Extraction’ Upload API, requires following parameters

  • Header
    • Authorization
    • Content-Type
  • Body
    • File Content
    • request (This is string)

If you notice, we need to pass ‘File’ as well as ‘Data’ to the API.

As of now, Sandblast doesn’t have a native .Net library to make API calls. So I’ve used Postman tool as a starting point for my development.

Below is the step-by-step, to pass ‘File’ and ‘Data’ in request ‘Body’, using Postman

  • Open Postman tool
  • Create a new ‘Request’

PM_2

  • Set the method as ‘Post’ and URL.
  • From the ‘Body’ tab, select ‘form-data’ option
  • To pass the ‘File’,  add a new ‘KEY’ of type ‘File’ and browse the file in ‘VALUE’ parameter.
  • To pass the ‘Data’, add a new ‘KEY’ of type ‘Text’ and set the ‘VALUE’

PM_1

Generate C# code from the Request:

  • Postman has an amazing feature to generate the code in your required language.
  • Click on ‘Code’ link and for C#, choose ‘C# (RestSharp)’ option.

PM_3

If anyone having struggling to use Sandblast API’s, post in comments section and I will address.

🙂

 

Categories: Misc Tags: , , ,

HTML & JScript – Read and Encode file

We got a requirement in ADX portal, to read and convert the file content to Base64String , which eventually will be submitted to an external API.

Below is the code snippet to read the File content, browsed using the HTML ‘File Upload’ control:

function readFileAsText(){
try {
// Read the browsed file from ‘File Upload’ control.
var fileToLoad = document.getElementById(“fileOE“).files[0];

var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
// Set the file content to variable
var textFromFileLoaded = fileLoadedEvent.target.result;
alert(“File Content – ” + textFromFileLoaded);

// Using ‘window.btoa’ encode the plain text to Base 64
var convertedString = window.btoa(unescape(encodeURIComponent(textFromFileLoaded)));
alert(“Base 64 content – ” + convertedString);
};

fileReader.readAsText(fileToLoad, “UTF-8”);
} catch (e) {
alert(“Error in readFileAsText(); Error – ” + e.description);
}
}

<body>
<label for=”fileOE”>Pick the file </label><br>
<input type=”file” id=”fileOE” />
<input type=”button” value=”Upload” onclick=”readFileAsText()” />
</body>

  • Execute the code and you will get output as below
jScript_1

File content as text

jScript_2

Encoded File Content (Base 64)

🙂

Categories: Misc Tags: , , , ,

Unable to install ‘Windows Service’ using installutil.exe

February 4, 2019 Leave a comment

The other day, I was using Installutil.exe from Command Prompt (refer below command), to install my Windows Service.

C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe -i “X:\XX\XXX.exe”

Although the tool was giving success response, Windows Service was not getting installed and not being listed in ‘Service Manager’ console.

Alternate Option:

  • While troubleshooting the issue, we came across another option to install the Windows Service using “SC Create

Using SC Create:

To install Windows Service,

  • Open the Command Prompt in ‘Run as administrator’ mode.
  • Below is the syntax using ‘SC Create’ to install the service.

C:\Windows\system32>SC Create “{Your WinService exe name}” binpath=”{Your WinService exe path}” displayname=”{Your desired name}

SCCreate_1

Using SC Delete:

To Uninstall the ‘Windows Service’,

  • Open the Command Prompt in ‘Run as administrator’ mode.
  • Below is the syntax using ‘SC Delete’ to Uninstall the service.

C:\Windows\system32>SC Delete “{Your WinService exe name}

🙂

[MS Word] The Linked file isn’t available

December 19, 2018 Leave a comment

Other day, I was getting below “The Linked file isn’t available” pop-up, while opening a word document shared by client.

Word_1

Reason:

  • Word document has charts and graphs controls embedded, which were rendering based on the data from another excel sheet.
  • Excel sheet had configured as a linked file in the Word document
  • Since Excel sheet was not shared by client in the first go, Word document was showing “The Linked file isn’t available” popup.

Fix:

  • Once you got the link file (i.e., Excel sheet in my case), below are the steps to fix, if you are still getting the popup.
  • From Word document, click on File -> Info -> Related Documents -> Edit Links to Files

Word_2

  • In the ‘Links’ window, either click on ‘Change Source’ to correct the ‘Link file’ path or click ‘Break Link’ button if you want to remove the files link.

Word_3

  • Once the file paths are corrected, click on ‘Update Now’ to reflect the changes in Word document
  • Now, Word document should open with no “The Linked file isn’t available” pop-up.

🙂

Categories: Misc Tags: ,

[Log4net] Exception while reading ConfigurationSettings

December 16, 2018 Comments off

We were using Log4net logging framework to log exceptions in our web application and encountered below exception during the log:

Log4Net_1

Reasons:

There can be many reasons right from misplacing Log4net config settings to not loading the config file.

Fix:

Below is the step by step approach on how to use Log4net framework.

Install Log4net NuGet package:

  • From Visual Studio, open Tools -> NuGet Package Manager -> Manage NuGet packages for solution…

Log4Net_2

  • Search for ‘Log4net’ and install the package

Log4Net_3

Add configurations in App.config/Web.config:

  • In your App.config/Web.config file, add a new config section named ‘log4net’. Make sure <configSections> is the first node under root <configuration> node.
  • Add <log4net> node with details like Path where you would like to store the logs and other properties.
  • Your App.config/Web.config file looks as below:

<configuration>
<configSections>
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler,Log4net”/>
</configSections>
<log4net>
<appender name=”General” type=”log4net.Appender.RollingFileAppender”>
<file value=”D:\log.txt“/> <!–Physical path to log files–>
<appendToFile value=”true”/>
<rollingStyle value=”Size”/>
<!– Specifies the number of files that will be written. This is an example for ten files. The files will be named engine.log.1, engine.log.2, engine.log.3, etc. –>
<maxSizeRollBackups value=”50″/>
<!– The File Size limit for each file. KB stands for Kilobytes, MB (MegaByte) is also an option. –>
<maximumFileSize value=”5MB”/>
<staticLogFileName value=”true”/>
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%date %-5level – %message%newline”/>
</layout>
</appender>
<root>
<appender-ref ref=”General”/>
</root>
</log4net>
<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.6.1″ />
</startup>
</configuration>

Using Log4net in your code:

  • We need to load the Log4net configurations defined App.config file in class file using below statement
    • Below statement can be either placed in your Class file or project’s “AssemblyInfo.cs” file

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

  • Define LogManager Object using below statement

private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

  • Use Log.Info(“”) to log info and Log.Error(“”) to log exception details.
  • With above specified statements your class file looks as below

Log4Net_5

using log4net;

// Load Log4net configurations defined App.config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Log4Net{
class Program{
// Define LogManager Object
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

static void Main(string[] args){
try{
Log.Info(“Log Starts”);
// Throw error
throw new Exception(“Exception!!!!”);
}
catch (Exception ex){
Log.Error(“Error – ” + ex.Message);
}
finally{
Log.Info(“Log Ends”);
Console.ReadLine();
}
}}

  • Execute the logic and you should get a log file stored in specified location with logs

Log4Net_4

Refer article for Log4net best practices.

🙂

Categories: Misc Tags: ,

Troubleshoot and Fix – Unable to access FTP folder error

September 12, 2018 Leave a comment

I was working on a POC/Prototype to read files from FTP location using console, for which I configured a FTP Site in my Windows 10 machine’s IIS.

FTP_1

FTP site connectivity worked fine till I switched my internet connectivity from LAN cable to WIFI, I started getting unable to access the FTP location error, when I access the FTP url from my console application.

Reason & Fix:

  • During the FTP web site configuration, we have to provide the “IP address:” which is specific to the machine.

FTP_2

  • When I switched from LAN to WIFI, my machine’s “IP address” has got changed.
  • Providing the correct “IP address” in ‘FTP web site’ binding solved the issue.
  • To check the “IP Address” of the machine
    • Open the ‘Command Prompt’
    • Execute ‘ipconfig‘ command
    • Copy the IP Address from “IPv4 Address”
    • Set the copied IPv4 Address in ‘FTP web site’ binding in IIS.

FTP_3

Refer this article on how to set up FTP website.

🙂