igUpload AddFinishingUploadEventHandler - User is null in event

Answered (Verified) This post has 1 verified answer | 8 Replies | 4 Followers Thread's RSS feed.

Paul
Points 1,895
Replied On: Wed, May 15 2013 10:23 AM Reply

Hi,

I attached a FinishingUploadEventHandler on my controller.  In that event, I plan to use the Username to rename the file.  HttpContext.Current.User is null when this method is called.  I'm using Windows Forms Authentication, and there is a valid user logged in.  The User is populated correctly with all other server calls I do.  What's the best way to get the username in the FinishingUploadEventHandler event?

Thanks,

Paul

  • Post Points: 35

Verified Answer

Answered (Verified) Replied On: Tue, May 21 2013 6:47 AM Reply
Verified by Paul

Hello Paul,

It's not very straightforward to use the user name to rename the file. That's because the igUpload is using its own HTTP module which is not part of the MVC pipeline, thus there is no session and user information when the FinishingUpload event handler is called.

Let me explain how can you overcome this problem. The igUpload uses 2 mechanisms to upload the file based on the browser capabilities.

The first one is using the updated XMLHttpRequest object in the HTML 5 specification, so its available to the latest browser versions. In this scenario you should create global static dictionary which will contain the session id for a key and the username for the value. When Index action method (or some other action) is executed you'll add item in this dictionary for the newly logged user (To get the session id use the following code: System.Web.HttpContext.Current.Session.SessionID). Later on when the FinishingUpload handler is executed you'll get the session id from the request using this code:

string sessionId = System.Web.HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;

Then use the sessionId key to find the corresponding user name.

The second mechanism targets older browsers and is using one FORM element per file. You can change the form action attribute and add the user name as a query string with this code:

var action = $("#upload1_0__frm").attr("action");

$("#upload1_0__frm").attr("action", action + "&username=exampleuser");

Where "upload1" is the id of the igUpload, and 0 is the unique id of the form (starting from 0).

Then on the server side you will read the query string information using the following code: System.Web.HttpContext.Current.Request.QueryString["username"]

P.S.: The statement in the topic which you refer to: http://www.infragistics.com/help/topic/87D0D638-77C4-4B2D-A5F6-52312DAAF606 is misleading and we probably will change it.

 

Hope this helps,

Martin Pavlov
Infragistics, Inc.

  • Post Points: 40

All Replies

Replied On: Wed, May 15 2013 2:54 PM Reply

Hi Paul D,

I have looked at this issue and have not yet found away that this can be accomplished. For details on this issue, I will direct you to the following forum post that addresses this issue:

http://www.infragistics.com/community/forums/t/67062.aspx

 

Sincerely,
Troy
Developer Support Engineer
http://www.infragistics.com/support

  • Post Points: 20
Paul
Points 1,895
Replied On: Wed, May 15 2013 3:08 PM Reply

Thanks for the explanation.

I was just reading another Infragistics Help Topic and it seems to suggest that putting username in the file name is possible (and recommended); however, there are no instructions on how to do so:

http://www.infragistics.com/help/topic/87D0D638-77C4-4B2D-A5F6-52312DAAF606

"Warning:The FinishingUpload handler overwrites files with the same name! In a real-world scenario, you usually do not want overwrite files, so you should employ a user name or some other identifier to ensure that files do not overwrite each other, but, of course, that depends on the business scenario which you are implementing."

  • Post Points: 35
Replied On: Thu, May 16 2013 2:07 PM Reply

Hi Paul,

I am looking further into this topic and am consulting with the engineers for this control. I will have an update for you on this on or about this Monday upcoming.

 

Sincerely,
Troy
Developer Support Engineer
http://www.infragistics.com/support

  • Post Points: 5
Replied On: Mon, May 20 2013 2:58 PM Reply

Hi Paul,

I want to update you that I am working with our engineers on this and we are looking into how this can be accomplished. I will update you on this thread once I some new information on this objective.

 

Sincerely,
Troy
Developer Support Engineer
http://www.infragistics.com/support

  • Post Points: 5
Answered (Verified) Replied On: Tue, May 21 2013 6:47 AM Reply
Verified by Paul

Hello Paul,

It's not very straightforward to use the user name to rename the file. That's because the igUpload is using its own HTTP module which is not part of the MVC pipeline, thus there is no session and user information when the FinishingUpload event handler is called.

Let me explain how can you overcome this problem. The igUpload uses 2 mechanisms to upload the file based on the browser capabilities.

The first one is using the updated XMLHttpRequest object in the HTML 5 specification, so its available to the latest browser versions. In this scenario you should create global static dictionary which will contain the session id for a key and the username for the value. When Index action method (or some other action) is executed you'll add item in this dictionary for the newly logged user (To get the session id use the following code: System.Web.HttpContext.Current.Session.SessionID). Later on when the FinishingUpload handler is executed you'll get the session id from the request using this code:

string sessionId = System.Web.HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;

Then use the sessionId key to find the corresponding user name.

The second mechanism targets older browsers and is using one FORM element per file. You can change the form action attribute and add the user name as a query string with this code:

var action = $("#upload1_0__frm").attr("action");

$("#upload1_0__frm").attr("action", action + "&username=exampleuser");

Where "upload1" is the id of the igUpload, and 0 is the unique id of the form (starting from 0).

Then on the server side you will read the query string information using the following code: System.Web.HttpContext.Current.Request.QueryString["username"]

P.S.: The statement in the topic which you refer to: http://www.infragistics.com/help/topic/87D0D638-77C4-4B2D-A5F6-52312DAAF606 is misleading and we probably will change it.

 

Hope this helps,

Martin Pavlov
Infragistics, Inc.

  • Post Points: 40
Paul
Points 1,895
Replied On: Wed, May 22 2013 12:16 PM Reply

Hi Martin,

Thanks for your detailed response. I was able to solve this issue by using your first suggestion.

Thanks,

Paul

  • Post Points: 5
Ognyan Dimitrov
Points 135
Replied On: Thu, Apr 3 2014 3:54 AM Reply

Another approach is to use ASP.NET machinery to decrypt the cookie :
http://ingeniarius.net/blog/infragistics-ignite-ui-upload-httpcontext-user-is-null/

  • Post Points: 20
Paul
Points 1,895
Replied On: Thu, Apr 3 2014 10:24 AM Reply

Thanks for the link, we actually found an easier way to do this.  Request.ServerVariables["LOGON_USER"] is populated with the username.  In debug, either controller.Request.ServerVariables["AUTH_USER"] is populated or User.Identity.Name.  Here is the code we use:

string userName = (controller.Request.ServerVariables.HasKeys()) ? controller.Request.ServerVariables["LOGON_USER"] : controller.User.Identity.Name;
#if DEBUG
    userName = (controller.Request.ServerVariables.HasKeys()) ? controller.Request.ServerVariables["AUTH_USER"] : controller.User.Identity.Name;
#endif

  • Post Points: 5
Page 1 of 1 (9 items) | RSS