我正在尝试使用ASP.NET和VB.NET在我们公司的Intranet上构建一个应用程序.
一旦我的应用程序发布到IIS,这些函数都不会返回任何内容.它们在开发中工作正常(即:按F5我得到我的常规网络用户名),但是一旦发布它们就会返回''(空字符串).
HttpContext.Current.User.Identity.Name
Page.User.Identity.Name
Run Code Online (Sandbox Code Playgroud)
我正在寻找能够获取当前用户登录名的东西 - 任何东西.请注意,由于其他功能要求,我无法在web.config中更改这些设置.
<authentication mode="Windows"/>
<roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="AspNetWindowsTokenRoleProvider" cookieName=".ASPXROLES" cookiePath="/" cookieTimeout="480" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieProtection="All" />
Run Code Online (Sandbox Code Playgroud)
我也无法更改任何IIS设置,包括"启用匿名用户"设置.规格是一成不变的,我必须砍掉我自己的腿(或头部)来改变它们.
我认为必须有一种方法来获取当前使用我当前配置登录的用户名.
有任何想法吗?
谢谢,
贾森
在IIS中禁用匿名身份验证。
User.Identity.Name如果在IIS中启用了匿名身份验证,则该字段可能为空。
在web.config中设置
<configuration>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
使用User.Identity.Name来获取登录用户。
Environment.UserName是正在运行的线程标识。如果您按照Mark的说明启用了模拟,则可以发现返回的结果将有所不同。但是,这需要ASP.NET Imperionation。如果您不需要ASP.NET模拟和处理线程标识,则可以忽略Environment.UserName是否使用,而只需使用User.Identity.Name.
在执行任何操作之前,还请检查。
if (User.Identity.IsAuthenticated)
{
Page.Title = "Home page for " + User.Identity.Name;
}
else
{
Page.Title = "Home page for guest user.";
}
Run Code Online (Sandbox Code Playgroud)
这是一个很好的例子
小智 2
这是我(在某处)找到并最终使用的。希望它可以帮助其他人!
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50398 次 |
| 最近记录: |