Search code examples
wcfsessionwcf-security

Avoid to create session in WCF


Is there a way to cancel the creation of the session if the method with the IsInitiating causes a validation error?

I am requiring the user and password in the first method and I do not want to create the session if the credentials are not valid. In fact, I want to return an object indicating if the credentials validation were successful.

Thank you in advance.


Solution

  • public override void Validate(string userName, string password)
    {
        if (!(userName == "test1" && password == "1tset"))
        {
            throw new FaultException("Unknown Username or Incorrect Password");
            // or
            // throw new SecurityTokenException("Unknown Username or Incorrect Password");
        }
    }
    

    For more information see: https://msdn.microsoft.com/en-us/library/aa702565%28v=vs.110%29.aspx