【问题标题】:Get Username with ASP.NET Generic Hanlder使用 ASP.NET 通用处理程序获取用户名
【发布时间】:2012-07-18 11:22:55
【问题描述】:

我正在尝试通过 ASP.NET 通用处理程序 (.ashx) 获取当前登录用户的用户名。但是即使用户当前已登录,用户名也始终是空的。

这是我的通用处理程序类:

Imports System.Web.SessionState

    Public Class FileUploadHandler
    Implements System.Web.IHttpHandler
    Implements IRequiresSessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        'username is always null, i'm not sure why there's nothing eventhough the user is logged in     
        Dim username = context.Current.User.Identity.Name

        If String.IsNullOrEmpty(username) Then
            context.Response.StatusCode = 0
        Else
            context.Response.StatusCode = 1

        End If

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

但我可以从这样的任何页面获取登录用户的用户名:

Dim username = Context.Current.User.Identity.Name

您认为这里有什么问题吗? 我对 C# 和 VB.NET 都很好。谢谢。

【问题讨论】:

标签: c# asp.net vb.net generic-handler


【解决方案1】:

如果您正在使用任何客户端组件,例如 UploadifyPlupload,则可能是该组件未随请求发送身份验证和会话 cookie。 workaround 这里有一个很好的解释。

查看Uploadify (Session and authentication) with ASP.NET

【讨论】:

    【解决方案2】:

    构建的默认 ASP.NET 应用程序使用 cookie 通过写入 cookie 本身的 session-id 对用户进行身份验证,而我认为应该是会话对象来处理这个问题。因此,默认创建的 MVC 应用程序并不安全。您最好将源代码更改为使用会话。 对于您的问题,从用户类获取用户名可能不是一个好的选择,因为它将返回一个空字符串。如果您使用会话,则在使用会话对象中的标识后,此问题将得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-30
      • 2013-06-12
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2011-06-20
      相关资源
      最近更新 更多