 |
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

February 26th, 2012, 12:52 PM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
store logged in user id in database from class file
Hello all,
I am storing the user id for a record who creates it. It is working fine in aspx.cs file.
myReview.Owner = (Guid)Membership.GetUser().ProviderUserKey;
but when I use the same code in a class file in App_Code folder then it does not work and code breaks.
Is there any other way to get user id in case of class file?
Please help me I am stuck on it for a long time.
Many thanks...
|

February 28th, 2012, 12:06 PM
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
Hi sophia
what error do you receive with App_Code?
as you know Membership Provider settings are stored in web.config file. so maybe you have two web.config file in your program (in sub-directories) with different Membership settings. but I'm not sure about what I said!!
are you getting error or it does not return the right result?
Are you using Windows Authentication?
__________________
happy every time, happy every where
Reza Baiat
|

February 28th, 2012, 02:02 PM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
HI I am trying to upload images using httphandler.
for this I have few settings in web.config as below.
<httpHandlers>
<remove verb="POST,GET" path="Upload.axd" />
<add verb="POST,GET" path="Upload.axd" type="Upload" />
</httpHandlers>
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)
public class Upload : IHttpHandler, IRequiresSessionState
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
using (ProjectEntities myEntities = new ProjectEntities())
{
MyFile myFile;
string virtualFolder = "~/Images/";
//string uploadPath = context.Server.MapPath(context.Request.Application Path + virtualFolder);
string uploadPath = context.Server.MapPath(virtualFolder);
// loop through all the uploaded files
for(int j = 0; j < context.Request.Files.Count; j++)
{
// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];
// if there was a file uploded
if (uploadFile.ContentLength > 0)
{
string fileName = Guid.NewGuid().ToString();
string extension = Path.GetExtension(uploadFile.FileName);
myFile = new MyFile();
myFile.Name = uploadFile.FileName;
myFile.Url = virtualFolder + fileName + extension;
myFile.Owner = (Guid)Membership.GetUser().ProviderUserKey;
//myFile.Owner = (Guid)Membership.GetUser(HttpContext.Current.User. Identity.Name).ProviderUserKey;
myEntities.AddToMyFile(myFile);
myEntities.SaveChanges();
uploadFile.SaveAs(Path.Combine(uploadPath, fileName + extension));}
}
}
}
}
The code breaks at when storing logged in user's id. Please help me.
Thanks
|

February 28th, 2012, 08:06 PM
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
Hi sophia
I think the problem maybe with Handler-Call-Time. when your handler is called?
Code:
public void Init(HttpApplication context)
{
context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest);
}
void context_PostAuthorizeRequest(object sender, EventArgs e)
{
throw new NotImplementedException();
}
void context_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication context = (HttpApplication)sender;
Rewrite(context);
}
these are two different calls: you may need to call your handler in PostAuthorizeRequest event of HttpContext object
__________________
happy every time, happy every where
Reza Baiat
|

February 29th, 2012, 05:34 AM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
can you please tell me where I should update my code
Thanks
|

March 2nd, 2012, 12:41 PM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
Hi Imar,
kindly look here, can you please spot out the mistake.
|

March 3rd, 2012, 01:15 PM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
Kindly any one please tell me how this can be overcome?
I am struggling with this problem for many days.
Pleas help.
Thanks
|

March 4th, 2012, 12:56 AM
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
I'll send it 4 hours later. I'm busy now ...
__________________
happy every time, happy every where
Reza Baiat
|

March 6th, 2012, 01:59 AM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
Hi, are you still busy?
|

March 10th, 2012, 07:46 AM
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
OK done....
I passed id through querystring.
|
|
 |