Link to home
Start Free TrialLog in
Avatar of LT1415
LT1415

asked on

Response.Redirect if Session if Empty

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
Avatar of Munawar Hussain
Munawar Hussain
Flag of Pakistan image

First make sure the variable UserName is persent in QueryString
second first check it for null
if (Request.QueryString["UserName"] != null)
{
string mysession= Request.QueryString["UserName"];
}
SOLUTION
Avatar of jasondavidcarr
jasondavidcarr
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LT1415
LT1415

ASKER

As they are coming in in snippets--perhaps I am not applying them correctly, but these suggestions did not resolve the problem
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LT1415

ASKER

Thanks.
Thiis is what finally worked found helpful suggestions in both replies
<%
string userName = Request.QueryString["UserName"];
if(userName != "")
{
Session["UserName"] = userName;
}
 
{
      Session["UserName"] = userName;
 

if (String.IsNullOrEmpty(userName) == true)  

           
Response.Redirect("/login.asp?" + Request.Url.ToString(), false);
}

%>