如何从 C# 中的异常消息中删除敏感信息
How to strip sensitive information from exception message in c#
目前在我们的日志记录中,我们记录这样的异常消息:
string.Format(
"caught exception while validating token, error detail:{0}", this.exception.ToString())
异常信息中有一些敏感信息(jwt):
System.IdentityModel.Tokens.SecurityTokenValidationException:
Jwt10315: Signature validation failed. Keys tried:
'System.IdentityModel.Tokens.X509AsymmetricSecurityKey
System.IdentityModel.Tokens.X509AsymmetricSecurityKey
System.IdentityModel.Tokens.X509AsymmetricSecurityKey'
我正在考虑解析异常消息字符串,删除敏感信息,然后将其记录下来。用这个剥离的数据创建一个新的异常对象,然后将它传递给其他API的
还有其他方法吗?
I was thinking of parsing the exception message string, removing
sensitive information and then logging it.
解析异常消息可能 none 很容易做到正确,因为您将不得不处理不同的信息和消息类型。相反,我会简单地记录原始异常消息并抛出一个自定义的异常,它大致描述了发生的事情:
try
{
RunSensitiveMethod();
}
catch (SecurityTokenValidationException e)
{
// Perhaps something fancier if needed.
logger.Log(e);
throw new MyCustomException("We screwed up!");
}
在 SecurityTokenValidationException 上捕获异常,然后抛出您自己类型的新自定义异常,该异常仅包含您需要的信息。您仍然可以记录它,并且您会确切地知道它的含义。
目前在我们的日志记录中,我们记录这样的异常消息:
string.Format(
"caught exception while validating token, error detail:{0}", this.exception.ToString())
异常信息中有一些敏感信息(jwt):
System.IdentityModel.Tokens.SecurityTokenValidationException: Jwt10315: Signature validation failed. Keys tried: 'System.IdentityModel.Tokens.X509AsymmetricSecurityKey System.IdentityModel.Tokens.X509AsymmetricSecurityKey System.IdentityModel.Tokens.X509AsymmetricSecurityKey'
我正在考虑解析异常消息字符串,删除敏感信息,然后将其记录下来。用这个剥离的数据创建一个新的异常对象,然后将它传递给其他API的
还有其他方法吗?
I was thinking of parsing the exception message string, removing sensitive information and then logging it.
解析异常消息可能 none 很容易做到正确,因为您将不得不处理不同的信息和消息类型。相反,我会简单地记录原始异常消息并抛出一个自定义的异常,它大致描述了发生的事情:
try
{
RunSensitiveMethod();
}
catch (SecurityTokenValidationException e)
{
// Perhaps something fancier if needed.
logger.Log(e);
throw new MyCustomException("We screwed up!");
}
在 SecurityTokenValidationException 上捕获异常,然后抛出您自己类型的新自定义异常,该异常仅包含您需要的信息。您仍然可以记录它,并且您会确切地知道它的含义。