首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在BasicHttpBinding中的WCF服务中进行身份验证?

在BasicHttpBinding中的WCF服务中进行身份验证,可以使用以下方法:

  1. 使用用户名和密码进行身份验证:

在客户端代理类中,设置用户名和密码,并在服务端配置身份验证。

客户端代码:

代码语言:csharp
复制
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service");

ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(binding, endpointAddress);
channelFactory.Credentials.UserName.UserName = "username";
channelFactory.Credentials.UserName.Password = "password";

ICalculator client = channelFactory.CreateChannel();

服务端配置:

代码语言:xml<system.serviceModel>
复制
  <bindings>
   <basicHttpBinding>
     <binding name="secureBinding">
       <security mode="TransportCredentialOnly">
         <transport clientCredentialType="Basic" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
 <services>
   <service name="MyService">
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureBinding" contract="ICalculator" />
    </service>
  </services>
  <behaviors>
   <serviceBehaviors>
      <behavior>
       <serviceCredentials>
          <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyNamespace.CustomUserNameValidator, MyAssembly" />
        </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>
  1. 使用证书进行身份验证:

在客户端代理类中,设置证书,并在服务端配置身份验证。

客户端代码:

代码语言:csharp
复制
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service");

ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(binding, endpointAddress);
channelFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "client.com");

ICalculator client = channelFactory.CreateChannel();

服务端配置:

代码语言:xml<system.serviceModel>
复制
  <bindings>
   <basicHttpBinding>
     <binding name="secureBinding">
       <security mode="Transport">
         <transport clientCredentialType="Certificate" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
 <services>
   <service name="MyService">
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureBinding" contract="ICalculator" />
    </service>
  </services>
  <behaviors>
   <serviceBehaviors>
      <behavior>
       <serviceCredentials>
         <serviceCertificate findValue="MyServiceCertificate" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
         <clientCertificate>
           <authentication certificateValidationMode="PeerOrChainTrust" />
          </clientCertificate>
        </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

这样,就可以在BasicHttpBinding中的WCF服务中进行身份验证了。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券