p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 4 General Discussion (http://p2p.wrox.com/forumdisplay.php?f=561)
-   -   store logged in user id in database from class file (http://p2p.wrox.com/showthread.php?t=86806)

sophia February 26th, 2012 12:52 PM

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...

irProject February 28th, 2012 12:06 PM

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?

sophia February 28th, 2012 02:02 PM

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

irProject February 28th, 2012 08:06 PM

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

sophia February 29th, 2012 05:34 AM

can you please tell me where I should update my code
Thanks

sophia March 2nd, 2012 12:41 PM

Hi Imar,
kindly look here, can you please spot out the mistake.

sophia March 3rd, 2012 01:15 PM

Kindly any one please tell me how this can be overcome?
I am struggling with this problem for many days.

Pleas help.
Thanks

irProject March 4th, 2012 12:56 AM

I'll send it 4 hours later. I'm busy now ...

sophia March 6th, 2012 01:59 AM

Hi, are you still busy?

sophia March 10th, 2012 07:46 AM

OK done....
I passed id through querystring.


All times are GMT -4. The time now is 05:10 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.