• WCF 身份验证 通过检查客户端IP


    WCF 身份验证

    功能描述:

    服务运行的时候,通过配置文件获取所有可访问SOA端的服务IP。每次客户调用服务时获取IP对比判定通过。

    以下是获取客户端IP的代码:

     /*************************************************************************************
     * 代码:吴蒋
     * 时间:2012.02.07
     * 说明:安全类
     * 其他:
     * 修改人:
     * 修改时间:
     * 修改说明:
     ************************************************************************************/
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    
    namespace Tools
    {
        public class Safe
        {
            public static Safe Instance()
            {
                return new Safe();
            }
    
            public string ClientIp()
            {             
                OperationContext context = OperationContext.Current;
                MessageProperties properties = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                return endpoint.Address;
            }
    
            public string ClientPort()
            { 
                OperationContext context = OperationContext.Current;
                MessageProperties properties = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                return endpoint.Port.ToString();
            }
    
            public string ClientIpAndPort()
            {
                OperationContext context = OperationContext.Current;
                MessageProperties properties = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                return endpoint.Address + ";" + endpoint.Port.ToString();
            }
        }
    }
    

    XML 存放可访问IP

    复制代码
    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <configuration>
    3 <ip>192.168.0.71</ip>
    4 <ip>192.168.0.6</ip>
    5 <ip>127.0.0.1</ip>
    6 <ip>192.168.0.72</ip>
    7 <ip>192.168.0.136</ip>
    8 <ip>192.168.0.3</ip>
    9 </configuration>
    复制代码
    复制代码
    #region 特殊函数
    /// <summary>
    /// 匹配允许访问IP
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <param name="node">节点名称</param>
    /// <returns>转换为DataTable</returns>
    public DataTable ReadRunIP(string path, string node)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    DataTable dt = new DataTable();
    dt.Columns.Add("ip", typeof(string));
    XmlNodeList xnlist = doc.SelectNodes(node);
    if (xnlist.Count > 0)
    {
    for (int i = 0; i < xnlist.Count; i++)
    {
    DataRow dr = dt.NewRow();
    dr["ip"] = xnlist[i].InnerText;
    dt.Rows.Add(dr);
    }
    }
    return dt;
    }
    #endregion
    复制代码


    页面加载时获取所有可访问IP

    复制代码
    1 public static DataTable dtRunIp;
    2 public static string MapPath = ConfigurationManager.ConnectionStrings["configPath"].ConnectionString;
    3
    4 protected void Application_Start(object sender, EventArgs e)
    5 {
    6 dtRunIp = XMLHelper.XmlHelper.Instance().ReadRunIP(MapPath + "/Config/RunConfig.config", "//configuration/ip");
    7 }
    复制代码


     


    判断IP许可


     

    在服务中的应用:

    复制代码
     1 [ServiceContract]
    2 public class SOAControl
    3 {
    4 string msgr = "无访问权限、服务器积极拒绝";
    5 //获取xml文档
    6 [OperationContract]
    7 public string GetXML(ref string msg)
    8 {
    9
    10 if (Certificate.IsCanRead())
    11 {
    12 return XmlHelper.Instance().XmlDocumentToString(Global.MapPath + "/Control/Control.config".ToString());
    13 }
    14 else
    15 {
    16 msg = msgr;
    17 return null;
    18 }
    19 }
    复制代码
    复制代码
     1 public static bool IsCanRead()
    2 {
    3 string clientIp = Tools.Safe.Instance().ClientIp();
    4 bool r = false;
    5 if (Global.dtRunIp.Rows.Count > 0)
    6 {
    7 for (int i = 0; i < Global.dtRunIp.Rows.Count; i++)
    8 {
    9 if (clientIp == Global.dtRunIp.Rows[i]["ip"].ToString())
    10 {
    11 r = true;
    12 }
    13 }
    14 }
    15 return r;
    16
    17 }
    复制代码


    WCF的配置文件设置

    复制代码
    <?xml version="1.0"?>
    <configuration>
    <system.serviceModel>
    <bindings>
    <wsHttpBinding>
    <binding name="NoneSecurity"
    maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
    <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
    <security mode="None"/>
    </binding>
    </wsHttpBinding>
    </bindings>
    <behaviors>
    <serviceBehaviors>
    <behavior name="Control.Service.SOAControlBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <services>
    <service behaviorConfiguration="Control.Service.SOAControlBehavior" name="Control.Service.SOAControl">
    <endpoint address="" binding="wsHttpBinding" contract="Control.Service.SOAControl" bindingConfiguration="NoneSecurity">
    <identity>
    <dns value="localhost"/>
    </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
    </services>
    </system.serviceModel>
    </configuration>
    复制代码
  • 相关阅读:
    十分简单的宏病毒
    爬虫入门六 总结 资料 与Scrapy实例-bibibili番剧信息
    爬虫入门五 gooseeker
    爬虫入门四 re
    爬虫入门三 scrapy
    爬虫入门二 beautifulsoup
    爬虫入门一 基础知识 以及request
    腾讯游戏策划实习面经
    网络安全实验合集
    HTML5总结
  • 原文地址:https://www.cnblogs.com/zxktxj/p/4330775.html
Copyright © 2020-2023  润新知