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>

WCF 身份验证 通过检查客户端IP的更多相关文章

  1. WCF身份验证三:自定义身份验证之<MessageHeader>

    关于使用SoadHeader验证Robin已经有一篇十分精彩的文章: WCF进阶:为每个操作附加身份信息, 不过我的思维方式总是跟别人有点不太一样, 还是把类似的内容用我的方式重新组织一下. 使用He ...

  2. WCF身份验证二:基于消息安全模式的自定义身份验证

    使用X509证书进行身份验证应该说是WCF安全模型中最”正常”的做法, 因为WCF强制要求使用证书加密身份数据, 离开了证书, 所有的身份验证机制拒绝工作, WCF支持的身份验证机制也相当复杂, 这里 ...

  3. WCF身份验证一:消息安全模式之<Certificate>身份验证

    消息安全模式的证书身份验证方式,基于WSHttpBinding绑定协议的实现过程.主要内容:基本概念,然后是制作证书.服务端配置.客户端配置.总结.这里应该和Transport传输安全模式之证书身份验 ...

  4. WCF身份验证之用户名密码认证

    WCF支持多种认证技术,例如Windowns认证.X509证书.Issued Tokens.用户名密码认证等,在跨Windows域分布的系统中,用户名密码认证是比较常用的,要实现用户名密码认证,就必须 ...

  5. 关于WEB Service&WCF&WebApi实现身份验证之WCF篇(2)

    因前段时间工作变动(换了新工作)及工作较忙暂时中断了该系列文章,今天难得有点空闲时间,就继续总结WCF身份验证的其它方法.前面总结了三种方法(详见:关于WEB Service&WCF& ...

  6. 关于WEB Service&WCF&WebApi实现身份验证之WCF篇(1)

    WCF身份验证一般常见的方式有:自定义用户名及密码验证.X509证书验证.ASP.NET成员资格(membership)验证.SOAP Header验证.Windows集成验证.WCF身份验证服务(A ...

  7. C# 实现身份验证之WCF篇(1)

    WCF身份验证一般常见的方式有:自定义用户名及密码验证.X509证书验证.ASP.NET成员资格(membership)验证.SOAP Header验证.Windows集成验证.WCF身份验证服务(A ...

  8. 关于WEB Service&WCF&WebApi实现身份验证之WebApi篇

    之前先后总结并发表了关于WEB Service.WCF身份验证相关文章,如下: 关于WEB Service&WCF&WebApi实现身份验证之WEB Service篇. 关于WEB S ...

  9. 部署开启了Kerberos身份验证的大数据平台集群外客户端

    转载请注明出处 :http://www.cnblogs.com/xiaodf/ 本文档主要用于说明,如何在集群外节点上,部署大数据平台的客户端,此大数据平台已经开启了Kerberos身份验证.通过客户 ...

随机推荐

  1. JNI_Z_06_方法的操作(没有String类型的参数)_父类的同名方法

    1.关键在于: 使用的 method id 是 子类的 还是 父类的,而 父类methodID的获取 必须使用 父类的class对象. 2.VC6(CPP)的DLL代码: #include<st ...

  2. thrift安装及常见问题

    一.安装thrift (macOS / Linux) 1. 下载thrift0.10.0源码 https://github.com/apache/thrift/releases/tag/0.10.0 ...

  3. combo

    什么是combo 上一节中我们有一行代码: <script src="http://g.tbcdn.cn/kissy/k/1.4.8/??seed-min.js,import-styl ...

  4. netty同时支持socket和http

    项目需要使用netty做中转服务器,同时支持两种不同协议的客户端,经过几天查询资料终于找到合适的方案了,同时感谢Netty权威指南及论坛问答,开始贴代码 客户端1==>socket public ...

  5. Ajax基础(四)--dom元素简单操作

    1 //js对dom元素的操作 //添加dom元素 var param = document.createElement("p"); var node = document.cre ...

  6. Codeforces Round #451 (Div. 2)

    水题场.... 结果因为D题看错题意,B题手贱写残了...现场只出了A,C,E A:水题.. #include<bits/stdc++.h> #define fi first #defin ...

  7. Flask安装配置

    倒腾了一下午了,还是不太顺利,顺便记录一下. 硬件环境:win8.1 64位 + python2.7.9 32位 安装easy_install 需要先下载ez_setup.py(需要复制该链接中的内容 ...

  8. 【spark】文件读写和JSON数据解析

    1.读文件 通过 sc.textFile(“file://") 方法来读取文件到rdd中. val lines = sc.textFile("file://")//文件地 ...

  9. 慕课网:4-2—— 使用DB facade实现CURD (09:11)

    public function test1() { //新增数据: /* $bool=DB::insert('insert into student(name,age) VALUES (?,?)', ...

  10. Nodejs 实用工具集笔记

    前言 工具列表 supervisor 安装 使用 node-inspector 安装 使用 SuperAgent 安装 使用 教程 cheerio 安装 使用 教程 总结 前言 学了Nodejs一天了 ...