Posts

Showing posts from April, 2010

How to get client "IP Address" using Asp.net /C#

I have found that in various forum newbie are very frequently asked this type of question how to get client IP or get mac address or country. So that I would like to share this for that user. Though its very simple but helpful.

Tip:1
To get the client IP you can use the following function
    string sClientIp=Request.UserHostAddress();
or
    string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

Tip:2:To avoid Proxy IP:

To get the IP address of the machine and not the proxy use the following code
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; This will returns the client ip in string format.

Code:
C#
    string ip;

    ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
    if(ip==string.Empty)
    {
      ip=Request.ServerVariables("REMOTE_ADDR");
    }
REMOTE_ADDR does not always provide the users IP but rather the ISPs' IP address so fi…

Validation control not work in FireFox or other browser

When I use validation control on asp.net it works for IE fine but failed to work in Firefox. After goggling I will find the solution. I share this code here, hope it may help others.

The ASP.Net validations controls only work client side in IE.
You can make them work in other browsers, coding as below:

C#

Page.Validate();

if ( !Page.IsValid )
{
return;
}
//Continue Your code
VB.NET

Page.Validate()

If Not Page.IsValid Then
Return
End If
'Continue Your code

Have a nice day.

how to get MAC address using C#

How to get the MAC address of  system using Asp.net/C#

Now I want to share with you some simple but important for our rapid development.
Here I want to discuss how to get system MAC address using c# ,

First way:
You need to import the System.Net namespace for this to work.
This will support IPv4 and IPv6.

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties properties = adapter.GetIPProperties();
        adapter.GetPhysicalAddress().ToString();
    }

Second way: public string GetMACAddress()
{
  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection moc = mc.GetInstances();
  string MACAddress = String.Empty;
 foreach (ManagementObject mo in moc)
 {
     if (MACAddress == String.Empty) // only return MAC Address from first card   
           {
              if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAdd…

How to detect browser Information in Asp.net

Sometimes some fresher asked me how they get browser information when using asp.net. In online you can find lots of resources about. I have placed this code snippet form msdn which may help newbie.

Place this code under any button event or Page_Load event:
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = "…

How to bind Data in Customized Gridview

In asp.net gridview control is widely used control. It has lots of properties and attributes which help the developers for rapid development. Generally we can use Gridvew to show data in tabular format.
But It has a lots of events and features which is make this control a most powerful control for asp.net.
There are various types of field in Gridview such as:
1.BoundField
2.ImageField
3.HyperLinkField
4.ButtonField
5.TemplateField
6.CommandField
7.CheckBoxField
These fields gives developer more flexibility to customize the gridview. Paging is efficiently handling by Gridview it is one of cool feature of it. There are various way to bind data into gridview.

Today I want to focus how to bind data to a customized gridview. I have used this following way to bind customized gridview very frequently. Obviously,there may be more easier way to bind in customize gridview.
Here I have used BoundField,Templatefield to customize the grid.


Here is the aspx code for Gridview:

<asp:GridView ID=&q…

Unable to start debugging on the web server. Could not start ASP.NET or ATL Server debugging.....

Image
Yesterday I had started my work at new work station. Very recently I needed to test a web application on localhost. I found that IIS is not installed so that I installed it myself. Then I hosted it in localhost and try run but showed me an error. Today I want to share with you how to solve this problem.

Problem:
After developing an ASP.NET Web application by using localhost as the Web server, and config the Web site settings in Microsoft Internet Information Services (IIS) to use the localhost or specific IP address for Web Site Identification. Then, when I try to run the application, then receive the following error message:

Error while trying to run project: Unable to start debugging on the web server. Could not start ASP.NET or ATL Server debugging.
Verify that ASP.NET or ATL Server is correctly installed on the server.


Solution:
After search in google I have found lots of solution. But suddenly remembered that I have insatalled IIS after .net framework installed. I hope that this …

Single Sign on (SSO) Using Cookie in asp.net

There are various ways to use Single Sign on(SSO) in asp.net web application. We can use cookies, session (state server), SAML and web services etc. Now we would like to give a brief overview of how to use cookie to implement Single Sign on(SSO) in asp.net web application.

Assume that we have two web application hosted on different virtual directory but under same domain. As for example, our root domain is: http://www.cookietest.com and
Other two virtual directory hosted under this domain are
http://www.cookietest.com/cookiesite1/Login.aspx
http://www.cookietest.com/cookiesite2/Default.aspx

If we login successfully in cookiesite1 then it writes the login information in cookie and now opens another tab or a new window in same browser (IE, FF whatever you like). Place this address http://www.cookietest.com/cookiesite2/Default.aspx in address bar logged in automatically in cookiesite2. When we try to access in cookiesite2 –> Default.aspx it checks the login information from cookie. If d…