开发者

ASP.Net Web service. How to reject request?

开发者 https://www.devze.com 2023-01-10 10:21 出处:网络
I have asmx web service and I would like to reject all requests coming from all ip addresses except one I know.

I have asmx web service and I would like to reject all requests coming from all ip addresses except one I know.

I used Application_BeginRequest but after I confirm that the ip is not the ip I know, I would like to know what I need to replace the comment in the code bellow.

Thanks

protected void Application_BeginRequest(object sender, EventArgs e)
{
     var  address = "916.222.18.0";
     var ip = Context.Request.ServerVariables["REMOTE开发者_Python百科_ADDR"];

     if (ip != address)
     {
         // reject request
     }
}


Try this:

Context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
Context.Response.End();

Or you can simply redirect to another page that does not have client restrictions:

Context.Response.Redirect("Head-Fake.aspx");


 if (ip != address)
 {
     Context.Response.StatusCode = 401; // Unauthorized
     Context.Response.End();
     // or
     throw new HttpNotFoundException();
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消