ASP.NET获取当前用户名
我试图建立在使用ASP.NET和VB.NET我们公司的内部网的应用程序。
I am trying to build an application on our company's intranet using ASP.NET and VB.NET.
一旦我的应用程序发布到IIS这些都不函数返回任何东西。他们工作在发展精细(即:pressing F5我得到我的正常网络用户名),但一旦公布他们返回''(空字符串)
Neither of these functions return anything once my application is published to IIS. They work fine in development (ie: pressing F5 I get my regular network username), but once published they return '' (an empty string).
HttpContext.Current.User.Identity.Name
Page.User.Identity.Name
我在寻找的东西 - 什么 - 这将抓住当前用户的登录名。请注意,我不能在我的web.config更改这些设置,因为其他功能的需求。
I'm looking for something -- anything -- that will grab the current users login name. Please note that I CANNOT change these settings in my web.config, because of other functionality requirements.
<authentication mode="Windows"/>
<roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="AspNetWindowsTokenRoleProvider" cookieName=".ASPXROLES" cookiePath="/" cookieTimeout="480" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieProtection="All" />
我也不能更改任何IIS设置,包括启用匿名用户设置。规格是一成不变的,我不得不砍掉我的腿自己(或负责人),让他们改变。
Nor can I change any IIS settings, to include the 'Enable Anonymous User' setting. Specs are cast in stone and I'd have to chop off my own leg (or head) to get them changed.
我觉得有一定是一种方式来获得登录的用户名与我当前的配置中的电流。
I would think there's got to be a way to get the current logged in user's name with my current configuration.
任何想法?
谢谢,
杰森
下面是我发现了什么(地方),并结束了使用。希望它可以帮助别人那里!
Here's what I found (somewhere), and ended up using. Hope it can help someone else out there!
Public Shared Function Check_If_Member_Of_AD_Group(ByVal username As String, _
ByVal grouptoCheck As String, _
ByVal domain As String, _
ByVal ADlogin As String, _
ByVal ADpassword As String) _
As Boolean
Dim myDE As DirectoryEntry
Dim EntryString As String
Dim NumberOfGroups As Integer
Dim tempString As String
'Checks to see if the specified user is a member of the specified group
Try
'Setup the LDAP basic entry string.
EntryString = "LDAP://" & domain
'Make the group to check all lowercase (for matching)
grouptoCheck = grouptoCheck.ToLower()
'Use the correct overloaded function of DirectoryEntry
If (ADlogin <> "" AndAlso ADpassword <> "") Then
myDE = New DirectoryEntry(EntryString, ADlogin, ADpassword)
Else
myDE = New DirectoryEntry(EntryString)
End If
'Filter the directory searcher and get the group names
Dim myDirectorySearcher As New DirectorySearcher(myDE)
myDirectorySearcher.Filter = "sAMAccountName=" & username
myDirectorySearcher.PropertiesToLoad.Add("MemberOf")
Dim myresult As SearchResult = myDirectorySearcher.FindOne()
'Get the number of groups, so they can be itereated
NumberOfGroups = myresult.Properties("memberOf").Count() - 1
While (NumberOfGroups >= 0)
'Extract the group name from the result set of the index
tempString = myresult.Properties("MemberOf").Item(NumberOfGroups)
tempString = tempString.Substring(0, tempString.IndexOf(",", 0))
tempString = tempString.Replace("CN=", "")
tempString = tempString.ToLower()
tempString = tempString.Trim()
If (grouptoCheck = tempString) Then 'We got a winner
Return True
End If
NumberOfGroups = NumberOfGroups - 1
End While
Return False 'User is not in the specified group
Catch ex As Exception
Check_If_Member_Of_AD_Group = False 'If all else fails, don't authenticate
End Try
End Function