Pages

Monday 5 December 2011

how to get client ip in asp.net(c#.Net)

Introduction:

In this article I will explain how to find system or client machine IP address from which user visited the website in asp.net behind proxy in c#.

Description:
  
Now I will explain how to get system or client IP address of machine from which the user visited the website in asp.net
in c#. To implement this one write the code as shown below
 
protected void Page_Load(object sender, EventArgs e)
{
string IPAdd = string.Empty;
IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAdd))
IPAdd = Request.ServerVariables["REMOTE_ADDR"];
lblIP.Text = IPAdd;
}
//or
protected void Page_Load(object sender, EventArgs e)
{
IPAddress myIP = IPAddress.Parse(Request.UserHostName);

IPHostEntry GetIPHost = Dns.GetHostEntry(Request.UserHostName);

Response.Write(GetIPHost.HostName);
}

If you observe above code when users behind any proxies or routers the REMOTE_ADDR/Request.UserHostName returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR/Request.UserHostName, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address.

Connect the remote desktop using c#.net


Introduction

Remote Desktop Services is one of Microsoft Windows components to access a remote computer through the network. Only the user interface of the application is presented at the client. Any input is redirected over to the remote computer over the network.
At work, we use Remote Desktop a great deal. It allows us to login to a remote server to perform health checks, deploy applications, troubleshoot problems, etc. We also use remote desktop often when we do WFH (work from home).
Why do you want to write a .NET application to do this when you have the MS Terminal Services client available from OS? Well, consider if you want to work on 3 different application servers at the same time and want to toggle between these 3 servers quite often. With the MSTSC, we will be running 3 different clients for the 3 servers and it is difficult to manage the working environment. In .NET, you can develop an application with tab control to load remote desktop sessions in different tabs in one window.

Background

We will be using AxMSTSCLib an ActiveX component in our program to connect to the remote computer. It’s not that hard to build a remote desktop application in .NET. Microsoft has a “Microsoft RDP client control” ActiveX control that we will be using in our application.

This is How We Do It

We will start by creating a Windows application in the Visual Studio IDE.
Add a reference to “Microsoft Terminal Services Control Type Library” from the COM tab. This will add MSTSCLib.dll to the project.

 
To add MSTSC to the toolbox, right click the toolbox and select “Choose Items…”. Now add “Microsoft Terminal Services control from the COM tab.

Drag the newly added control from toolbox to the form. Add 3 textbox and 2 button controls to the form:


Connect Button - Click Event

Here is how we write the Connect button click event. Now assign the properties (Server, UserName) of RDP control with the textbox values. Here’s how easy it is to login to remote machine. However there is one catch, there is no direct method in RDP control through which you can pass the username and password to login to the remote desktop. Due to security reasons, you have to implement an interface (IMsTscNonScriptable) to cast it separately.
 private void button1_Click(object sender, EventArgs e){
try
{
rdp.Server = txtServer.Text;
rdp.UserName = txtUserName.Text;

System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply = ping.Send(rdp.Server);
if (pingReply.Status == IPStatus.Success)
{
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text;
rdp.Connect();
}
}

catch (Exception Ex)
{
MessageBox.Show("Error Connecting RDC", "Error connecting to remote desktop " + txtServer.Text + " Error: " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Disconnect Button – Click Event

To disconnect from the remote desktop session, we just need to call the Disconnect() method.
Before disconnecting, we want to ensure that the connection is still available. We don't want to disconnect if it is already disconnected (very clever, huh).
 private void button2_Click(object sender, EventArgs e){
try
{
// Check if connected before disconnecting
if (rdp.Connected.ToString() == "1")
rdp.Disconnect();
}
catch (Exception Ex)
{
MessageBox.Show("Error Disconnecting", "Error disconnecting from remote desktop " + txtServer.Text + " Error: " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

 

please put the comments..............

Calculate age depending on dateofbirth using c#.net

Here I will explain Calculating dateofbirth in c#.net

//Date of Birth

DateTime dateOfBirth = new DateTime(1992, 06, 10);
//Current date
DateTime currentDate = DateTime.Now;
int ageInYears = 0;
int ageInMonths = 0;
int ageInDays = 0;
//Calculate days
ageInDays = currentDate.Day - dateOfBirth.Day;
//Calculate Months
ageInMonths = currentDate.Month - dateOfBirth.Month;
//Calculate Years
ageInYears = currentDate.Year - dateOfBirth.Year;
//leaf years
if (ageInDays < 0)
{
ageInDays +=
DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
ageInMonths = ageInMonths--;
if (ageInMonths < 0)
{
ageInMonths += 12;
ageInYears--;
}
}

if (ageInMonths < 0)
{
ageInMonths += 12;
ageInYears--;

Console.WriteLine("Age in Years :{0}, Months : {1}, Days : {2}", ageInYears, ageInMonths, ageInDays);