【发布时间】:2019-02-12 14:42:32
【问题描述】:
我已经实现了一个IErrorHandler,因为我的服务女巫需要返回 403 而不是 o 400,以防授权错误。
这就是我的ProvideFault 方法的样子:
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (((FaultException)error).Code.SubCode.Name == "FailedAuthentication")
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription =
"StatusCode is forced to change in order to have the correct response on authentication.";
}
}
我想知道是否有任何方法可以为错误及其状态代码编写更好的检查,或者如何获得授权异常而不是FaultException 女巫更通用?我觉得在FailedAuthentication 字符串之后检查并不好,实际上它的授权不是身份验证......
【问题讨论】:
-
哪里引发了异常?如果您可以找到或创建更合适的异常,您可以简单地检查它的类型。
if(error is NotAuthorizedException notAuthed){ // raise 403 error } -
据我所知,
FailedAuthentication代码是 SOAP 1.1 和 1.2 标准 wsse:FailedAuthentication。
标签: c# wcf exception service microservices