在ASP.net上读取Active Directory参数 [英] Reading Active Directory parameters on ASP.net
问题描述
我被困在Somthing:
我想读一个与登录用户相关的属性,即员工ID ,
我的代码是:
导入两个库:
< br $> b $ b
Hi ,
I Stuck in Somthing :
I wanna Read an attribute which is related with logged on user which is Employee ID ,
My Code is :
Importing two libraries :
Imports System.Security.Principal
Imports System.DirectoryServices.AccountManagement
然后读取参数EmployeeID:
Then Reading Parameter EmployeeID :
Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
Session("UserEmpID") = currentADUser.EmployeeId
当我在visual studio上运行它时,它就是它KS完美,但是当我发布时间IIS它,这错误所示:
无法转换类型的对象 'System.DirectoryServices.AccountManagement.GroupPrincipal' 来输入'System.DirectoryServices.AccountManagement.UserPrincipal'
当我深入搜索时,我发现该网站应具有访问Active Directory数据库的凭据,但我怎么也不知道。
请帮助我被困住。
谢谢
When I Run this on visual studio , it works Perfectly , but when I Published it on IIS , this Error Shown :
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'
When I searched deep I find out that this website should have a credential to access Active Directory database , but how I couldn't know.
Please help I Am Stuck.
Thanks
推荐答案
我找到了解决方案:
首先:IIS身份验证应该是Windows身份验证
第二:域用户应该是以这种格式插入web.config:
I found the solution:
First : IIS authentication should be Windows Authentication
Second : a Domain user should be inserted in web.config in this format :
<configuration><appsettings>
<add key="LDAPsvcAcct" value="domain\username" />
<add key="LDAPsvcPass" value="user_password" />
</appsettings></configuration>
第三:那么你可以使用这段代码:
Third : then you can use this code:
Dim Username As String = ""
Dim identityName = User.Identity.Name
Using HostingEnvironment.Impersonate()
Using context = New PrincipalContext(ContextType.Domain, "Domain.com", Nothing, ContextOptions.Negotiate Or ContextOptions.SecureSocketLayer)
Using userPrincipal__1 = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, identityName)
EmpID = userPrincipal__1.EmployeeId
Session("EmpID") = userPrincipal__1.EmployeeId 'To Read attributes
Username = userPrincipal__1.DisplayName
End Using
End Using
End Using
这篇关于在ASP.net上读取Active Directory参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!