C# WCF身份验证不支持';不提示输入凭据

C# WCF身份验证不支持';不提示输入凭据,c#,wcf,wcf-security,C#,Wcf,Wcf Security,我已经创建了一个测试WCF应用程序,我正在其中尝试让身份验证工作,但它只运行我的方法,不要求我登录/身份验证。以下是我的WCF应用程序中my web.config中的代码片段: <bindings> <wsHttpBinding> <binding name="Binding1"> <security mode="Message"> <me

我已经创建了一个测试WCF应用程序,我正在其中尝试让身份验证工作,但它只运行我的方法,不要求我登录/身份验证。以下是我的WCF应用程序中my web.config中的代码片段:

<bindings>
       <wsHttpBinding>
           <binding name="Binding1">
               <security mode="Message">
                   <message clientCredentialType="UserName" />
               </security>
           </binding>
       </wsHttpBinding>
</bindings>

<serviceCredentials>
         <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyAPI.Authorization, App_Code" />
</serviceCredentials>
我的Service.svc.cs类

public string Hello(string message)
{
     return "You typed: " + message;
}
我应该在这个方法上面放置一些属性以要求身份验证,还是在类上面

然后,我创建了一个测试控制台应用程序,下面是代码:

public static Test.Service1Client client = new Test.Service1Client();
        static void Main(string[] args)
        {
            Console.WriteLine(client.Hello("hello"));
            Console.ReadLine();
        }
这只输出“youtyped:hello”,而不要求验证。以下是my app.config的片段:

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://MyServer/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="Test.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
但显然不是

编辑


看起来您使用了错误的绑定,当您在服务器上定义WsHttpBinding时,客户端正在使用BasicHttpBinding。

WCF/IIS将神奇地连接您拥有的服务,而无需进行配置(无法立即记住此功能的名称…)。但是,您正在定义一些自定义绑定配置——这很好,但是您需要告诉您的服务使用它

您需要将
元素添加到服务器的配置中,例如:

<system.serviceModel>
   ...
   <services>
      <service name="FullClassNameOfYourService">
         <endpoint binding="wsHttpBinding"
                   bindingConfiguration="Binding1"
                   contract="FullClassNameOfYourServiceContract" />
      </service>
   </services>
<userNameAuthentication userNamePasswordValidationMode="Custom"
 customUserNamePasswordValidatorType="MyNamespace.Authorization , MyNamespace" />

...

此外,您的客户端配置不包含
wsHttpBinding
元素这一事实表明,承载您的服务的网站未启用HTTPS。

您的用户名验证不正确。customUserNamePasswordValidatorType的格式必须为“[完全限定程序集+类名],[命名空间]”。我无法从您的帖子中看出您的名称空间是什么,但类似于:

<system.serviceModel>
   ...
   <services>
      <service name="FullClassNameOfYourService">
         <endpoint binding="wsHttpBinding"
                   bindingConfiguration="Binding1"
                   contract="FullClassNameOfYourServiceContract" />
      </service>
   </services>
<userNameAuthentication userNamePasswordValidationMode="Custom"
 customUserNamePasswordValidatorType="MyNamespace.Authorization , MyNamespace" />

正如其他人所说,您的客户端必须使用相同的绑定类型来连接到服务


此外,在服务器端,您已将安全模式设置为“无”,但您有一个传输和消息标记。在安全标签中。如果您不执行任何安全性规范,则会忽略任何传输和消息安全性规范。换句话说,在安全模式为“无”的情况下,您的客户端凭据类型将被忽略,因此客户端不必进行身份验证。

您使用具有简化配置文件的WCF 4。它有其优点,但调试起来比较困难。我怀疑您的自定义wshttpbinding未应用。尝试更详细的配置(如wcf 3.5):



通常,客户端(如浏览器)会提示输入凭据。您的测试控制台应用程序将不会提示,因为您没有这样编码。说如果您没有提供凭据,服务调用应该失败。检查您是否已使用服务器配置中的Binding1 biding绑定服务。@VinayC-在哪里可以看到?查看我的编辑,它包含我的服务中的整个web.config文件我现在得到:在服务“Service1”实现的合同列表中找不到合同名称“MyAPI.IService1”。属性的值是否仅为“Serivce1”?根据您的合同类型,我认为服务名称应该是“MyAPI.Service1”-类名也需要名称空间。我有以下设置:
hmm,您可能需要在您的问题中发布您的服务合同接口和服务实现类,我有点困惑!我将其更改为:
,错误仍然存在,但我仍然无法进行身份验证,我仍然可以运行
客户端。Hello(“Hello”)
,而不传递任何身份验证详细信息。我已经尝试过,但它不断抛出错误,这就是为什么我将授权类放在App_代码中并使用它,成功了!现在,我已将安全类型设置为Transport,并在客户端和服务器上设置了相同的绑定类型(都使用basicHttpbinding),但出现以下错误:
提供的URI方案“http”无效;应为“https”。参数名称:via
“错误”对我帮助不大。如果您遇到不同的错误,您如何知道将其放入App_代码中是有效的?如果它正在寻找https,这似乎表明您已经指定了传输级别的安全性。所谓“错误”,我的意思是它抛出了一个错误,直到我将其更改为使用App_代码,然后它才“工作”,即没有抛出错误,但这不是问题所在
<userNameAuthentication userNamePasswordValidationMode="Custom"
 customUserNamePasswordValidatorType="MyNamespace.Authorization , MyNamespace" />
<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="NewBinding0">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="WcfService6.Service1Behavior"
        name="WcfService6.Service1">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="NewBinding0"
          contract="WcfService6.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService6.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>