WCF身份验证不起作用

时间:2013-04-29 05:08:57

标签: wcf authentication

我正在使用Message Security进行WCF身份验证。我的clientCredentialType =“UserName”。

即使我在访问服务时没有提供有效的用户名和密码,它也能正常工作。

它应该进行身份验证,如果凭据是正确的,那么只有它应该允许访问.enter代码 代码如下: WCF服务行为部分:

 <behaviors>
      <serviceBehaviors>
        <behavior name="AuthenticationBehaviour">
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfServiceAuthentication.Authenticator, WcfServiceAuthentication"/>            
          </serviceCredentials>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>

Web.config中的WCF服务绑定部分

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

我的身份验证类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IdentityModel.Selectors;
using System.ServiceModel;
using log4net;
using System.Reflection;
namespace WcfServiceAuthentication
{
    public class Authenticator : UserNamePasswordValidator
    {
        private static ILog _logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public override void Validate(string userName, string password)
        {
            _logger.Info("Validate called with username:" + userName + " and password:" + password);

            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }

            if (!(userName == "Admin" && password == "Admin123"))
            {
                // This throws an informative fault to the client.
                throw new FaultException("Unknown Username or Incorrect Password");
            }

            _logger.Info("End called");
        }
    }
}

我的身份验证服务

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
    public class AuthenticationService : IAuthenticationService
    {


        public int add(int num1, int num2)
        {
            return (num1 + num2);
        }
    }
}

客户申请:

 AuthenticationServiceClient proxy = new AuthenticationServiceClient();

            //proxy.ClientCredentials.UserName.UserName = "Admin";
            //proxy.ClientCredentials.UserName.Password = "Admin123";
            int addition= proxy.add(10, 10);
            return View();

虽然我没有提供凭据,但Add方法工作正常。它应该要求验证。

1 个答案:

答案 0 :(得分:0)

通过添加以下标记来修改Web配置以启用身份验证服务。

<system.web.extensions>   <scripting>
<webServices>
  <authenticationService enabled="true" 
     requireSSL = "true"/>
</webServices>   </scripting> </system.web.extensions>

应将其添加到Web配置文件中。样本如此

<system.web.extensions>
  <scripting>
    <webServices>
      <authenticationService enabled="true" 
         requireSSL = "true"/>
    </webServices>
 </scripting>
 </system.web.extensions>
 <system.serviceModel>
   <services>
      <service name="System.Web.ApplicationServices.AuthenticationService"
         behaviorConfiguration="AuthenticationServiceTypeBehaviors">
           <endpoint contract=
         "System.Web.ApplicationServices.AuthenticationService"
          binding="basicHttpBinding"
          bindingConfiguration="userHttps" 
          bindingNamespace="http://asp.net/ApplicationServices/v200"/>
      </service>
   </services>
   <bindings>
    <basicHttpBinding>
        <binding name="userHttps">
            <security mode="Transport" />
        </binding>
    </basicHttpBinding>
   </bindings>
    <behaviors>
       <serviceBehaviors>
            <behavior name="AuthenticationServiceTypeBehaviors">
            <serviceMetadata httpGetEnabled="true"/>
    </behavior>
</serviceBehaviors>
   </behaviors>
    <serviceHostingEnvironment 
      aspNetCompatibilityEnabled="true"/>
     </system.serviceModel>