2013-04-29 3 views
0

Я использую Message Security для проверки подлинности WCF. И мой clientCredentialType = "UserName".Проверка подлинности WCF не работает

Даже если я не предоставляю действительное имя пользователя и пароль при доступе к службе, он работает нормально.

Он должен сделать проверку подлинности, если учетные данные верны, то только она должна позволять access.`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> 

службы WCF Связывание раздел в Web.config

<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(); 

Вот хотя я не предоставляет учетные данные, Добавить метод работает отлично. Он должен запросить аутентификацию.

+0

Пожалуйста, показать код, если вы хотите помочь. – Tim

ответ

0

Измените веб-конфигурацию, добавив теги ниже, чтобы включить службу аутентификации.

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

Его следует добавить в файл конфигурации сети. Образец здесь, как здесь

<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> 
+0

Где это должно быть добавлено? В конфигурации клиента или конфигурации службы. Я добавил его в конфигурацию службы wcf. то он также не аутентифицируется. –