我正在通过Visual Studio 2013构建一个网站,我希望在页面加载时获取该用户名,然后显示它。
我已尝试过来自其他网站的一些代码,但这只是提出了Windows身份验证,要求用户登录,但我想避免这种情况。所以我想要的是一个函数在页面加载之前获取用户名,然后自动显示在屏幕上。
我使用的代码..
Public Shared Function GetUserName() As String
If TypeOf My.User.CurrentPrincipal Is
System.Security.Principal.WindowsPrincipal Then
Dim parts() As String = Split(My.User.Name, "\")
Dim username As String = parts(1)
Return username
Else
Return LCase(HttpContext.Current.User.Identity.Name.ToString())
End If
End Function
你能帮我解决一下如何在我的aspx.vb页面上调用该函数。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetUserName()
End Sub
谢谢
答案 0 :(得分:0)
您需要配置IIS以启用集成安全性并禁用匿名登录(这是提示您输入usernam的一个原因)
完成后,像这样的代码应该为您提供用户名:
If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then
Return System.Web.HttpContext.Current.User.Identity.Name
Else
Return "Unknown"
End If