Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET WCF
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 4-Oct-12 23:59pm
Edited 5-Oct-12 0:00am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

OperationContext context = OperationContext.Current;
RemoteEndpointMessageProperty endpoint = (RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
return endpoint.Address;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I am getting client IP address correct...
OperationContext context = OperationContext.Current;
RemoteEndpointMessageProperty endpoint = (RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
return endpoint.Address;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is a situation where I would propose using foreach instead of a traditional for loop.
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:
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.)
  Permalink  
Comments
niraj.kumar108 at 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
Marcus Kramer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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;
  Permalink  
Comments
niraj.kumar108 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 355
1 _Amy 235
2 Peter Leow 185
3 Andreas Gieriet 180
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,462
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100