学会WCF之试错法——安全配置报错分析

服务端配置

<system.serviceModel>
     <bindings>
      <wsHttpBinding>
        <binding name ="WsHttpBinding_IService" maxReceivedMessageSize="370000" receiveTimeout="00:10:01" maxBufferPoolSize="100">
          <readerQuotas maxStringContentLength="240000"/>
          <security mode="Transport">
            <transport clientCredentialType="Windows"></transport>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WCF_Find_Error_Lib.Service" behaviorConfiguration="beh">
        <endpoint address="" 
                  binding="wsHttpBinding"
                  contract="WCF_Find_Error_Lib.IService" 
                  bindingConfiguration="WsHttpBinding_IService">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/S" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="beh">
          <serviceThrottling maxConcurrentCalls="1"/>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" httpGetUrl="http://localhost/S"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

服务契约

[ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        string GetString(string value);

        [OperationContract]
        void Upload(Request request);
    }

    [MessageContract]
    public class Request
    {
        [MessageHeader(MustUnderstand = true)]
        public string FileName { get; set; }

        [MessageBodyMember(Order = 1)]
        public Stream Content {get;set;}
}

服务

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class Service : IService
    {
        public string GetData(int value)
        {
            //Thread.Sleep(120000);
            return string.Format("You entered: {0}", value);
        }

        public string GetString(string value)
        {
            //Thread.Sleep(120000);
            Thread.Sleep(5000);
            return string.Format("You entered: {0}", value);
        }

        public void Upload(Request request)
        {
            try
            {
                StreamReader sr = new StreamReader(request.Content, Encoding.GetEncoding("GB2312"));
                StreamWriter sw = new StreamWriter("E:\\" + request.FileName + ".txt", false, Encoding.GetEncoding("GB2312"));
                while (!sr.EndOfStream)
                {
                    sw.WriteLine(sr.ReadLine());
                    Thread.Sleep(5000);
                }
                sr.Close();
                sw.Close();
            }
            catch (Exception ex)
            { }
            
        }
  }

服务寄宿

  class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ServiceHost host = new ServiceHost(typeof(Service));
                host.Open();
                Console.WriteLine("服务状态:"+host.State.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            

            Console.WriteLine("服务启动");
            Console.WriteLine("按任意键停止服务");
            Console.ReadLine();
        }
  }

客户端配置

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name ="WsHttpBinding_IService" maxReceivedMessageSize="370000" receiveTimeout="00:10:01" maxBufferPoolSize="100">
          <readerQuotas maxStringContentLength="240000"/>
          <security mode="Transport">
            <transport clientCredentialType="Windows">
            </transport>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/S" binding="wsHttpBinding"
          bindingConfiguration="WsHttpBinding_IService" contract="IService"
          name="WHttpBinding_IService" />
    </client>
  </system.serviceModel>

客户端代理

  public class ServiceProxy
    {
        public string GetData(int value)
        {
            string ret = null;
            ServiceClient client = null;
            try
            {
                client = new ServiceClient();
                ret = client.GetData(value);
                client.Close();
            }
            catch
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
            return ret;
        }

        public string GetString(string value)
        {
            string ret = null;
            ServiceClient client = null;
            try
            {
                client = new ServiceClient();
                ret = client.GetString(value);
                client.Close();
            }
            catch
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
            return ret;
        }
        public void Upload(Request request)
        {
            ServiceClient client = null;
            try
            {
                client = new ServiceClient();
                client.Upload(request);
                client.Close();
            }
            catch
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
        }

    }


    [ServiceContractAttribute(ConfigurationName = "IService")]
    public interface IService
    {

        [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/GetData", ReplyAction = "http://tempuri.org/IService/GetDataResponse")]
        string GetData(int value);

        [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/GetString", ReplyAction = "http://tempuri.org/IService/GetStringResponse")]
        string GetString(string value);

        [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/Upload", ReplyAction = "http://tempuri.org/IService/UploadResponse")]
        void Upload(Request request);
    }
    [MessageContract]
    public class Request
    {
        [MessageHeader(MustUnderstand = true)]
        public string FileName { get; set; }

        [MessageBodyMember(Order = 1)]
        public Stream Content { get; set; }
    }
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class ServiceClient : System.ServiceModel.ClientBase<IService>, IService
    {

        public ServiceClient()
        {
        }

        public string GetData(int value)
        {
            return base.Channel.GetData(value);
        }

        public string GetString(string value)
        {
            return base.Channel.GetString(value);
        }

        public void Upload(Request request) 
        {
            base.Channel.Upload(request);
        }
  }

1 Transport安全模式(本机调试)

客户端调用

using (ServiceClient client = new ServiceClient())
{
    StreamReader sr = new StreamReader("D:\\CSBMTEMP.txt", Encoding.Default);
    string str = sr.ReadToEnd();
    sr.Close();
    var s = client.GetString(str);
}

服务端和客户端配置如上。wsHttpBinding的Message安全模式,客户端凭据默认为Windows

运行客户端,抛出异常:

抛出异常的原因是,Transport模式支持HTTPS,TCP,ICP,MSMQ,而这里终结点地址是http://localhost/S,没有使用HTTPS协议。将终结点地址改为https://localhost/S即可。

服务端和客户端配置的安全模式不一样时

服务端安全模式修改为:

<security mode="Transport">
       <transport clientCredentialType="Basic"></transport>
</security>

客户端保持不变:

<security mode="Transport">
     <transport clientCredentialType="Windows"></transport>
</security>

运行客户端,抛出异常:

将客户端安全配置改为Basic,与服务端相同

<security mode="Transport">
            <transport clientCredentialType="Basic"></transport>
</security>

运行客户端,抛出异常,因为Transport模式不支持Basic这种客户端凭据。

2 Message安全模式(本机调试)

wsHttpBinding的Message安全模式,客户端凭据默认为Windows。

服务地址配置为https://localhost/S,服务端与客户端安全模式相同

<security mode="Message">
     <transport clientCredentialType="Windows"></transport>
</security>

运行客户端,抛出异常

抛出异常的原因是,wsHttpBinding的Message安全模式不支持https协议,改为http协议则正常。

但是,当服务端的客户端凭据配置与客户端不一致时,也可以正常执行,并获得正常的结果。

例如,服务端配置:

<security mode="Message">
       <transport clientCredentialType="Basic"></transport>
</security>

客户端配置:

<security mode="Message">
      <transport clientCredentialType="Windows"></transport>
</security>

-----------------------------------------------------------------------------------------

时间仓促,水平有限,如有不当之处,欢迎指正。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏SHERlocked93的前端小站

几种浏览器存储方法及其优缺点

在做项目的过程中,我们经常遇到需要把信息存储在本地的情况,比如权限验证的token、用户信息、埋点计数、客户配置的皮肤信息或语言种类等,我们可以暂存一下避免浏览...

1094
来自专栏非著名程序员

基于 RxJava2+Retrofit2 精心打造的 Android 基础框架 XSnow

XSnow ? 基于RxJava2+Retrofit2精心打造的Android基础框架,包含网络、上传、下载、缓存、事件总线、权限管理、数据库、图片加载、UI模...

2357
来自专栏大内老A

谈谈基于OAuth 2.0的第三方认证 [中篇]

虽然我们在《上篇》分别讨论了4种预定义的Authorization Grant类型以及它们各自的适用场景的获取Access Token的方式,我想很多之前没有接...

1926
来自专栏跟着阿笨一起玩NET

如何创建一个RESTful WCF Service

原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html

451
来自专栏大内老A

Enterprise Library深入解析与灵活应用(2): 通过SqlDependency实现Cache和Database的同步

对于一个真正的企业级的应用来说,Caching肯定是一个不得不考虑的因素,合理、有效地利用Caching对于增强应用的Performance(减少对基于Pers...

2027
来自专栏PingCAP的专栏

TiKV 是如何存取数据的(上)

本文会详细的介绍 TiKV 是如何处理读写请求的,通过该文档,同学们会知道 TiKV 是如何将一个写请求包含的数据更改存储到系统,并且能读出对应的数据的。

962
来自专栏晓晨的专栏

.NET Core 使用RabbitMQ

2182
来自专栏龙渊阁测试精英

Jmeter(二)_基础元件

它用来描述一个测试方案,包含与本次性能测试所有相关的功能。也就说本次测试的所有内容是于基于一个计划的。

642
来自专栏ml

flume安装及配置介绍(二)

注: 环境: skylin-linux Flume的下载方式:   wget http://www.apache.org/dyn/closer.lua/flu...

32311
来自专栏熊二哥

快速入门系列--WebAPI--04在老版本MVC4下的调整

WebAPI是建立在MVC和WCF的基础上的,原来微软老是喜欢封装的很多,这次终于愿意将http编程模型的相关细节暴露给我们了。在之前的介绍中,基本上都基于.N...

2076

扫码关注云+社区