store logged in user id in database from class filehttp://forums.asp.net/t/1773528.aspx/1?store+logged+in+user+id+in+database+from+class+fileSat, 17 Mar 2012 12:13:57 -040017735284849774http://forums.asp.net/p/1773528/4849774.aspx/1?store+logged+in+user+id+in+database+from+class+filestore logged in user id in database from class file <p>Hello all,</p> <p>I am storing the user id for a record who creates it. It is working fine in aspx.cs file.</p> <p>myReview.Owner = (Guid)Membership.GetUser().ProviderUserKey;</p> <p>but when I use the same code in a class file in App_Code folder then it does not work and code breaks.&nbsp;</p> <p>Is there any other way to get user id in case of class file?</p> <p>Please help me I am stuck on it for a long time.</p> <p>Many thanks...</p> 2012-02-24T16:51:09-05:004849846http://forums.asp.net/p/1773528/4849846.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>Try this.</p> <p>use Context.user instead of Membership.GetUser() .</p> 2012-02-24T17:39:44-05:004849944http://forums.asp.net/p/1773528/4849944.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Shellymn</h4> <p></p> <p>Try this.</p> <p>use Context.user instead of Membership.GetUser() .</p> <p></p> </blockquote> <p></p> <p>HI Shelly,</p> <p>I did not get any reference to Context.User</p> <p>However I used&nbsp;HttpContext.Current.User.Identity.Name).ProviderUserKey;</p> <p>but it is also not working...</p> 2012-02-24T18:51:15-05:004850439http://forums.asp.net/p/1773528/4850439.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>Membership.GetUser() is returning the surrently logged in <strong>User</strong>.</p> <p>HttpContext.Current.User.Identity.Name is returning the <strong>Username</strong> of the currently logged in user</p> <p>So try Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey</p> 2012-02-25T09:11:43-05:004850610http://forums.asp.net/p/1773528/4850610.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>Hi Hans,&nbsp;</p> <p>it is also not working.&nbsp;<strong>Object reference not set to an instance of an object.&nbsp;</strong>error message is displayed. when I debugged the code and copied the details then it gives the following message.</p> <p>System.NullReferenceException was unhandled by user code<br> Message=Object reference not set to an instance of an object.<br> Source=App_Code.wp83lrto<br> StackTrace:<br> at Upload.ProcessRequest(HttpContext context) in e:\Projects\Web Site Projects\Project\App_Code\Upload.cs:line 127<br> at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()<br> at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)<br> InnerException:</p> <p></p> <p>Please help me.</p> <p>Thanks</p> 2012-02-25T12:34:30-05:004854723http://forums.asp.net/p/1773528/4854723.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>Is HttpContext.Current.User.Identity.Name actually returning what you are expecting?&nbsp; Can you give much more detail on how you are doing this (for example, are you using AspNetMembershipProvider?).&nbsp; Maybe you can go through this walkthrough and let us know if you have issues:</p> <p><a href="http://msdn.microsoft.com/en-us/library/879kf95c.aspx">http://msdn.microsoft.com/en-us/library/879kf95c.aspx</a></p> <p>HTH</p> 2012-02-28T11:12:41-05:004855258http://forums.asp.net/p/1773528/4855258.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>HI I am trying to upload images using httphandler.</p> <p>for this I have few settings in web.config as below.</p> <p>&lt;httpHandlers&gt;<br> &lt;remove verb=&quot;POST,GET&quot; path=&quot;Upload.axd&quot; /&gt;<br> &lt;add verb=&quot;POST,GET&quot; path=&quot;Upload.axd&quot; type=&quot;Upload&quot; /&gt;<br> &lt;/httpHandlers&gt;</p> <p>in aspx page I am having a control to upload files, which in turn transfers the control to the Upload.cs class in App_code folder, where code is as follows (I am logged in at the time of uploading files)</p> <p></p> <p>public class Upload : IHttpHandler, IRequiresSessionState<br> { <br> public Upload()<br> {<br> <br> }</p> <p>#region IHttpHandler Members</p> <p>public bool IsReusable<br> {<br> get { return true; }<br> }</p> <p>public void ProcessRequest(HttpContext context)<br> {<br> using (ProjectEntities myEntities = new ProjectEntities())<br> {<br> MyFile myFile;<br> string virtualFolder = &quot;~/Images/&quot;;<br> //string uploadPath = context.Server.MapPath(context.Request.ApplicationPath &#43; virtualFolder);<br> string uploadPath = context.Server.MapPath(virtualFolder);<br> // loop through all the uploaded files<br> for(int j = 0; j &lt; context.Request.Files.Count; j&#43;&#43;)<br> {<br> // get the current file<br> HttpPostedFile uploadFile = context.Request.Files[j];<br> // if there was a file uploded<br> if (uploadFile.ContentLength &gt; 0)<br> {</p> <p>string fileName = Guid.NewGuid().ToString();</p> <p>string extension = Path.GetExtension(uploadFile.FileName);<br> <br> myFile = new MyFile();<br> myFile.Name = uploadFile.FileName;<br> myFile.Url = virtualFolder &#43; fileName &#43; extension;<br> myFile.Owner = (Guid)Membership.GetUser().ProviderUserKey;&nbsp;</p> <p>//myFile.Owner = (Guid)Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;<br> myEntities.AddToMyFile(myFile);<br> myEntities.SaveChanges();</p> <p>uploadFile.SaveAs(Path.Combine(uploadPath, fileName &#43; extension));}&nbsp;</p> <p>}<br> <br> <br> }<br> }</p> <p>}</p> <p></p> <p>The code breaks at when storing logged in user's id. Please help me.&nbsp;</p> <p>Thanks</p> 2012-02-28T16:09:10-05:004862224http://forums.asp.net/p/1773528/4862224.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file Please guys help me, I am struggling with this error for a long time. Kindly suggest some solution Thanks 2012-03-03T16:52:29-05:004870021http://forums.asp.net/p/1773528/4870021.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>Can you verify that you are getting this value?&nbsp; Just have a page that does Response.Write(HttpContext.Current.User.Identity.Name);</p> <p>My best guess is that you don't have a value there - you are maybe using Anonymous authentication?&nbsp; You could also just add a good username in the web.config to see if it gets past the error:</p> <p>&lt;identity impersonate=&quot;true&quot; userName=&quot;good user&quot; password=&quot;password&quot; /&gt;</p> 2012-03-08T13:42:07-05:004872983http://forums.asp.net/p/1773528/4872983.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>HI cts,</p> <p>yes I was not getting the username there (I don't know why) but later I passed the userid directly through querystring, and then it is working.&nbsp;</p> <p>If you can point out why it was not getting username in initial code then it would be very helpful for me to point out the mistake.</p> <p>Many thanks</p> 2012-03-10T10:59:01-05:004885234http://forums.asp.net/p/1773528/4885234.aspx/1?Re+store+logged+in+user+id+in+database+from+class+fileRe: store logged in user id in database from class file <p>Perhaps you were using Anonymous Authentication?&nbsp; The HttpContext will be the process identity unless you are impersonating - in which case it will use the IIS authenticated user by default.&nbsp; So, if you were using anonymous authentication and impersonating, you would likely have nothing for your httpContext identity.</p> <p>See <a title="ASP .NET Impersonation" href="http://msdn.microsoft.com/en-us/library/aa292118(v=VS.71).aspx" target="_blank"> ASP.NET Impersonation</a>&nbsp;for more details.</p> 2012-03-17T12:13:57-04:00