现在的位置: 首页 > 综合 > 正文

WCF NetTcpBinding Transport安全模式(2) 默认安全配置

2013年02月01日 ⁄ 综合 ⁄ 共 4006字 ⁄ 字号 评论关闭

WCF NetTcpBinding Transport安全模式(2)  默认安全配置

新建一个类库名为“WcfSecurityExampleServiceLibrary类库项目,添加如代码清单11-10所示契约,其中将示例契约命名为HelloService

代码清单11-10  HelloService契约

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

 

namespace WcfSecurityExampleServiceLibrary

{

     [ServiceContract]

    public interface IHelloService

    {

        [OperationContract]

        string GetHello();

    }

}

代码清单11-11HelloService契约实现。

代码清单11-11    HelloService契约实现

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

 

namespace WcfSecurityExampleServiceLibrary

{

    public class HelloService : IHelloService

    {

    

        public string GetHello()

        {

             if (ServiceSecurityContext.Current != null)

            {

 

 

                if (!ServiceSecurityContext.Current.IsAnonymous)

                {

 

                    return "Hello:" + ServiceSecurityContext.Current.PrimaryIdentity.Name + ";type="

                        + ServiceSecurityContext.Current.PrimaryIdentity.AuthenticationType;

 

                }

                return "";

            }

            else

            {

                return "hello";

            }        }

    }

}

这里采用控制台程序做自托管宿主,宿主代码如代码清单11-12所示。

代码清单11-12    宿主代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using WcfSecurityExampleServiceLibrary;

 

namespace SimpleHost

{

    class Program

    {

        static void Main(string[] args)

        {

            ServiceHost hostForHello = new ServiceHost(typeof(HelloService));

            hostForHello.Open();

            try

            {

                while (true)

                {

 

                }

            }

            catch

            {

              

                hostForHello.Abort();

            }

        }

    }

}

宿主配置文件如代码清单11-13所示。

代码清单11-13    宿主配置文件

<?xml version="1.0"?>

<configuration>

  <system.serviceModel>

    <services>

      <service name="WcfSecurityExampleServiceLibrary.HelloService" behaviorConfiguration="mex">

        <host>

          <baseAddresses>

            <add baseAddress="net.tcp://127.0.0.1:64567/"/>

          </baseAddresses>

        </host>

        <endpoint address="net.tcp://127.0.0.1:64567/HelloService" binding="netTcpBinding"

                  bindingConfiguration="tcpWindowsSecurity" name="helloEndPoint"

                  contract="WcfSecurityExampleServiceLibrary.IHelloService"/>

  

        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />

      </service>

    </services>

    <bindings>

 

      <netTcpBinding>

        <binding name="tcpWindowsSecurity">

        </binding>

      </netTcpBinding>

    </bindings>

 

    <behaviors>

      <serviceBehaviors>

        <behavior name="mex">

          <serviceMetadata  />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

 

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

  </startup>

</configuration>

代码清单11-13所示配置文件并没有对netTcpBinding做任何安全配置,因此一切将采用默认设置。

客户端实现如代码清单11-14所示。

代码清单11-14    客户端实现

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using WcfSecurityExampleServiceLibrary;

 

namespace WcfSecurityExampleConsoleClient

{

    class Program

    {

        static void Main(string[] args)

        {

            using (ChannelFactory<IHelloService> channelFactory = new ChannelFactory<IHelloService>("helloEndPoint"))

            {

                IHelloService helloService = channelFactory.CreateChannel();

                using (helloService as IDisposable)

                {

                    Console.WriteLine(helloService.GetHello());

                              }

            }

         Console.Read();

        }

    }

}

在代码请单11-14所示实现中,首先根据配置文件创建服务管道,然后请求服务GetHello方法,输出结果。客户端配置文件,如代码清单11-15所示。

代码清单11-15    客户端配置文件

<?xml version="1.0" encoding="utf-8" ?>

<configuration>       

  <system.serviceModel>

    <bindings>

      <netTcpBinding>

        <binding name="tcpWindowsSecurity">

        </binding>

抱歉!评论已关闭.