WCF Windows身份验证

网上的太多太多是不完整的了

我研究了几个小时才弄明白

搞来搞去费神太多

下面我把完整的代码贴上

 

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="服务类">
        <endpoint address="" binding="netTcpBinding" contract="服务接口" bindingConfiguration="myTcpBinding">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:端口/mex" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings >
      <netTcpBinding >
        <binding name="myTcpBinding" >
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

 

以上是Host的app.config

请自己更改3个参数


 

NetTcpBinding binding = new NetTcpBinding();

binding.CloseTimeout = TimeSpan.FromMinutes(5);
binding.OpenTimeout = TimeSpan.FromSeconds(5);
binding.SendTimeout = TimeSpan.FromMinutes(10);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.MaxBufferPoolSize = 2147483647;
binding.MaxBufferSize = 2147483647;

binding.TransactionFlow = false;
binding.TransferMode = TransferMode.Buffered;
binding.TransactionProtocol = TransactionProtocol.OleTransactions;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;

binding.ReaderQuotas.MaxDepth = 64;
binding.ReaderQuotas.MaxStringContentLength = 2147483647;
binding.ReaderQuotas.MaxArrayLength = 2147483647;
binding.ReaderQuotas.MaxBytesPerRead = 4096;
binding.ReaderQuotas.MaxNameTableCharCount = 16384;

binding.ReliableSession.Ordered = true;
binding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
binding.ReliableSession.Enabled = false;

binding.Security.Mode = SecurityMode.Transport;
//binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;

EndpointAddress address = new EndpointAddress(new Uri("net.tcp://" + url + ":8770/mex"), EndpointIdentity.CreateDnsIdentity("localhost"));

ChannelFactory<服务接口> factory = new ChannelFactory<服务接口>(binding, address);
服务接口 client = factory.CreateChannel();

 

以上是客户端实例化代码。

 

自动以当前Windows用户登录。

 

服务器通过OperationContext.Current.ServiceSecurityContext.WindowsIdentity获得登录身份

 

Win7上如果想要WindowsBuiltInRole.Administrator身份

客户端需要使用管理员权限打开程序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值