应用程序级别 Intranet 用户的 Windows 身份验证(带有 VB 的 ASP.NET)
我的公司使用 ActiveDirectory,IT 部门自然希望保持对其的控制,而不是将控制权交给其他用户。我正在使用 SQL Server 2008 数据库开发 ASP.NET 应用程序(仅供内部使用)。
我的问题是,如何最好使用 .NET 命名空间和 SQL Server 在应用程序(或可能是数据库)级别管理组织内对应用程序的访问?我希望根据 ActiveDirectory 提供的网络用户名对用户进行授权。
顺便说一句,我还想访问他们的 AD 联系信息。
根据我的理解,我可以在 System.DirectoryServices 命名空间中使用 ActiveDirectoryMembershipProvider 类或域服务。还有 LDAP,这显然是另一种可能性。我很难理解这一切,更不用说哪个是最好的选择以及如何实现它。任何人都可以为我提供一些方向和可能的一些简单的示例代码吗?
更新:抱歉,我忘了提及我正在使用 VB.NET 作为我的代码源,因为它是公司标准。
多谢! ;)
My company uses ActiveDirectory, and naturally the IT department wants to maintain control over it and not give control to other users. I'm developing an ASP.NET app (for internal use only) using an SQL Server 2008 database.
My question is, how can I BEST use the .NET namespaces and SQL Server to manage access to the app within the organization at the application (or possibly DB) level? I would like the user to be authorized based on their network username as provided by ActiveDirectory.
On a side note, I would also like to access their AD contact information.
From my understanding I can use the ActiveDirectoryMembershipProvider class or Domain Services in the System.DirectoryServices namespace. There's also LDAP which apparently is another possibility. I'm having a hard time making sense of it all, and much less which is the best option and how to implement it. Can anyone provide me with some direction and possibly some simple sample code?
UPDATE: Sorry, I forgot to mention I'm using VB.NET as my code source as it's company standard.
Much obliged! ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一段快速代码片段,它开启了我的 Active Directory 和 ASP.NET 永无止境的旅程。在一个小测试页面中,我放置了一个文本框并插入了 LAN ID,它会返回所有可用的 AD 字段。
要获取当前经过身份验证的用户的 AD 登录 ID,
Request.ServerVariables("LOGON_USER")
和Httpcontext.Current.User.Identity.Name
将成为您的朋友。请记住,如果您使用允许匿名安全访问 ASP 页,则不会填充 LOGON_USER 变量。< /a>我将回顾我的一些笔记,并尝试找到一些我使用过的、最终对我最有帮助的资源。我可以立即告诉您《目录服务编程的 .NET 开发人员指南》一书 (亚马逊)是我每天使用的。
Here's a quick code snippet that started me on my never ending journey with Active Directory and ASP.NET. It's from a little test page that I put a text box and plugged in a LAN ID and it returns all the AD fields available.
To get the AD login ID of the currently authenticated user
Request.ServerVariables("LOGON_USER")
andHttpcontext.Current.User.Identity.Name
are going to be your friends. Keep in mind that the LOGON_USER variable is not populated if you use the Allow Anonymous security to access the ASP page.I'll go back through some of my notes and try to find some of the resources that I used that ended up being the most helpful for me. Off the top of my head, I can tell you that the book "The .NET Developer's Guide to Directory Services Programming" (Amazon) was used daily by me.
后来我发现,Windows 身份验证模式在内网环境中运行得很好。我决定在开发时在 IIS 配置中启用此模式,并让每个页面通过网络登录自动识别用户并指向他们(或允许/禁止访问适当的页面)。请记住,我的解决方案仅适用于 Intranet/如果您位于域控制器上。
这是相关的 Web.config
我认为最简单的方法是在数据库中查找用户名并找到其关联的角色/组,然后评估该角色是否应该允许他们访问 VB 代码隐藏中的请求页面。以下是如何获取用户网络用户名:
这是一些示例 VB.NET 代码,可用于(自动)进行身份验证并允许访问给定页面(不完全是我使用的代码,只是一个示例)。
我希望有些人觉得这很有用。我花了无数的时间来解决与此几乎相同的解决方案。
干杯;)
As I later come to find out, Windows authentication mode works perfectly within an intranet environment. I decided to enable this mode in my IIS configuration while developing and have each page automatically identify the user by their network login and point them (or allow/disallow access to the appropriate pages). Keep in mind that my solution ONLY works on an intranet / if you're on a domain controller.
Here's the relevant Web.config
The simplest approach I think is to lookup the username in the database and find their associated role/group, then evaluate whether or not that role should give them access to the requested page in the VB codebehind. Here's how to get the users network username:
Here's some sample VB.NET code that can be used for authenticating (automatically) and allowing access to a given page (not precisely the code I used, just a sample).
I hope some people find this useful. I spent countless hours settling on a solution almost identical to this.
Cheers ;)
使用“在数据库中查找用户名并找到其关联的角色/组”的接受答案恕我直言,它忽略了整个要点。
解决方案是在 Visual Studio 中选中 NTLM 身份验证复选框(使用版本 2012 位于项目的“属性”、“Web”、“服务器”下;其他版本应该类似)。
The accepted answer using "lookup the username in the database and find their associated role/group" IMHO is missing the whole point.
The solution is to check NTLM Authentication check box in Visual Studio (using version 2012 is under project's Properties, Web, Servers; other version should be similar).