开发者

Local access only for ASP.NET web page

Is it p开发者_如何学运维ossible to configure web.config to authorize a page to be only read locally (similar in concept to the RemoteOnly feature for error messages).


You can check this on the Page that you wish to. Here is an example code that I write and check if the user is local or not.

override protected void OnInit(EventArgs e)
{
    if (!IsUserLocal())
    {
        Response.Redirect("~/");
        return;
    }

    base.OnInit(e);
}

public bool IsUserLocal()
{
    string userHostAddress = Request.ServerVariables["REMOTE_HOST"].ToString();

    if (string.IsNullOrEmpty(userHostAddress))
    {
        return false;
    }
    return (((userHostAddress == "127.0.0.1") || (userHostAddress == "::1")) || (userHostAddress == LocalAddress()));
}


public string LocalAddress()
{
    IServiceProvider provider = (IServiceProvider)HttpContext.Current;
    HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

    return wr.GetLocalAddress();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜