Wednesday, 18 March 2020

Web API authentication using certificate

RestSharp Example:




Get Example 
NameSpace

using RestSharp;

public IRestResponse GetExample(string URL)
{

var client = new RestClient(URL);
var request = new RestRequest(Method.GET);
//Add Header value
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
IRestResponse response = client.Execute(request);
return response;
}

Get Example  With Certificate 
public IRestResponse GetExampleWithCertificate(string URL)
{

var client = new RestClient(URL);
var request = new RestRequest(Method.GET);
//Add Header value
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
  X509Certificate2 certificate = new X509Certificate2();
 certificate.Import(System.Web.Hosting.HostingEnvironment.MapPath("Cerficate"), "", X509KeyStorageFlags.MachineKeySet);
                client.ClientCertificates = new X509CertificateCollection() { certificate };
IRestResponse response = client.Execute(request);

 SecurityProtocolType securityProtocols = ServicePointManager.SecurityProtocol;
            if (securityProtocols.HasFlag(SecurityProtocolType.Ssl3) || securityProtocols.HasFlag(SecurityProtocolType.Tls) || securityProtocols.HasFlag(SecurityProtocolType.Tls11))
            {
                securityProtocols &= ~(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11);
                if (securityProtocols == 0)
                {
                    securityProtocols |= SecurityProtocolType.Tls12;
                }
                ServicePointManager.SecurityProtocol = securityProtocols;
            }
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
IRestResponse response = client.Execute(request);
return response;
}



Post Example

public IRestResponse PostExample(string URL)
{

var client = new RestClient(URL);
var request = new RestRequest(Method.Post);
//Add Header value
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
//Post Json
request.AddParameter("application/json", Requestjson, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
return response;
}

Get Example  With Certificate 
public IRestResponse PostExampleWithCertificate(string URL)
{

var client = new RestClient(URL);
var request = new RestRequest(Method.Post);
//Add Header value
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
//Post Json
request.AddParameter("application/json", Requestjson, ParameterType.RequestBody);
// Add Certificate
  X509Certificate2 certificate = new X509Certificate2();
 certificate.Import(System.Web.Hosting.HostingEnvironment.MapPath("Cerficate"), "", X509KeyStorageFlags.MachineKeySet);
                client.ClientCertificates = new X509CertificateCollection() { certificate };
IRestResponse response = client.Execute(request);

 SecurityProtocolType securityProtocols = ServicePointManager.SecurityProtocol;
            if (securityProtocols.HasFlag(SecurityProtocolType.Ssl3) || securityProtocols.HasFlag(SecurityProtocolType.Tls) || securityProtocols.HasFlag(SecurityProtocolType.Tls11))
            {
                securityProtocols &= ~(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11);
                if (securityProtocols == 0)
                {
                    securityProtocols |= SecurityProtocolType.Tls12;
                }
                ServicePointManager.SecurityProtocol = securityProtocols;
            }
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
IRestResponse response = client.Execute(request);
return response;
}

No comments:

Post a Comment

Upload valid file in C#

    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)     {         try         {             Dictionary<string, byte[]>...