Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using System.Net;

namespace apiv2.travmechanix.com
{
    public class WCFTransactions
    {
        string strConnection = @"Data Source=********;Initial Catalog=*******;User Id=********;Password=******;";
        DataTable dt = new DataTable();
        bool Flag = false;
        public int Serch(string id, string pass)
        {
                //Convert password in md5 format 
         pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5");
                //Get client IP address
                string host = Dns.GetHostName();
                IPHostEntry ip = Dns.GetHostEntry(host);
                string clientAddress = ip.AddressList[1].ToString();
                //Get All Authorize IP Addresses for this User
                DataTable dtIpAddresses = new DataTable();
         SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select IP from t2security.AuthorisedIP", strConnection);
                objSqlDataAdapter.Fill(dtIpAddresses);
                //Finding IP Address exist or not
                for (int i = 0; i < dtIpAddresses.Rows.Count; i++)
                {
                    if (dtIpAddresses.Rows[i][0].ToString() == clientAddress)
                    {
                        Flag = true;
                        break;
                    }
                }
                //Finding User Address exist or not
                if (Flag == true)
                {
                    SqlDataAdapter dausers = new SqlDataAdapter("select * from t2security.userProfile where vcUserName='" + id + "'and vcUserPassword='" + pass + "' ", strConnection);
                    dausers.Fill(dt);
                    if (dt.Rows.Count > 0)
                        return 1;
                    else
                        return 0;
                }
                else
                    return 0;
       }
   }
}
Posted
Updated 5-Oct-12 0:00am
v2

OperationContext context = OperationContext.Current;
RemoteEndpointMessageProperty endpoint = (RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
return endpoint.Address;
 
Share this answer
 
I am getting client IP address correct...
OperationContext context = OperationContext.Current;
RemoteEndpointMessageProperty endpoint = (RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
return endpoint.Address;
 
Share this answer
 
This is a situation where I would propose using foreach instead of a traditional for loop.
C#
foreach( DataRow row in dtIpAddresses.Rows )
{
   if( row[0] != null && row[0].ToString() == clientAddress )
   {
      Flag = true;
      break;
   }
}


But there is a deeper issue. That is the inefficiency of selecting all the allowed ip addresses when you are simply trying to match 1. A more efficient way would be this:
C#
if (clientAddress != string.Empty)
{
   var dbConn = new SqlConnection(strConnection);
   var cmd = new SqlCommand("SELECT count(*) FROM t2security.AuthorizedIP WHERE IP=@clientAddress", dbConn);
   cmd.Parameters.AddWithValue("@clientAddress", clientAddress);
   var dsResults = new DataSet("Results");
   var daResults = new SqlDataAdapter();
   daResults.SelectCommand = cmd;
   daResults.Fill(dsResults);

   if(dsResults.Rows.Count == 1)
   {
      Flag = ( Convert.ToInt32(dsResults.Rows[0].ToString()) == 1 );
   }

(Note: There may be typos in the code, but it should give you the general idea.)
 
Share this answer
 
Comments
niraj.kumar108 6-Oct-12 7:46am    
Thanks All for reply

but my actual problem is that
in my wcf service i have a class which have this code and with this code my service is well functioning on the local machine.
if we host my service then my service getting A exception like this

FaultException was manhandled by user code
Index was outside the bounds of the array.

Note:- in my local Machine everything is fine work(WCF Service)

Please reply
fjdiewornncalwe 6-Oct-12 12:28pm    
If a piece of software works on your local but then not on the server, the likely culprit is that from the server you can't hit the database properly.
please refere this modified code and check it out. Please read comments also.

and one more thing if it is possible then please debug your service and check where's the actual error is.

C#
pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5");
            //Get client IP address
            string host = Dns.GetHostName();
            IPHostEntry ip = Dns.GetHostEntry(host);
            
            //Here you need to check wheather any IP address is selected or not
            //If your ip object has any count then and then you can directly get it with id.AddressList[1]
            string clientAddress = string.Empty;
            if (ip.AddressList.Count() > 0)
                clientAddress = ip.AddressList[1].ToString();

            //Put check validation for empty string so it will reduce burden when client address is empty
            if (clientAddress != string.Empty)
            {
                //Get All Authorize IP Addresses for this User
                DataTable dtIpAddresses = new DataTable();
                SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select IP from t2security.AuthorisedIP", strConnection);
                objSqlDataAdapter.Fill(dtIpAddresses);

                //Finding IP Address exist or not
                for (int i = 0; i < dtIpAddresses.Rows.Count; i++)
                {
                    // i think here also you need to put validation for column checking
                    //if column value is null then please do not directly cast it to ToString() it will throw error
                    //Instead you can check like  String.IsNullOrEmpty("your string value") please implement that thing here.
                    if (dtIpAddresses.Rows[i][0].ToString() == clientAddress)
                    {
                        Flag = true;
                        break;
                    }
                }
            }
            //Finding User Address exist or not

            //Or if your Flag is type of bool or boolean then you need not to check like this
            //if(Flag == true) you can directly check like this if(Flag)
            if (Flag)
            {
                SqlDataAdapter dausers = new SqlDataAdapter("select * from t2security.userProfile where vcUserName='" + id + "'and vcUserPassword='" + pass + "' ", strConnection);
                dausers.Fill(dt);
                if (dt.Rows.Count > 0)
                    return 1;
                else
                    return 0;
            }
            else
                return 0;
 
Share this answer
 
Comments
niraj.kumar108 9-Oct-12 1:11am    
hey guys Finally i have find solution this is the code which
on server not work
clientAddress = ip.AddressList[1].ToString();
i am replace this code with
OperationContext context = OperationContext.Current;

RemoteEndpointMessageProperty endpoint = (RemoteEndpointMessageProperty)context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];

clientAddress = endpoint.Address;
Thanks
Tejas Vaishnav 9-Oct-12 9:09am    
that's sounds grt you have solved your problem.

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