Thursday, August 23, 2018

How to get IP Address of Visitors Machine in ASP.Net

How to get IP Address of Visitors Machine in ASP.Net


First the IP Address is determined for the Client machine’s which are behind Routers or Proxy Servers and hence the HTTP_X_FORWARDED_FOR server variable is checked.


NoteWhen the Client machine is behind a Proxy Server its IP Address of the Proxy Server is appended to the Client machine’s IP Address. If there are multiple Proxy Servers then the IP Addresses of all the Proxy Servers are appended to the client machine IP Address.
 
If the IP Address is not found in the HTTP_X_FORWARDED_FOR server variable, it means that it is not using any Proxy Server and hence the IP Address is now checked in the REMOTE_ADDR server variable.
NoteWhile running this application on your machine locally i.e. in Visual Studio, the IP Address will show as 127.0.0.1 or ::1. This happens because in such case the Client and Server both are the same machine. Thus no need to worry when you deploy it on a Server, you will see the results.
 

string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
    ipaddress = Request.ServerVariables["REMOTE_ADDR"];


if (ipaddress == "xxx.xxx.x.xxx") // Enter your IP
   {
      lblMsg.Text = "You are not authorized to login into the system.";
   }

Tuesday, June 19, 2018

Message send to Web Application to Desktop Application using SignalR

Sample Application

Let us build a sample chat application that broadcasts the messages to all connected clients or to particular groups. In this example consider the clients to be a Windows application and a web form client.

Creating an Asp.Net SignalR Hub

Open Visual Studio and create an empty Asp.net MVC application named MyChatApplicationServer. Now open the NuGet packages, search and include Microsoft ASP.NET SignalR and Microsoft.Owin.Cors.
Now right click on the project, add the SignalR Hub class and name it as MyChatHub.  Add the following code to the class.



The HUB class also provides the properties Clients, Groups, Context and events like OnConnected, etc.
Finally you should also add Owin startup class to map the available hubs as shown below.

Pushing Data to a Web Form Client

Now create a web form client project named WebClient and add a simple HTML file ChatWebClient.html with the following code. Add the references to the required script files.

There are two ways of implementing the client function one with proxy and other without. The above example is the one without a proxy.
In the connection start you may also mention which transport SignalR should use.

Pushing Data to a Windows Application Client

Let us add a windows app and subscribe to the SignalR host. Create a new windows application named WinFormClient and from the nugget packages install Microsoft ASP.NET SignalR .NET client package. In the Program.cs add the following code.

Once done go ahead and run both the web and Windows client. Enter the messages and look at how the message is getting pushed across both the clients though they are of a different nature. Fig 2.0 shows a sample screenshot.