C# 在服务堆栈中返回403状态代码
我有一个使用服务堆栈的web服务,我正在进行黑名单检查,以停止处理来自黑名单IP地址列表的请求 如何将403状态代码返回给用户 这是我到目前为止试过的C# 在服务堆栈中返回403状态代码,c#,.net,
servicestack,C#,.net,
servicestack,我有一个使用服务堆栈的web服务,我正在进行黑名单检查,以停止处理来自黑名单IP地址列表的请求 如何将403状态代码返回给用户 这是我到目前为止试过的 public static CustomHttpResult CheckIfIPIsBlackListed(string ipAddress) { try { DataClasses1DataContext db = new DataClasses1D
public static CustomHttpResult CheckIfIPIsBlackListed(string ipAddress)
{
try
{
DataClasses1DataContext db = new DataClasses1DataContext();
int resultCount = (from r in db.WebService_BlackLists
where r.BlackList_IPAddress == ipAddress
select r).Count();
//check if we have 1 or more record
if(resultCount >= 1)
{
return CustomHttpResult[HttpStatusCode.Forbidden];
}
return null;
}
catch (Exception exc)
{
return null;
}
}
在服务内部,您可以(或返回),例如:
在过滤器内部,您可以使用:
在服务内部,您可以(或返回),例如:
在过滤器内部,您可以使用:
非主题评论:我会将此移到网络层(通过切断路由器上此类IP的通信量)您是否尝试了第一个google结果,如
抛出新的HttpError(HttpStatusCode.Forbidden,“某些错误消息”)代码>从那里github:谢谢大家。因此,我添加了以下内容:if(CommonFunctions.CheckIfIPIsBlackListed(userSession.RequestIP)){throw new HttpError(HttpStatusCode.Forbidden,“Forbidden”);}到接收请求的接口,该接口似乎正在工作。非主题注释:我会将其移动到网络层(通过切断路由器上此类IP的通信)您是否尝试过第一个google结果,比如抛出新的HttpError(HttpStatusCode.Forbidden,“一些错误消息”)代码>从那里github:谢谢大家。因此,我添加了以下内容:if(CommonFunctions.CheckIfIPIsBlackListed(userSession.RequestIP)){throw new HttpError(HttpStatusCode.Forbidden,“Forbidden”);}到接收请求并似乎正常工作的接口。哦,整洁,我不知道HttpError,有很多情况下我会使用它。哦,整洁,我不知道HttpError,有太多的情况我会使用它。
throw HttpError.Forbidden("Thou shall not pass");
res.StatusCode = (int)HttpStatusCode.Forbidden;
res.StatusDescription = "Thou shall not pass";
res.EndRequest();