2017-02-12 11 views
0
namespace POCApplication 
    { 
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
     [ServiceContract] 
     public interface IService1 
     { 

      [OperationContract] 
      string GetData(int value); 

      [OperationContract] 
      CompositeType GetDataUsingDataContract(CompositeType composite); 


     [OperationContract] 
     string UpdateEmpData(Infromation emp); 


     // TODO: Add your service operations here 
    } 


    // Use a data contract as illustrated in the sample below to add composite types to service operations. 
    [DataContract] 
    public class CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hi test Application "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

     [DataMember] 
     public string StringValue 
     { 
      get { return stringValue; } 
      set { stringValue = value; } 
     } 
    } 

    [DataContract] 
    public class Infromation 
    { 

     [DataMember] 
     public string Name { get; set; } 

     [DataMember] 
     public int Age { get; set; } 

     [DataMember] 
     public string Email { get; set; } 

     [DataMember] 
     public string Gender { get; set; } 
    } 
} 

Service1.svc.cs 

namespace POCApplication 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. 
    public class Service1 : IService1 
    { 
     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     public CompositeType GetDataUsingDataContract(CompositeType composite) 
     { 
      if (composite == null) 
      { 
       throw new ArgumentNullException("composite"); 
      } 
      if (composite.BoolValue) 
      { 
       composite.StringValue += "Suffix"; 
      } 
      return composite; 
     } 

     public string SayHello(string value) 
     { 
      return "Hello:" + value; 
     } 

     public string UpdateEmpData(Infromation emp) 
     { 
      return string.Format("You entered: {0} , {1} , {2} , {3}", 
      emp.Name, emp.Age, emp.Gender, emp.Email); 
     } 
    } 
} 

UserAuthentication.cs 

namespace POCApplication 
{ 
    public class UserAuthentication : UserNamePasswordValidator 
    { 
     public override void Validate(string userName, string password) 
     { 
      try 
      { 
       if (userName == "test1" && password == "test123") 
       { 
        Console.WriteLine("Authentic User"); 
       } 
      } 
      catch (Exception ex) 
      { 
       throw new FaultException("Unknown Username or Incorrect Password"); 
      } 
     } 
    } 
} 

答えて

0

あなたの設定はどこですか?このカスタム検証をあなたの設定に入れず、このような適切なバインドを追加しないとうまくいかない場合は、動作しません。link

カスタムユーザー名検証を作成すると、

これをサービス設定に追加してください。

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

<serviceCredentials> 
    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" /> 
</serviceCredentials> 

を名前空間と、カスタムvalition名を変更し、あなたの結合に応じて変更することを忘れないでください。

関連する問題