代码之家  ›  专栏  ›  技术社区  ›  pdiddy

WCF安全身份验证

  •  8
  • pdiddy  · 技术社区  · 14 年前

     <system.serviceModel>
        <services>
          <service name="WcfService.Service1" behaviorConfiguration="WcfService.Service1Behavior">
            <host>
              <baseAddresses>
                <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfService/Service1/" />
              </baseAddresses>
            </host>
            <endpoint address ="" binding="wsHttpBinding" contract="WcfService.IService1">
              <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="WcfService.Service1Behavior">
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="True" />
    
              <serviceCredentials>
                <userNameAuthentication userNamePasswordValidationMode = "Windows"/>
              </serviceCredentials>
    
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    

    这是我的委托人应用程序配置

    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
              <binding name="WSHttpBinding_IService1">
    
                  <security mode = "Message">
                    <message  clientCredentialType = "UserName"/>
                  </security>
    
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8731/Design_Time_Addresses/WcfService/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
    

    这是我的客户代码

    ServiceReference1.Service1Client client = new WcfAuthentication.ServiceReference1.Service1Client();
    
    client.ClientCredentials.UserName.UserName = "mywindowsusername";
    client.ClientCredentials.UserName.Password = "mywindowsuserpassword";
    Console.WriteLine(client.GetData(5));
    

    {“无法打开安全通道,因为与远程终结点的安全协商失败。这可能是由于用于创建通道的EndpointAddress中缺少EndpointIdentity或未正确指定EndpointIdentity造成的。请验证EndpointAddress指定或暗示的EndpointIdentity是否正确标识远程终结点。“} {“对安全令牌的请求包含无效或格式错误的元素。”}

    3 回复  |  直到 8 年前
        1
  •  7
  •   Ronald Wildenberg    14 年前

    看起来您分别(手动)生成了服务和客户机配置。从服务中使用 svcutil 或Visual Studio的“添加服务引用”。这样您就知道您得到了与服务配置相对应的客户机配置。

    你想要的是可能的,但是WCF不允许你在使用时以纯文本形式传输你的用户名/密码令牌 wsHttpBinding . 这意味着您必须使用https托管服务或使用服务证书。 Here

    但我也想知道你为什么会想要这样的东西。最好使用集成的Windows身份验证。这甚至是的默认设置 wsHttpBinding . 这样,您就不需要客户端输入他们的Windows用户名/密码。

        2
  •  2
  •   stombeur    14 年前

    我认为使用WsHttpBinding的Windows身份验证只适用于https。

    看这个: http://msdn.microsoft.com/en-us/library/ff650619.aspx

        3
  •  -2
  •   tonytonov QIBIN LI    10 年前
    binding.Security = new WSHttpSecurity{Mode = SecurityMode.None};