2

I am trying to figure out how to reload a page upon the user choosing to log out. This involves setting the logged in cookie to expired, and when the page reloads it will display as an unlogged in user.

// expiring the cookie for logout
ARCookie.Expires = DateTime.Now;
Response.Cookies.Set(ARCookie);

What seems to be a quick and dirty way of doing this is one of these three:

// reload the page
Response.Redirect(thisPage);
//or
Server.Transfer(thispage, false);
//or 
Server.TransferRequest(thispage, false);

Which of these is the best way, or is there another way of doing it that is preferred. I can't find any kind of explicit "Refresh" command or action in the Page object. Is there one that I am perhaps missing somewhere?

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121

5 Answers5

3

This seems like the most reasonable approach:

Response.Redirect(thisPage);

Keep in mind that there isn't really a concept of a "reload" in an HTTP request/response environment. The client issues a request, the server returns a response. A "reload" from the client's perspective is to re-issue the same request. A "reload" form the server's perspective is to indicate in the response that the client should issue a specific request, even if that request is right back to the same page. A redirect response is the standard way for the server to indicate this.

David
  • 208,112
  • 36
  • 198
  • 279
  • Just a side-note, using Response.Redirect like that causes a `ThreadAbortionException`. – MikeSmithDev Mar 17 '14 at 19:27
  • @MikeSmithDev: Correct: http://stackoverflow.com/a/4874947/328193 In most cases it's not an issue, and ultimately comes down to how one's code paths are arranged. A redirect should logically be treated similarly to a `return` in any method, in that it's an exit path for the logic. – David Mar 17 '14 at 19:32
  • 2
    OK yeah just wanted OP to be aware. And you were supposed to link to http://stackoverflow.com/questions/13727422/can-endresponse-increase-performance-of-asp-net-page/13727769#13727769 :) – MikeSmithDev Mar 17 '14 at 19:33
3

Can you throw in some JS code and register it in the code-behind? Try the following:

 StringBuilder sb = new StringBuilder("<script ");
 sb.Append(" type=\"text/javascript\">\r\n");
 sb.Append("window.location.reload();\r\n");
 sb.Append("</script>")

 RegisterStartupScript("reload", sb.ToString());

You can also use RegisterClientScriptBlock() depending on your needs.

Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53
fahadash
  • 3,133
  • 1
  • 30
  • 59
  • This is neither C#, ASP.Net-centric nor is it 'fast'. David's answer is the most commonly-used way. – Brinky Mar 17 '14 at 19:13
  • 2
    I could, but why? I guess I wasn't aware of the "reload" method of the "window" object, and thanks for that, but I'd have to reload the page in order to register this javascript in the first place. And this would cause the page to reload *twice*. This is suboptimal. – Cyberherbalist Mar 17 '14 at 19:17
  • 2
    @brinky - I am afraid that you are mistaken. The posted code IS C# and ASP.NET-centric. As far as "fast" is concerned, the measure of speed in this instance is actually inconclusive, as the solution that he posted showed how JS can be used to do a page reload. As the post does not say where he plans to do the reload, it is safe to assume that he will be putting it somewhere to reload the page without causing an infinite loop. As such, I think this answer is sufficient and opens up doors to other flexibility, should he desire to reload dynamically, without AJAX. – Zachary Kniebel Mar 17 '14 at 19:19
  • 2
    @Cyberherbalist - Why do you say that this would make you have to load the page twice to register the script? Putting the `RegisterStartupScript` or `RegisterClientScript` blocks in your `Page_Load` handler registers the script the first time the page loads. If you were looking for redirection, specifically, then you should have been more specific in your OP. – Zachary Kniebel Mar 17 '14 at 19:22
  • The OP asked specifically for "in ASP.Net", plus he shows a few ways he can, and asked for clarification of which is "preferred". Don't muddy the waters by throwing in some unnecessarily obtuse javascript creation, when he clearly (from his posted code samples) is expecting to do this directly from the page's codebehind, in C#. – Brinky Mar 17 '14 at 19:25
  • 1
    @brinky - ASP.NET is not a language, it is a framework. If you are trying to say that he would like a solution that uses the ASP.NET framework, that is what this solution is: both `RegisterStartupScript` and `RegisterClientScript` are methods in C#, in the ASP.NET framework. This solution is neither obtuse, nor is it not in C#. I would agree with you if the post asked for "Redirection", but asking for "Reloading" (which inherently states that it is loaded again after initially loading - semantics) the best way to do that is through JS, unless you wish to use AJAX, which still involves JS. – Zachary Kniebel Mar 17 '14 at 19:29
  • @brinky and this is exactly being done using Server-Side code. Sending an HTTP 204 for the same URL may be subject to Browser's cache limitations (depending on settings). And can open the window of infinite redirects. Many websites send you to a median redirect page with a message like "This page is being refreshed." – fahadash Mar 17 '14 at 19:29
  • 1
    @brinky - The fact of the matter is that of all of the solutions that have been posted on this page, this one is the ONLY one that actually "reloads" the page. All of the other solutions (again), without AJAX, redirect the page, instead of reloading it - even if that means they are redirecting to the same page. – Zachary Kniebel Mar 17 '14 at 19:32
  • fahadash - I am sorry that this post was down-voted. Even if it is not what @Cyberherbalist was looking for, I still believe that this is a very accurate answer and would be very helpful to others in the future. – Zachary Kniebel Mar 17 '14 at 19:37
  • @ZacharyKniebel Thanks for backing me on this. Downvotes don't make a difference to me in this case as I actually earned 16 reps and lost zero. (Don't know why). But of all my years of web development, I've found this to be a very good way of reloading. And your comments were informative too. Thanks – fahadash Mar 17 '14 at 19:51
  • @fahadash - not a problem at all. I found this to be a great solution and I can only hope that the perceived lack of upvotes does not dissuade future users from looking at it. – Zachary Kniebel Mar 17 '14 at 20:01
  • @ZacharyKniebel, the question was tagged C# precisely because I was not expecting to use javascript. And I suppose you mean that I could always put a reload script in there which under some circumstances would be called. Fair enough. If fahadash will edit his answer in some minute way, I will take back my downvote. – Cyberherbalist Mar 17 '14 at 21:03
  • @fahadash, please edit your answer (add a space, some minor change) and I will be able to take my downvote back (SE won't let you change your downvote after 1 hour, unless the answer gets edited). Then I will change my vote. – Cyberherbalist Mar 17 '14 at 21:04
  • 1
    @Cyberherbalist - as fahadash stated, the downvotes do not bother him. It is my opinion that this answer should remain as it is, as I believe that it would be very helpful to others, in its current state. If you disagree, and believe that it would be an unhelpful solution that would cause more harm then good to others who view this post, then I implore you to stand by your opinion and leave your downvote. Otherwise, removing it is simply a matter of what you think is best for StackOverflow and its users. :) Happy coding! – Zachary Kniebel Mar 17 '14 at 21:08
  • You're not listening, @ZacharyKniebel. I asked him to make a nominal change to the answer (one that doesn't affect his meaning), so that SE would enable me to remove my downvote. I am not bothered if he doesn't care about the downvote -- I don't want my downvote to stand because I am now convinced my giving it was unfair, and I want to correct MY error. And it is also a matter of what is best for SO and its users, as you say. OK? Thanks for your input, by the way. :-) – Cyberherbalist Mar 17 '14 at 21:19
  • Ah, I see - I apologize for the misunderstanding. I will help you out and unlock it for changing your vote. Give me a few seconds. – Zachary Kniebel Mar 17 '14 at 21:22
  • @fahadash wrote: "I actually earned 16 reps and lost zero. (Don't know why)." The reason for this is that upvoted answers are worth +10 each, and downvotes cost only -2 rep points each. The accentuation is on the positive, fortunately. – Cyberherbalist Mar 17 '14 at 21:24
  • OK, I've removed my downvote and added an upvote. Thanks all around. I could have done the edit myself, of course, but it seemed unethical in some small way. – Cyberherbalist Mar 17 '14 at 21:25
2

I think in the most cases you don't need to return to the same page after logout. You redirect to the main page instead, which is prepared to show the right layout whether the user are logged or not.

Returning always to the current page after a logout could cause unexpected behavior such an authorization failed page or let the user see a page for logged users.

So I would say none of the three. Maybe something like

System.Web.Security.FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
Sérgio S. Filho
  • 364
  • 1
  • 4
  • 13
  • Well, OK, but the situation is not what you assume. I didn't explain the page's operation (because it wasn't germane to the question), but just for you I will say that the page is freely viewable by anyone, although logged in users are presented with controls for editing the page that non-logged in users won't see. Their logged status is maintained in a cookie which expires after a period of time, or immediately if they log out. The risk you rightfully cite does not apply here. But thanks for your answer. – Cyberherbalist Mar 17 '14 at 21:14
1

In the HTTP protocol there's no "reload". Redirecting the browser to the same page, after the cookie is deleted, should probably work.

zmbq
  • 38,013
  • 14
  • 101
  • 171
-2

try this will reload the same page or request the same page

Response.Redirect(Request.Path);       

Reload page after set cookie value and expires to take effect

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
M ragab
  • 1
  • 2