troubleshooting Question

Response.Redirect if Session if Empty

Avatar of LT1415
LT1415 asked on
Web DevelopmentASP.NET
5 Comments2 Solutions1813 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
nisarkhan

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros