无法在wcf服务中提供身份验证和授权

时间:2017-02-12 07:25:54

标签: wcf

    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");
            }
        } 
    }
}

1 个答案:

答案 0 :(得分:0)

那么你的配置在哪里。如果您不在配置中添加自定义验证并添加正确的绑定(例如此link

,它将无法工作

当您创建自定义用户名验证时,不要期望它能够自动运行。

在服务配置中添加此项。不要忘记更改名称空间和自定义名称

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

并相应地更改您的装订:

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