ASP.NET:需要基于网络掩码登录

发布于 2024-08-16 07:03:29 字数 198 浏览 2 评论 0原文

我需要保护对 .NET web 应用程序中所有页面的访问 - 除了以下请求:

  • 本地网络(运行 IIS 的网络)
  • 数据库中列出的 IP/网络掩码

所有其他请求都应重定向到

我正在考虑的 登录表单HttpModule 的方向 - 但从未编写过。 谁能对此提供任何想法?

谢谢你!

I need to secure access to all pages in a .NET webapp - EXCEPT requests from:

  • local network (the network IIS is running on)
  • IPs listed/netmasks listed in a database

all other requesets should be redirected to a login form

I was thinking in the direction of a HttpModule - but never wrote one.
Can anyone provide any ideas to this?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不咬妳我踢妳 2024-08-23 07:03:29

使用 HttpModule 将是实现此目的的最佳方法。您可以使用它在页面执行之前捕获任何请求,并在需要时重定向到登录表单。

public class SecurityModule : IHttpModule
{
    private HttpApplication m_HttpApplication;

    public void Init(HttpApplication context)
    {
        m_HttpApplication = context;
        m_HttpApplication.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
    }

    public void Dispose()
    {
        // Do Nothing
    }

    private void OnPreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Get IP address
        string ipAddress = m_HttpApplication.Context.Request.UserHostAddress;

        // Check if the IP address requires login
        bool requiresLogin = ValidateIpAddress(ipAddress);

        // Redirect if required
        if (requiresLogin)
            Response.Redirect("~/Login.aspx", true);
        }

        private bool ValidateIpAddress(string ipAddress)
        {
            // This method would check that the IP address is from the local
            // network or in the database and return true or false accordingly.

            return false;
        }
    }

您还需要修改 web.config 并添加对该模块的引用:

<httpModules>
    <add name="SecurityModule" type="MyApp.SecurityModule, MyApp"/>
</httpModules>

此代码还需要一些修改以确保登录的用户不会重定向回登录页面,但这应该足以让您开始了。

Using a HttpModule would be the best way to do this. You could use this to catch any requests before the page executes and redirect to the login form if required.

public class SecurityModule : IHttpModule
{
    private HttpApplication m_HttpApplication;

    public void Init(HttpApplication context)
    {
        m_HttpApplication = context;
        m_HttpApplication.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
    }

    public void Dispose()
    {
        // Do Nothing
    }

    private void OnPreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Get IP address
        string ipAddress = m_HttpApplication.Context.Request.UserHostAddress;

        // Check if the IP address requires login
        bool requiresLogin = ValidateIpAddress(ipAddress);

        // Redirect if required
        if (requiresLogin)
            Response.Redirect("~/Login.aspx", true);
        }

        private bool ValidateIpAddress(string ipAddress)
        {
            // This method would check that the IP address is from the local
            // network or in the database and return true or false accordingly.

            return false;
        }
    }

You'll also need to modify web.config and add a reference to the module:

<httpModules>
    <add name="SecurityModule" type="MyApp.SecurityModule, MyApp"/>
</httpModules>

This code would also need some modification to ensure that users who are logged in are not redirected back to the login page, but it should be enough to get you started.

相对绾红妆 2024-08-23 07:03:29

我宁愿构建一个全局身份验证方法来检查 IP。在 MasterPage 的 OnInit 或 OnLoad 或您自己的 System.Web.Page 实现中调用此函数应该可以解决问题。

如果用户必须登录,请在会话中设置一些随机生成的 ID 进行检查(将随机 ID 保存到数据库和会话中)。在全局身份验证方法中,您现在可以检查有效的 IP 范围或有效的(数据库注册的)会话令牌。

I'd rather build a global authentication method to check against the ip. Calling this function in the OnInit or OnLoad of your MasterPage or your own implementation of System.Web.Page should do the trick.

If the user has to login, set some randomly generated id in your session to check against (saving the random id to your database and session). In your global authentication method, you can now check for the valid ip range or a valid (database-registred) session token.

回忆凄美了谁 2024-08-23 07:03:29

下面是一个基于正则表达式的自定义授权模块:
http://code.google.com/p/talifun-web/wiki/ RegexUrlAuthorizationModule

它应该很容易重构以满足您的需求。

Here is a custom authorization module based on regular expressions:
http://code.google.com/p/talifun-web/wiki/RegexUrlAuthorizationModule

It should be easy to refactor to your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文