2012-07-19 61 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應用程序並不安全。您最好將源代碼改爲使用會話。 對於你的問題,從用戶類獲取用戶名可能不是一個好的選擇,因爲它會返回一個空字符串。如果您使用會話,則使用會話對象的身份後,此問題將得到解決。