Wherever I look this is the correct way to add a Cookie:
HttpCookie Session = new HttpCookie("Session");
Session.Value = someguid;
Session.Expires = somedatetime;
Response.Cookies.Add(Session);
And wherever there is no explanation as to how to get Response into the current context. So I get this error:
The name 'Response' does not exist in the current context
What do I need to do to get the above code to work?
You may want to use System.Web.HttpContext.Current.Response if you sure that this code will be executed inside web application.
But I have life-proven practice (and Law of Demeter says the same) to pass Response as a parameter to a method where I want to use it.
Related
I am using SWFAddress in actionscript 3 to control urls for navigation and controls, and while I am able to target and change specific parameters, I feel like I am missing a cleaner and more consistent way of handling it, perhaps even a feature or method I am not aware of.
Say I have a url and I want to change just the second param of def to xyz.
http://localhost/some-page/#/?param1=abc¶m2=def¶m3=ghi changed to
http://localhost/some-page/#/?param1=abc¶m2=xyz¶m3=ghi
I currently am doing:
if (SWFAddress.getParameterNames().indexOf("param2") >= 0) {
SWFAddress.setValue(SWFAddress.getPath() + "?"
+ SWFAddress.getQueryString().replace("param2=" + SWFAddress.getParameter("param2"), "param2=xyz"))
Essentially, checking if the param exists, checking what its current value is, then recreating the whole url using base, '?", and query, making sure I replace the the parameter and the parameter's value, making sure I don't miss the equal sign. This get's sloppy, and is error prone if the param exists but is not set to anything, or whether or not there is an equal sign, and a host of other pitfalls.
So, I can not just tell SWFAddress to update that one parameter in the url? A theoretical function of SWFAddress.setParam("param2, "xyz").
Has anyone coded their own method to micro-manipulate SWFAddress and the url, beyond the single function they supply you with of setValue(val:String)?
I think the short answer is no. According to the documentation there is no setParameter to go with the getParameter method. Looking at the code, it seems that the URL is not cached as a property in the class and therefore cannot be manipulated other than via the setValue method which, of course, updates the URL in the browser.
Presumably you're already parsing the URL in your onChange event so you can use the values to set your application state? If so, you shouldn't need to do so again when you come to rebuild the URL prior to updating it from Flash. If you store the deep-link properties on a Model class somewhere you can handle the defaulting, updating, and error checking without needing to resort to String manipulation. You would then rebuild the URL using those properties, a process you could abstract into a method in your Model class if required.
You should also note that the following line is not particularly robust since it will return true for properties such as param22 and sparam2:
if (SWFAddress.getParameterNames().indexOf("param2") >= 0) { }
I am using valueUnbound method of HttpSessionBindingListener to release lock(an entry from the database), before session is about to expire:
#Override
public void valueUnbound(HttpSessionBindingEvent event) {
String user = (String) event.getSession().getAttribute("currentUsr");
removeLock(user);
}
When the lock is set, I am setting up the username as a session variable.
I need this "username" in my remove lock method. But the getAttribute is throwing an exception:
java.lang.IllegalStateException: getAttribute: Session already invalidated
I need help in getting the session variable?? or is there any other way to get the username?
No, since session has been invalidated.
Although, I figured out the solution, I am setting the attribute via servlet context in
valueBound method and getting it through the : event.getSession().getServletContext().getAttribute("cUser");
it works fine. Thank You EJP
I got your point EJP, you are right , I am making it complex, I can get it from event.getValue() . +1 to your answer, Thank You.
Although, I figured out the solution, I am setting the attribute via servlet context in valueBound method and getting it through the : event.getSession().getServletContext().getAttribute("cUser");
So.. You are storing session scoped data in the application scope. Do you realize that this way the data is shared among all visitors of the webapp? Visitor X would then see the attribute set by visitor Y which has visited the website at a later moment. It makes the problem only worse.
Anyway, as to the concrete problem, as the exception message is trying to tell you, the session has already been invalidated at that point. There are two ways to solve this:
Make currentUsr a property of the class which is implementing HttpSessionBindingListener, so that you don't need to grab it as a distinct session attribute.
Use a HttpSessionListener instead. The sessionDestroyed() method is called right before invalidation, so you should still have access to all attributes.
I tried to do the following:
20 session[:atoken] = linked_in_data['extra']['access_token'].token
21 session[:asecret] = linked_in_data['extra']['access_token'].secre
t
This is within a method inside of a User model.
But I get an error saying undefined method for session...why? Can session variables only be set in a controller?
It's a bad practice, but if you must do it :
http://m.onkey.org/how-to-access-session-cookies-params-request-in-model
But, finding a workaround is always better. Take a look at that as well :
http://media.railscasts.com/videos/119_session_based_model.mov
The session can't be accessed in the models. It would break somehow the MVC structure of the app. If you want to change the session values during save, update etc..., you can try to use sweeper.See the api. You can access the model's attributes, and the session as well, and you can observe changes of the object.
I've put some code like this
flash[:task_loader] = flash[:task_loader]
flash[:task_loader_params] = flash[:task_loader_params]
in a function in my controller which all my actions can call. This has the effect of keeping those two flash entries in the FlashHash. (I presume this rather odd-looking code works because the '=' does more than just assign values.)
My question is, is there a better way to do this? Something like
flash[:task_loader].pin
Flash is a convenient wrapper for storing data in cookies and expiring them in the next request. So that your notification messages will work through 2 (or multiple) request response cycles.
If you want some more persistence, you can use session:
session[:task_loader] = my_task_loader
Note that one cookie can hold only 4KB of data.
(I presume odd-looking code works
because the '=' does more than just
assign values.)
This is because it is not simply an assignment, but a method []=, with a signature similar to this:
def []=(k, v)
I need to detect if a request cookie - value is different from a response cookie - value.
Its not as easy as:
if(cookiesArePresent)
{
bool isDifferent = HttpContext.Current.Response.Cookies[".ASPXANONYMOUS"].value == HttpContext.Current.Response.Cookies[".ASPXANONYMOUS"].value;
}
But I read that changing the Response.Cookies changes the Request.Cookies. That would mean they are always the same if HttpContext.Current.Response.Cookies[".ASPXANONYMOUS"] was changed. Is there an easy way around this?
http://chance.lindseydev.com/2009/04/aspnet-httprequestcookies-and.html
If you use Reflector to examing System.Web.Security.AnonymousIdentificationModule, you can see that the anonymous cookie is only read/written in PostAuthenticateRequest.
So, if you suspect something is wonky, write a simple HttpModule that reads the cookie during AuthenticateRequest and compare it to the value you get in AuthorizeRequest.
For more information about the request lifecycle see Exploring Web.config - system.web/httpModules by yours truly.