这应该不是User.Identity.Name造成的,你可以实际用两个用户调试一下,看看各自的User.Identity.Name是不是有错误。建议如果还是维持表单验证登陆,然后注册该用户的session信息。当添加部门的时候使用该Session.

解决方案 »

  1.   

    Code the Event Handler So That It Validates the User Credentials
    This section presents the code that is placed in the code-behind page (Logon.aspx.vb). 
    Open the Logon.aspx.vb file. 
    Import the required namespaces in the code-behind file:Imports System.Data.SqlClient
    Imports System.Web.Security

    Create a ValidateUser function to validate the user credentials by looking in the database. (Make sure that you change the Connection string to point to your database.)Private Function ValidateUser(ByVal userName As String, ByVal passWord As String) As Boolean
            Dim conn As SqlConnection
            Dim cmd As SqlCommand
            Dim lookupPassword As String        lookupPassword = Nothing        ' Check for an invalid userName.
            ' userName  must not be set to nothing and must be between one and 15 characters.
            If ((userName Is Nothing)) Then
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.")
                Return False
            End If
            If ((userName.Length = 0) Or (userName.Length > 15)) Then
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.")
                Return False
            End If        ' Check for invalid passWord.
            ' passWord must not be set to nothing and must be between one and 25 characters.
            If (passWord Is Nothing) Then
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")
                Return False
            End If
            If ((passWord.Length = 0) Or (passWord.Length > 25)) Then
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")
                Return False
            End If        Try
                ' Consult with your SQL Server administrator for an appropriate connection
                ' string to use to connect to your local SQL Server.
                conn = New SqlConnection("server=localhost;Integrated Security=SSPI;database=pubs")
                conn.Open()            ' Create SqlCommand to select pwd field from the users table given a supplied userName.
                cmd = New SqlCommand("Select pwd from users where uname=@userName", conn)
                cmd.Parameters.Add("@userName", SqlDbType.VarChar, 25)
                cmd.Parameters("@userName").Value = userName
                ' Execute command and fetch pwd field into lookupPassword string.
                lookupPassword = cmd.ExecuteScalar()            ' Cleanup command and connection objects.
                cmd.Dispose()
                conn.Dispose()
            Catch ex As Exception
                ' Add error handling here for debugging.
                ' This error message should not be sent back to the caller.
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " & ex.Message)
            End Try        ' If no password found, return false.
            If (lookupPassword Is Nothing) Then
                ' You could write failed login attempts here to the event log for additional security.
                Return False
            End If        ' Compare lookupPassword and input passWord by using a case-sensitive comparison.
            Return (String.Compare(lookupPassword, passWord, False) = 0)End Function

      

  2.   

    Private Sub cmdLogin_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) _
       Handles cmdLogin.ServerClick
       If ValidateUser(txtUserName.Value,txtUserPass.value) Then
          FormsAuthentication.RedirectFromLoginPage(txtUserName.Value, _
          chkPersistCookie.Checked)
       Else
          Response.Redirect("logon.aspx", True)
       End If
    End Sub
      

  3.   

    User.Identity.Name不会造成这样的情况吗?那这个方法的原理是什么?
      

  4.   

    User.Identity.Name 获取用户名称 — 当前进程正代表该用户而运行
    完全限定对当前活动的请求/响应上下文以及要使用的 System.Web 中的类的引用
    根据当前的HttpContext得到用户的名称,所以应该不会是它造成的问题
      

  5.   

    User.Identity.Name是取得当前用户登录名,等于数据库表中的Username