In this article you will learn, how to use gst web api for validate GSTIN in asp.net c#
using System;
using System.Collections.Generic;
using System.Linq
using System.Text;
using System.Threading.Tasks;
namespace EInvoiceAuthDotNet
{
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace EinvoiceAuth
{
class EwbAuth
{
static void Main(string[] args)
{
call_server_api();
}
public static void call_server_api()
{
try
{
string public_key = "<Public Key>";
HttpClient client = new HttpClient();
string uri = "<URL>/v1.03/auth";
client.DefaultRequestHeaders.Add("client-id", "<Client Id>");
client.DefaultRequestHeaders.Add("client-secret", "<Client Secret>");
string userName = "< User Id >";
string password = "< Password >";
client.DefaultRequestHeaders.Add("gstin", "<GSTIN>");
byte[] _aeskey = generateSecureKey();
string straesKey = Convert.ToBase64String(_aeskey);
RequestPayloadN aRequestPayload = new RequestPayloadN();
Auth data = new Auth();
data.Action = "ACCESSTOKEN";
data.Password = password;
data.App_Key = straesKey;
data.UserName = userName;
string authStr = JsonConvert.SerializeObject(data);
byte[] authBytes = System.Text.Encoding.UTF8.GetBytes(authStr);
aRequestPayload.Data = Encrypt(Convert.ToBase64String(authBytes), public_key);
string abc = JsonConvert.SerializeObject(aRequestPayload);
HttpResponseMessage res = client.PostAsJsonAsync(uri, aRequestPayload).Result;
if (res.IsSuccessStatusCode)
{
Console.WriteLine("Call is success");
string verification = res.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Response{verification}");
AuthResponse authResponse = res.Content.ReadAsAsync<AuthResponse>().Result;
string sek = DecryptBySymmerticKey(authResponse.Data.Sek, _aeskey);
Console.WriteLine($"Sek {sek}");
}
else
{
var stream = res.Content.ReadAsStreamAsync().Result;
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
string err = res.ReasonPhrase;
Console.WriteLine($"error Response{text} reason{err}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static string DecryptBySymmerticKey(string encryptedText, byte[] key)
{
try
{
byte[] dataToDecrypt = Convert.FromBase64String(encryptedText);
var keyBytes = key;
AesManaged tdes = new AesManaged();
tdes.KeySize = 256;
tdes.BlockSize = 128;
tdes.Key = keyBytes;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform decrypt__1 = tdes.CreateDecryptor();
byte[] deCipher = decrypt__1.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
tdes.Clear();
string EK_result = Convert.ToBase64String(deCipher);
// var EK = Convert.FromBase64String(EK_result);
// return EK;
return EK_result;
}
catch (Exception ex)
{
throw ex;
}
}
public static byte[] generateSecureKey()
{
Aes KEYGEN = Aes.Create();
byte[] secretKey = KEYGEN.Key;
return secretKey;
}
public static string Encrypt(string data, string key)
{
byte[] keyBytes =
Convert.FromBase64String(key); // your key here
AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(keyBytes);
RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
RSAParameters rsaParameters = new RSAParameters();
rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParameters);
byte[] plaintext = Encoding.UTF8.GetBytes(data);
byte[] ciphertext = rsa.Encrypt(plaintext, false);
string cipherresult = Convert.ToBase64String(ciphertext);
//string cipherresult = Encoding.ASCII.GetString(ciphertext);
return cipherresult;
}
}
public class Auth
{
public string Password { get; set; }
public string App_Key { get; set; }
public string UserName { get; set; }
public string Action { get; set; }
}
public class RequestPayloadN
{
public string Data { get; set; }
}
public class AuthResponse
{
public string Status { get; set; }
public List ErrorDetails { get; set; }
public List InfoDtls { get; set; }
public class ErrorDetail
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
public class InfoDtl
{
public string InfCd { get; set; }
public List Desc { get; set; }
}
public class Infodata
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
public data Data { get; set; }
public class data
{
public string ClientId { get; set; }
public string UserName { get; set; }
public string AuthToken { get; set; }
public string Sek { get; set; }
public string TokenExpiry { get; set; }
public static implicit operator data(string v)
{
throw new NotImplementedException();
}
}
}
}
}
For more details, follow below Url:
https://docs.ewaybillgst.gov.in/apidocs/sample-code-in-c-sharp-dot-net.html
How to use gst web api for validate gst in asp.net c#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<title>Asp.Net Repeater Control</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font:13px Verdana;width:310px;">
<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<h3>Asp.Net Repeater Control</h3>
<%--THE REPEATER CONTROL.--%>
<asp:Repeater id="rp" runat="server">
<%--HEADER OF THE REPEATER--%>
<HeaderTemplate>
<table border="0" width="200px">
</HeaderTemplate>
<%--SHOWING ITEMS--%>
<ItemTemplate>
<tr>
<td style="padding:2px;
border:solid 1px #CCC;">
<asp:Label Text='<%# Container.DataItem.ToString() %>'
runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
<%--ALTERNATE TEMPLATE (SHOWING ITEMS IN DIFFERENT COLOR--%>
<AlternatingItemTemplate>
<tr>
<td style="padding:2px;
border:solid 1px #CCC;
background:#EAF7FB;
width:200px;">
<asp:Label Text='<%# Container.DataItem.ToString() %>'
runat="server"></asp:Label>
</td>
</tr>
</AlternatingItemTemplate>
<%--REPEATER FOOTER--%>
<FooterTemplate>
</table>
<div style="padding:20px 0;">
<asp:Label ID="lblFoot" runat="server"></asp:Label>
</div>
</FooterTemplate>
</asp:Repeater>
<%--BUTTON CONTROL--%>
<asp:Button ID="btVF" Text="View Files" AutoPostBack="true"
OnClick="Show"
Font-Names="sanserif"
Font-Italic="true"
Font-Size="155%"
runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="PostBackTrigger" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {}
protected void Show(object sender, EventArgs e)
{
DirectoryInfo Folder = new DirectoryInfo(Server.MapPath("Files\\"));
FileInfo[] fileList = Folder.GetFiles("*.*");
//BIND THE FILE LIST WITH THE REPEATER CONTROL.
rp.DataSource = fileList;
rp.DataBind();
}
}
Adding the <AlternatingTemplate> below the <ItemTemplate> will show files with a different background color for every alternate row.
<AlternatingItemTemplate>
<div style="padding:3px; border:solid 1px #FFF; background:#black">
<asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label>
</div>
</AlternatingItemTemplate>
</AlternatingItemTemplate>
<SeparatorTemplate><hr />
</SeparatorTemplate>
Add the <HeaderTemplate> element just above the <ItemTemplate>
<HeaderTemplate>
<div style="padding-bottom:20px"><strong>Files</strong> (Header)</div>
</HeaderTemplate>
Repeater with <FooterTemplate> element
<FooterTemplate>
<div style="padding-top:20px"><asp:Label ID="lblFooter" runat="server"></asp:Label></div>
</FooterTemplate>
Code Behind In C#
Control ft = rp.Controls[rep.Controls.Count - 1].Controls[0];
Label lbl_Footer = ft.FindControl("lblFooter") as Label;
lbl_Footer.Text = "Showing total <b>" + fileList.Length + "</b> files in the Footer.";