【问题标题】:UserNamePasswordValidator send custom error message to clientUserNamePasswordValidator 向客户端发送自定义错误消息
【发布时间】: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 的错误。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    当从UserNamePasswordValidatorValidate 方法抛出异常时,异常被包装为MessageSecurityException(来自System.ServiceModel.Security 包)的InnerException,这是您必须捕获客户端的类型-方:

    try
    { ... }
    catch (MessageSecurityException e)
    {
        Exception exc = e.InnerException;
        // Process "exc" here, depending of its type
    }
    

    我希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 2019-01-26
      • 2017-04-08
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多