Solved

Working with Session variables == null and response.redirect

Posted on 2013-01-02
7
961 Views
Last Modified: 2013-01-07
I'm getting an error when trying to add code in my Site.Master page.
Error: The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.

When I use this code in the actual page (not the Site.Master) I don't get an error and it redirects.  When I try  to add this to my site.master, I get the above error in Firefox.  I thought if I added it to the site master it would always check for the session ID and redirect if it was not found.

Code I am using in
            if (Session["ID"] == null)
            {
                Response.Redirect("page.aspx");  //this page will have end user set the ID session variable before continuing.  
            }
            else
            {
                //stay on page
            }

Is there a better way to handle this?    I was trying to avoid having to add the above code to every page that checks for a Session["ID"].

Thanks!
0
Comment
Question by:javierpdx
  • 3
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
paulmacd earned 400 total points
ID: 38737605
Modify the code so it doesn't run when someone visits Page.aspx.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38737688
Could we see the entire site.master code?  Also, are you sure your master page and your 'actual' page have the exact same code?  You can change them independently.
0
 

Author Comment

by:javierpdx
ID: 38738337
The site.master does not load the page load on each link click.  My code below only works on initial load.
When I add  "if (url.EndsWith("page.aspx"))" it ignores my link clicks.

This is the code I'm using.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Portal
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Changes all users to HTTPS
            string url = HttpContext.Current.Request.Url.ToString();
            if (url.StartsWith("http:") && !url.Contains("localhost"))
            {
                HttpContext.Current.Response.Redirect("https" + url.Remove(0, 4), false);
            }
          
           if (url.EndsWith("NSPLogin.aspx"))
                {
                    //ignores "else"
                }

                else
                {
                    if (Session["ID"] == null)
                    {
                        Response.Redirect("Page.aspx");
                    }
                    else
                    {
                        //stay on page
                    }
                }           
        }
    }
}

Open in new window

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Portal.SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <!-- favorites ico -->
<link rel="shortcut icon" href="Images/favicon.ico" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
                <h1>
                    Portal
                </h1>
            </div>
            <div class="clear hideSkiplink">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                        <asp:MenuItem NavigateUrl="~/AddDetails.aspx" Text="Add Student Application"/>
                        <asp:MenuItem NavigateUrl="~/Counts.aspx" Text="View Reports" 
                            Value="View Reports"/>
                    </Items>
                </asp:Menu>
                
            </div>
        </div>
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="footer">
    </div>
    </form>
</body>
</html>

Open in new window

Thanks.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Expert Comment

by:Barry62
ID: 38738365
Is that the entire site.master.cs file?

Also, are you sure you have the CodeBehind reference on every page?
0
 

Author Comment

by:javierpdx
ID: 38738402
That is the entire file.

No.   I thought I could avoid adding the reference on every page.    That is why I was using the site.master.cs for the reference.   Do I need to add it to every page?

Thanks.
0
 
LVL 8

Assisted Solution

by:Barry62
Barry62 earned 100 total points
ID: 38738847
Yes.  The line:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Portal.SiteMaster" %>

is what tells every page to use the master page.
0
 

Author Closing Comment

by:javierpdx
ID: 38752316
Thanks for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now