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
Web DevelopmentASP.NET

Avatar of undefined
Last Comment
LT1415

8/22/2022 - Mon