【发布时间】:2012-06-30 21:27:37
【问题描述】:
我开始通过 WCF 为我的 c#/.net 应用程序开发 REST API。
我通过自定义的 UserNamePasswordValidator 使用基本 HTTP 身份验证。它验证我的 API 方法。
它基于这个示例代码,我可以毫无问题地让它工作:
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");
}
}
}
当身份验证失败时,我只能向客户端发送“401 Unauthorized”。我知道这是应该的方式,但是在这种情况下,是否有可能以某种方式将一些详细信息发回给我的来电者?一些自定义的错误消息或更详细的信息。如果没有,我的客户端只有身份验证失败的http信息,在我的情况下还不够。
【问题讨论】:
标签: wcf http authentication rest