I started to develop a REST API for my c#/.net app by WCF.
I use basic HTTP authentication by a customized UserNamePasswordValidator. It authenticates my API methods.
It based on this sample code and I could make it work without any problem:
public class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (null == userName || null == password)
{
throw new ArgumentNullException("You must provide both the username and password to access this service");
}
if (!(userName == "user1" && password == "test") && !(userName == "user2" && password == "test"))
{
throw new FaultException("Unknown Username or Incorrect Password");
}
}
}
When authentication fails, I can’t send back nothing but a ‘401 Unauthorized’ to the client. I know it is the way it should be, but is it possible somehow send back some detailed information back to my caller in this case? Some customized error message or some more detailed info. If not, my client has only authentication failed http info, and it is not enough in my case.
maybe you should use WebFaultException<> , in my case I used like this :
throw new WebFaultException<string>("Bad Request", HttpStatusCode.Unauthorized);