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?
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.
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:
Sincerely,TroyDeveloper Support Engineerhttp://www.infragistics.com/support
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:
"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."
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.
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.
Thanks for your detailed response. I was able to solve this issue by using your first suggestion.
Another approach is to use ASP.NET machinery to decrypt the cookie : http://ingeniarius.net/blog/infragistics-ignite-ui-upload-httpcontext-user-is-null/
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