【发布时间】:2015-04-17 05:25:19
【问题描述】:
我有一个实现 UserNamePasswordValidator 的自定义用户验证器。 此验证器正在调用另一个使用 WCF 验证用户的身份验证 Web 服务。
它工作正常,但我想向最终客户端抛出一条自定义消息,以防身份验证服务调用(在中间服务器端)由于某种原因(FaultException、CommunicationException、TimeoutException 或任何其他异常)失败。
我在客户端上得到的总是相同的消息弹出,告诉身份验证基本失败。
如何将自定义消息返回给客户端(作为 FaultException 或任何其他方式)?
class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
try {
string domain="";
string userNameOnly;
//Parse userName to extract domain and userNameOnly
if(userName.Contains('\\')) {
String[] splitted = userName.Split('\\');
domain = splitted[0];
userNameOnly = splitted[1];
}
else {
userNameOnly = userName;
}
//throw new FaultException("Test fault exception from CustomUserNameValidator");
//Get the authentication service
ServiceReference1.AuthWSClient service = new ServiceReference1.AuthWSClient("AuthWSPort");
//Call the athentication service
ServiceReference1.user user = service.connect(domain, userNameOnly, password);
//TODO: Catch exceptions (invalid user throws one)
//throw new SecurityTokenValidationException("Authentication failed for \"" + userName + "\"");
}
catch (FaultException ex)
{
//throw new FaultException("Authentication service (called from ATLAS server) returned an error: \n\n" + ex.Message);
throw new SecurityTokenValidationException("Authentication service (called from ATLAS server) returned an error: \n\n" + ex.Message);
}
catch (CommunicationException ex)
{
//throw new FaultException("Communication issue with authentication service (called from ATLAS server): \n\n" + ex.Message);
throw new SecurityTokenValidationException("Communication issue with authentication service (called from ATLAS server): \n\n" + ex.Message);
}
catch (TimeoutException ex)
{
//throw new FaultException("Connection with authentication service (called from ATLAS server) has timed out: \n\n" + ex.Message);
throw new SecurityTokenValidationException("Connection with authentication service (called from ATLAS server) has timed out: \n\n" + ex.Message);
}
//catch (Exception ex)
//{
// //Unexpected exception
// log.LogError(ex.ToString());
// throw;
//}
}
}
客户端有类似的 try/catch 块,但看起来它没有用于 UserNamePasswordValidator:似乎有一个特定的 WCF 内部捕获来自 UserNamePasswordValidator 的错误。
【问题讨论】: