We help IT Professionals succeed at work.
Get Started

Response.Redirect if Session if Empty

LT1415
LT1415 asked
on
1,812 Views
Last Modified: 2008-02-01
I have been trying to add a redirect script on this simple line of code but it either fails or I get an error.

We are passing login information through querystring.
This code sits in every page. It works to pass the token to a meta tag if they login.

<%
string userName = Request.QueryString["UserName"];
if(userName != "")
{
Session["UserName"] = userName;
}
%>

What I would like to do is if username or session  is empty Response.Redirect to login.

This produces a new line in constant error
<%
string userName = Request.QueryString["UserName"];
if(userName != "")
{
      Session["UserName"] = userName;

if Session["UserName] = ""

 Response.Redirect("http://blah.com/login.asp");
}
%>

Object reference not set to an instance of an object. Doesn't like  (userName.Length == 0)

<%
string userName = Request.QueryString["UserName"];
if(userName != "")
{
      Session["UserName"] = userName;

if (userName.Length == 0)
           
Response.Redirect("/login.asp?" + Request.Url.ToString());
}
%>

How can I get this to redirect if session is empty and they have skipped the login?
Thanks
Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE