2012-07-19 62 views
1

我试图通过ASP.NET通用处理程序(.ashx)获取currely登录用户的用户名。但是,用户名是始终没有即使用户当前已登录获取用户名和ASP.NET通用Hanlder

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

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。谢谢。

+2

看起来像一个重复:http://stackoverflow.com/questions/9524466/httpcontext-current-user-identity-name-not-working-in-ashx-hander – Vedran 2012-07-19 09:48:24

回答

0

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