ASP.NET:需要基于网络掩码登录
我需要保护对 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 HttpModule 将是实现此目的的最佳方法。您可以使用它在页面执行之前捕获任何请求,并在需要时重定向到登录表单。
您还需要修改 web.config 并添加对该模块的引用:
此代码还需要一些修改以确保登录的用户不会重定向回登录页面,但这应该足以让您开始了。
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.
You'll also need to modify web.config and add a reference to the module:
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.
我宁愿构建一个全局身份验证方法来检查 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.
下面是一个基于正则表达式的自定义授权模块:
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.