I just noticed in my meteor app that the session variables are getting cleared on page refresh. So
How is meteor saving user login details ?
Are they not saved in session ?
How can this issue be managed ?
Meteor.Session is only for the client-side. It's a JavaScript global object in your application. If you refresh the page it's wiped out. Your session is stored in client-side localStorage, https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage. Meteor does not use cookies for Session, https://www.meteor.com/blog/2014/03/14/session-cookies.
You would need to explain more what you are trying to accomplish. I use Meteor.Session once the page is loaded, and not for many things, but to get my initial state of things, my URLs contain enough info to set initial state.
With Meteor you ideally do not want to refresh the browser. Everything happens or should happen with AJAX and HTML5 push state ideally and Meteor's reactiveness.
You should read their documentation. Here's the section on Session, http://docs.meteor.com/#session
This is an old question, but since I found myself needing the same thing, here's how I did it. I 'extended' the session to actually store it on the localstorage when setting a value, and loading up the localstorage into the session when the page loads.
// improving the session package to persist it to the localstorage
Session._set = Session.set;
Session.set = function(key,value) {
Session._set(key,value);
localStorage.setItem(key,JSON.stringify(value));
};
// helper function
function isJSON(str) {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}
// loading the localstorate on load
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var value = localStorage.getItem(key);
Session._set(key,isJSON(value) ? JSON.parse(value) : value);
}
Related
The following example code successfully performs a callback to the Google App Script:
var SCRIPT_ID = "1eC5VsM2vkJXa9slM40MTKTlfARGAGyK1myMCU3AB_-Ox_jGxQaoPM8P2";
function getURL() { return getCallbackURL('testCallback'); }
function getCallbackURL(callback) {
var state = ScriptApp.newStateToken().withTimeout(3600).withMethod(callback).createToken();
return 'https://script.google.com/macros/d/'+SCRIPT_ID+'/usercallback?state='+state;
}
function doGet(e){ return HtmlService.createTemplate(" <div><p><a href='<?=getURL()?>' id='start-auth'><?=getURL()?></a></p></div>").evaluate()); }
function testCallback(e){
Logger.log('myVariable1= ' + e.parameter.myVariable1); // this doesn't work
Logger.log('myVariable2= ' + e.parameter.myVariable2); // this doesn't work
return HtmlService.createHtmlOutput('<b>Success. You can close this window. !</b>')
}
However I need to return a variable to the "testCallback" method as part of a HttpRepsonse redirect. I've tried settings a cookie and also setting a header variable in my asp.net C# application as follow:
HttpCookie cookie = new HttpCookie("myVariable1");
cookie.Value = "someValue1";
cookie.Expires = DateTime.Now.AddMinutes(1);
Response.Cookies.Add(cookie);
Response.AddHeader("myVariable2", "someValue2");
Response.Redirect(applicationCallbackUri, true);
but it's not clear whether the header or cookie variables are available to Google's callback method:
function testCallback(e){
Logger.log('myVariable1= ' + e.parameter.myVariable1);
Logger.log('myVariable2= ' + e.parameter.myVariable2);
return HtmlService.createHtmlOutput('<b>Success. You can close this window.
!</b>')
}
I've also tried using the .WithArguments method when creating the a new state token, but I'm not sure if its possible for my asp.net application to update the state object's arguments as part of the redirect/return.
I've also tried appending the variable to the Google Callback URL, e.g.
https://script.google.com/macros/d/1eC5VsM2vkJXa9slM40MTKTlfARGAGyK1myMCU3AB_-Ox_jGxQaoPM8P2?state=ADEpC8w0dL6mBVmDQHX3XcYcBP0JqQ5_etc&myVariable1=someValue1
However Google throws an "invalid state" error
The event object passed to your testCallback(e) function cannot reference HTTP headers or cookies and appending arbitrary url variables to the redirect url won't work either (the authorization server that does the redirect will omit them to ensure security).
The only valid way to send state vars is using the withArguments() method. However, your Logger.log() calls won't work b/c the calls (to the testCallback(e) function) are asynchronous and they are not tracked in the editor. Instead, try enabling StackDriver Logging from the Apps Script Editor's View menu and replace those Logger.log() calls with console.log(). Your logs should then show up under StackDriver Logging in your Google API Console for the project.
I'm developing a website with asp.net, visual studio 2012, IIS8, .Net Framework 4.
I use SESSIONS to store user information after login.
when user click sign in button, I send information to .ashx file like this:
var url = "SignIn.ashx?username=" + UserName.value + "&password=" + PassWord.value;
xmlRequest_SignIn.open("GET", url);
xmlRequest_SignIn.onreadystatechange = function () { ApplyUpdateSignIn() }
xmlRequest_SignIn.send(null);
SignIn.ashx:
... // check login information and then
HttpContext.Current.Session["UserName"] = username.toString();
...
Now in every page of my website, I check this session to authenticate the user in ajax:
var url = "CheckMemberAccount.ashx";
xmlRequest_member.open("GET", url);
xmlRequest_member.onreadystatechange = function () { ApplyUpdateGetMemberAccount() }
xmlRequest_member.send(null);
CheckMemberAccount.ashx:
if (Convert.ToString(HttpContext.Current.Session["UserName"]) == "" || null == HttpContext.Current.Session["UserName"])
{
// user not logged in
}
else
{
// user logged in
}
For log out I use ajax too:
var url = "SignOut.ashx";
xmlRequest_SignOut.open(GET, url);
xmlRequest_SignOut.onreadystatechange = function () { ApplyUpdateSignOut() }
xmlRequest_SignOut.send(null);
SignOut.ashx:
// I try everything to make the session null or invalid!
// BUT it doesn't take effect in Internet Explorer !!
HttpContext.Current.Session["UserName"] = "";
HttpContext.Current.Session["UserName"] = null;
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.RemoveAll();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value = "";
As you can see I've tried everything to make session null or invalid.
This code works well in all browsers(Chrome, FireFox, Safari, Opera, Netscape, ...) EXCEPT in Internet Explorer !
In IE after Signing out, when I check the member acount with CheckMemberAccount.ashx file, the session still remain valid with valid value as if no sign out happened !
This problem is only in IE !!
note that IE settings are all in their defaults.
I have tried Global.asax:
After adding the class, and without adding any code to it ( just keep it unchanged with it's default functions such as Session_Start() ), My problem changed and now I always get null for the session in CheckMemberAccount.ashx ! and again the problem is just in IE !
I didn't find any solution after 3 days.
I'll appreciate any help :)
I believe the checkmemberaccount call is being cached in the browser by iE. this has gotten me in trouble many times before. Add a random variable to your Ajax call and I believe your problem will be solved.
You can verify this by using a tool like fiddler. You should see that the browser is caching the GET request. You could also switch from a GET to a POST. POSTS are not cached by IE.
By the way... You should fix this for any get requests that you don't want to cache.
I have some asp.net pages that read and write cookie values. During the life cycle of a page it may update the cookie value and then need to read it again further in the code. What I've found is that it's not getting the latest value of the cookie until a page refresh. Is there a way around this? Here's the code I'm using to set and get the values.
public static string GetValue(SessionKey sessionKey)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiePrefix];
if (cookie == null)
return string.Empty;
return cookie[sessionKey.SessionKeyName] ?? string.Empty;
}
public static void SetValue(SessionKey sessionKey, string sessionValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiePrefix];
if (cookie == null)
cookie = new HttpCookie(cookiePrefix);
cookie.Values[sessionKey.SessionKeyName] = sessionValue;
cookie.Expires = DateTime.Now.AddHours(1);
HttpContext.Current.Response.Cookies.Set(cookie);
}
What you're missing is that when you update the cookie with SetValue you're writing to the Response.Cookies collection.
When you call GetValue you're reading from the Request.Cookies collection.
You need to store the transient information in a way that you access the current information, not just directly the request cookie.
One potential way to do this would be to writer a wrapper class that with rough psuedo code would be similar to
public CookieContainer(HttpContext context)
{
_bobValue = context.Request.Cookies["bob"];
}
public Value
{
get { return _bobValue; }
set {
_bobValue = value;
_context.Response.Cookies.Add(new Cookie("bob", value) { Expires = ? });
}
}
I ran into needing to do similar code just this week. The cookie handling model is very strange.
Start using Sessions to store your information, even if it's only temporary.
Cookies rely on a header being sent to the browser before the page has rendered. If you've already sent information to the client then proceed to set a cookie, you're going to see this "page refresh delay" you've described.
If it's necessary to have this value, use a session variable between the time you set the cookie and when you refresh the page. But, even then I would just recommend avoiding settings cookies so late in the processing step and try to set it as early as possible.
I have an ASP.NET Web Forms application. I want to have a button to post back to the server that will use my fields on my form (after validation) as parameters to a server process that will generate a document and stream it back to the browser. I want the form to be updated with some status results.
What is the best way to achieve this? Right now, I've got the button click generating the document and streaming it back to the browser (it's a Word document and the dialog pops up, and the Word document can be opened successfully) but the page doesn't get updated.
I have jQuery in my solution, so using js isn't an issue if that is required.
I have a very similar process on one of my servers, and the way I've handled it is to create a temporary document on the server rather than doing a live stream. It requires a bit of housekeeping code to tidy it up, but it does mean that you can return the results of the generation and then do a client-side redirect to the generated document if successful. In my case, I'm using jQuery and AJAX to do the document generation and page update, but the same principle should also apply to a pure WebForms approach.
This was way more difficult to do than I thought. The main issue is with opening a new browser window for a Word document. The window briefly flashes up, then closes - no Word document appears. It seems to be a security issue.
If i click a button on my page, I can stream the Word doc back as the response, and the browser dialog pops up allowing me to Open/Save/Cancel, but of course, my page doesn't refresh.
My final solution to this was to use a client script on the button click to temporarily set the form's target to _blank. This forces the response to the click on the postback to go to a new browser window (which automatically closes after the download dialog is dismissed):
<asp:Button Text="Generate Doc" runat="server" ID="btnGenerateDoc"
onclick="btnGenerateDoc_Click" OnClientClick="SetupPageRefresh()" />
My SetupPageRefresh function is as follows:
function SetupPageRefresh() {
// Force the button to open a new browser window.
form1.target = '_blank';
// Immediately reset the form's target back to this page, and setup a poll
// to the server to wait until the document has been generated.
setTimeout("OnTimeout();", 1);
}
Then my OnTimeout function resets the target for the form, then starts polling a web service to wait until the server process is complete. (I have a counter in my Session that I update once the process has completed.)
function OnTimeout() {
// Reset the form's target back to this page (from _blank).
form1.target = '_self';
// Poll for a change.
Poll();
}
And the Poll function simply uses jQuery's ajax function to poll my web service:
function Poll() {
var currentCount = $("#hidCount").val();
$.ajax({
url: "/WebService1.asmx/CheckCount",
data: JSON.stringify({ currentCount: currentCount }),
success: function (data) {
var changed = data.d;
if (changed) {
// Change recorded, so refresh the page.
window.location = window.location;
}
else {
// No change - check again in 1 second.
setTimeout("Poll();", 1000);
}
}
});
}
So this does a 1 second poll to my web service waiting for the Session's counter to change from the value in the hidden field on the page. This means it doesn't matter how long the server process takes to generate the Word document (and update the database, etc.) the page won't refresh until it's done.
When the web service call comes back with true, the page is refreshed with the window.location = window.location statement.
For completeness, my Web Service looks like this:
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : WebService
{
[WebMethod(EnableSession=true)]
public bool CheckCount(int currentCount)
{
if (Session["Count"] == null)
Session["Count"] = 0;
var count = (int)Session["Count"];
var changed = count != currentCount;
return changed;
}
}
Hopefully that helps somebody else!
I have an ASP.NET application that caches some business objects. When a new object is saved, I call remove on the key to clear the objects. The new list should be lazy loaded the next time a user requests the data.
Except there is a problem with different views of the cache in different clients.
Two users are browsing the site
A new object is saved by user 1 and the cache is removed
User 1 sees the up to date view of the data
User 2 is also using the site but does not for some reason see the new cached data after user 1 has saved a new object - they continue to see the old list
This is a shortened version of the code:
public static JobCollection JobList
{
get
{
if (HttpRuntime.Cache["JobList"] == null)
{
GetAndCacheJobList();
}
return (JobCollection)HttpRuntime.Cache["JobList"];
}
}
private static void GetAndCacheJobList()
{
using (DataContext context = new DataContext(ConnectionUtil.ConnectionString))
{
var query = from j in context.JobEntities
select j;
JobCollection c = new JobCollection();
foreach (JobEntity i in query)
{
Job newJob = new Job();
....
c.Add(newJob);
}
HttpRuntime.Cache.Insert("JobList", c, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}
}
public static void SaveJob(Job job, IDbConnection connection)
{
using (DataContext context = new DataContext(connection))
{
JobEntity ent = new JobEntity();
...
context.JobEntities.InsertOnSubmit(ent);
context.SubmitChanges();
HttpRuntime.Cache.Remove("JobList");
}
}
Does anyone have any ideas why this might be happening?
Edit: I am using Linq2SQL to retreive the objects, though I am disposing of the context.
I would ask you to make sure you do not have multiple production servers for load balancing purpose. In that case you will have to user some external dependency architecture for invalidating/removing the cache items.
That's because you don't synchronize cache operations. You should lock on writing your List to the cache (possibly even get the list inside the lock) and on removing it from the cache also. Otherwise, even if reading and writing are synchronized, there's nothing to prevent storing the old List right after your call to Remove. Let me know if you need some code example.
I would also check, if you haven't already, that the old data they're seeing hasn't been somehow cached in ViewState.
You have to make sure that User 2 sent a new request. Maybe the content it saws is from it's browser's cache, not the cache from your server