Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, November 3, 2015

Conver JSON to XML

Following is the method to convert json to xml:

string ConvertToJson(string json)
{
    XmlDocument rootxml = new XmlDocument();
    XmlDocument doc;
    doc = JsonConvert.DeserializeXmlNode(json,"root");
    return doc.OuterXml;

}

For this we need to use newtonsoft.json dll to our project.
This dll can be installed by package manager. Perform following steps to download this package:

1. Go to Tools > Library Package Manager > Manage NuGet Packages for Solution...

2. Search for newtonsoft
3. Install Json.net

Saturday, August 22, 2015

HTTP Error 500.19 - Internal Server Error IIS

This error is due to application pool of website. You might map wrong application pool to website. Please follow the below steps to check.
  1. Open inetmgr.exe(IIS).
  2. Click advance settings of website by clicking advance settings link in right side panel.
  3. Check application pol of website.
  4. Now click application pool.
  5. Double click website's application pool(or right click and properties of app pool).
  6. Application pool should be same as website name.

The configuration section 'system.web.extensions' cannot be read because it is missing a section declaration

This error is due to version of application pool. You need to update version of app pool from v2 to v4. Follow the below steps:

  1. Open IIS.
  2. Right click website and click settings to check the application pool name of website. OR
  3. Click website, click advance settings in right side panel.
  4. Now click application pool.
  5. Double click website's application pool(or right click and properties of app pool).
  6. Change version in dropdown from v2 to v4.

Friday, August 21, 2015

IIS - Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list - ASP.net

This error is due to asp.net installation on IIS. To resolve this error you need to install asp.net on IIS. For this you need to execute aspnet_regiis.exe -i command. First location this exe. It should be on following location:
On Windows 32 bit:
C:\Microsoft.NET\Framework\v4.0.21006\aspnet_regiis.exe -i
On Windows 64 bit:
C:\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i


Monday, June 29, 2015

Take SSH from asp.net

Take SSH from asp.net
We can download .rdp file to connect to some server. Following code will create an rdp file and download that on client’s machine. Opening this file, user will be able to rdp specified server.

For this we need to have putty file on client machine. Here we will put putty.exe file in some specified download folder and create a putty.bat file to execute a command to run putty. We will edit configuration in this putty.bat file and mention the server ip and port(22) number to take SSH:

ASPX Code:
Note: script part will download the putty.exe file. Here we will download 2 files in single click.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SSH.aspx.cs" Inherits="_SSH" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function btnclick() {
            setTimeout(function () {
                document.getElementById('btnpty').click();
            }, 500);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr style="background-color: #FF9900; color: #FFF">
                    <td colspan="2">RDP
                    </td>
                </tr>
                <tr style="background-color: #ECECEC; color: #000">
                    <td>Description
                    </td>
                    <td>Link
                    </td>
                </tr>
                <tr>
                    <td>SSH:
                    </td>
                    <td>
                        <asp:LinkButton runat="server" OnClientClick="javascript:return btnclick()" OnClick="LinkButton1_Click">127.0.0.3</asp:LinkButton>
                        <a href="download/putty.exe" id="btnpty">a </a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C# Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _SSH : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void DownLoadFile(string filePath)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + file.Name));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~/Download/putty.bat");
        UpdateBatFile("192.168.0.2", filePath);
        DownLoadFile(filePath);
    }
    private void UpdateBatFile(string IPAddress, string filePath)
    {
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string[] fileToUpdate = File.ReadAllLines(filePath);
        foreach (string line in fileToUpdate)
        {
            if (line.Contains("putty.exe"))
            {
                temp = temp.Insert(0, "putty.exe -ssh " + IPAddress + " 22");
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }

        File.WriteAllText(filePath, newFile.ToString());
    }
}

RDP/RDC from asp.net


We can download .rdp file to connect to some server. Following code will create an rdp file and download that on client’s machine. Opening this file, user will be able to rdp specified server.

Foe this we need to create an rdp file in some download folder. Create a download folder and place a dummy download rdp file:


ASPX Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr style="background-color: #FF9900; color: #FFF">
                    <td colspan="2">RDP
                    </td>
                </tr>
                <tr style="background-color: #ECECEC; color: #000">
                    <td>Description
                    </td>
                    <td>Link
                    </td>
                </tr>
                <tr>
                    <td>RDP1:
                    </td>
                    <td>
                        <asp:LinkButton runat="server" OnClick="lnkDownload_Click">127.0.0.1</asp:LinkButton>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C# Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        string fileName = "Default.rdp";
        string filePath = Server.MapPath("~/Download/" + fileName);
        LinkButton linkButton = (LinkButton)sender;
        UpdateRDPFile(linkButton.Text, filePath);
        DownLoadFile(filePath);
    }

    private void UpdateRDPFile(string IPAddress, string filePath)
    {
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string[] fileToUpdate = File.ReadAllLines(filePath);
        foreach (string line in fileToUpdate)
        {
            if (line.Contains("full address:s:"))
            {
                temp = temp.Insert(0, "full address:s:" + IPAddress);
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }

        File.WriteAllText(filePath, newFile.ToString());
    }

    private void DownLoadFile(string filePath)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + file.Name));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
    }

}