views:

30

answers:

2

One reason we currently use UpdatePanels for doing our AJAX is because our BL and DA layers pass around the Page.User.Identity for authentication.

Is there a way to access this?

+2  A: 

Yes, you can get the current user via HttpContext.Current.User. From the MSDN documentation for Page.User:

This property uses the HttpContext object's User property to determine where the request originates.

As for your broader question, "How can I use jQuery Ajax and PageMethods with instance variables?" The answer is "not directly."

No instance of your page is created when executing a page method. (Why do ASP.NET AJAX page methods have to be static? is a great conceptual overview of the differences between normal page operations and static page methods). The only way to access instance variables in page methods is to first put the variables into Session during the initial page request - but this is a rather fragile strategy: you're better off figuring out another way to get the data or values in question.

Jeff Sternal
A: 

I agree with Jeff Sternal's answer to this post. On my current project, we frequently use the session as a "scratch pad" to store data for later use by PageMethods and ASMX webservices.

However, if you don't like using session in that fashion, here is another approach that should be a viable alternative:

At page-creation time, you can put instance variable values into javascript vars or hidden fields. From there, they can easily be accessed by javascript/jquery and included as params on calls to webservices. You could then code your webservices (PageMethods, ASMX services or others) to take those values as parameters.

mikemanne
is that safe???
Homer
Ah, good point. I was thinking of general ways to get data from an ASPX instance to a PageMethod or ASMX webservice. But if the data is sensitive, then the session is definitely far safer than putting the data on the page itself.
mikemanne