Search code examples
c#web-serviceswcfsoapwshttpbinding

How to return operation result in custom validator in soap web service at c#


I write one wcf soap web service with ws securtiy.I write custom validator .But I have problem.My custom validator only return; or send to throw exception.But I want to return operation result.I attached code below.How can I do?

  public class CustomValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {

            OperationResult retVal = new OperationResult()
            {
                ReturnCode = 0,
                ReturnMessage = "OK"
            };

            string s_userName = ConfigurationManager.AppSettings["username"].ToString();
            string s_password = ConfigurationManager.AppSettings["password"].ToString();
            if (userName == null || password == null)
            {
                throw new ArgumentNullException();--I dont want
                 return retVal; --It is good for me

            }

            if (userName == s_userName && password == s_password)
            {

                return;

            }
            else
            {

             //return;
               throw new FaultException("-2 Unauthorized");--I dont want this
               return retVal; --This giving problem.How can I solve?
            }

        }
    }

Solution

  • your validate method needs to return OperationResult not void, that's your problem.

    So the signature should be

    public override OperationResult Validate(string userName, string password)
    

    This means you'll need to update your UserNamePasswordValidator class as well to match this signature.