Thursday 9 February 2012

IP Address in ASP.NET and host name

ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
;;
if (!string.IsNullOrEmpty(ip))
{
   string[] ipRange = ip
.Split(',');
   int le = ipRange.Leng
th - 1;
   string trueIP = ipRan
ge[le];
}
else
{
   ip=Request.ServerVariables("REMOTE_A
DDR");
}

///---------------------------------------------------------------
Find IP Address in ASP.NET Behind Proxy

If you want to find the IP address of visitors to your aspx page or application or wanna retrieve IP for other uses than u need to write this code

Using this code we can find IP address of visitor even if visitor is behind any proxy

public string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}

To find IP address of a machine behind LAN you can use this code
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses
(strHostName).GetValue(1).ToString();
//---------------------------------------------------------------------------
Method that gets IP address [ASP.NET, C#]

using System;
using System.Web;

namespace WebApplication4
{
    public class Global : HttpApplication
    {
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // Get request.
        HttpRequest request = base.Request;

        // Get UserHostAddress property.
        string address = request.UserHostAddress;

        // Write to response.
        base.Response.Write(address);

        // Done.
        base.CompleteRequest();
    }
    }
}

Result of the application

127.0.0.1

/////-----------------------------------------------------------------------------

Example 2-2: DNS LookupService

using System.Web.Services;
using System.Net;
[WebService(Namespace="http://www.bostontechnical.com/webservices/",
    Description="<b>A web service which performs Domain Name Lookups.</b>")]
public class DNSLookupService : System.Web.Services.WebService
{
         [WebMethod]
         public string getIPforHostname(string strHostname)
         {
                 IPHostEntry hostInfo = Dns.GetHostByName(strHostname);
                 return hostInfo.AddressList[0].ToString();
         }
}
//--------------------------------


Getting the User Name :

using three ways we can get the User Name using C#

1) System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;

string strName = p.Identity.Name;

[ OR ]

2) string strName = HttpContext.Current.User.Identity.Name.ToString();

[ OR ]

3) string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name

string strName = Request.ServerVariables[5]; //Finding with index

In Above 3 Cases returnin string contains DomainName\WinNTLoggedUserName

(for Ex: Microsoft\Bill.Gates. Here Microsoft is domain Bill.Gates is Logger User Name )

Using string operations seperate the DomainName and UserName.

//-----------------------------------------------------

1
string username = User.Identity.Name.ToString();
Be sure to add the Security reference:

1
using System.Web.Security;
Thats it!

 

No comments:

Post a Comment