Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1 string userIP = Request.UserHostAddress;
2 string serverIP = Request.ServerVariables["LOCAL_ADDR"];
3
4 userIP = userIP.Substring(0, userIP.LastIndexOf("."));
5 serverIP = serverIP.Substring(1, serverIP.LastIndexOf("."));


C#
// only redirect to WinLogin.aspx if user is internal & IE
 if (userIP.Equals(serverIP) && Request.Browser.IsBrowser("ie"))
  {
     Server.Transfer("SecondLogin.aspx"");
  }
else
  {
     Server.Transfer("ThirdLogin.aspx"");
  }
Posted
Updated 2-Dec-14 22:04pm
v2

What values coming inside userIP and serverIP.

Put a check if length is greater then 0 then only execute the substring statement.
 
Share this answer
 
userIP.Substring(0, userIP.LastIndexOf("."));
LastIndexOf returns -1 if text is not found.

In your case, a '.' is not being found in userIP and -1 is being returned.
As a result, Substring fails becaause Substring(0, -1) makes no sense.

Put some validation conditions to ensure there is a valid IP value before doing a sub string.
 
Share this answer
 
v3
Comments
Member 11270220 3-Dec-14 3:59am    
so what I do instead of that?
Abhinav S 3-Dec-14 4:00am    
Try
int p = userIP.LastIndexOf(".");
if (p >0)
{
userIP = userIP.Substring(0, p);
}
Member 11270220 3-Dec-14 4:03am    
now it shows error on that line
serverIP = serverIP.Substring(1, serverIP.LastIndexOf("."));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900