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 else15 {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>
时间: 2024-10-19 08:25:42