索引超出Wcf Service中数组的范围.没有这个复杂的代码,我的服务就可以正常工作 [英] Index was outside the bounds of the array the in Wcf Service. without this complex code my service is work fine

查看:60
本文介绍了索引超出Wcf Service中数组的范围.没有这个复杂的代码,我的服务就可以正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
       }
   }
}

推荐答案

请参考此修改后的代码并将其检出.请同时阅读评论.

还有另一件事,如果可能的话,请调试您的服务并检查实际错误在哪里.

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;


这是一种情况我建议使用foreach代替传统的for循环.
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;
   }
}



但是,还有一个更深层次的问题.这就是在您仅尝试匹配1时选择所有允许的ip地址的效率低下.一种更有效的方法是:



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.)


我正在正确获取客户端IP地址...
OperationContext上下文= OperationContext.Current;
RemoteEndpointMessageProperty端点=(RemoteEndpointMessageProperty)
context.IncomingMessageProperties [RemoteEndpointMessageProperty.Name];
返回endpoint.Address;
I am getting client IP address correct...
OperationContext context = OperationContext.Current;
RemoteEndpointMessageProperty endpoint = (RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
return endpoint.Address;


这篇关于索引超出Wcf Service中数组的范围.没有这个复杂的代码,我的服务就可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆