Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,
I have application.The structure of application is like login.aspx when registred user logon its redirect to default.aspx.when user is in default.aspx page if i copy url and past it in other browser or tab.its directly open the default.aspx page.I wanted to do When user past the URL its should redirect to login.aspx page.How to do it

What I have tried:

I have tried by giving authorization in web.config file
Posted
Updated 4-Mar-16 17:42pm
Comments
Philippe Mori 4-Mar-16 12:28pm    
There are many tutorials and videos on the web. Use them as a starting point to learn how to do it. By the way, there are even some template that give working site that has authentification.
Veena Hosur 4-Mar-16 23:40pm    
what are those tutorials and videos.can you please share link
Philippe Mori 5-Mar-16 8:06am    
Use Google, YouTube, MSDN and ASP.NET and you will find a lot of stuff.

Hi, refer the following article,
ASP.NET authentication and authorization[^]
 
Share this answer
 
If you are building simple application which require less security, here is a simpler way to do it.

It gives you a very basic idea.

You can build a class for storing user login credentials in session sate something like this:
C#
public class LoginUser
{
    public static bool IsLogin
    {
        get
        {
            if (HttpContext.Current == null ||
                HttpContext.Current.Session == null ||
                HttpContext.Current.Session["IsLogin"] == null)
                return false;

            return (bool)HttpContext.Current.Session["IsLogin"];
        }
        set
        {
            HttpContext.Current.Session["IsLogin"] = value;
        }
    }

    public static string Username
    {
        get
        {
            return HttpContext.Current.Session["Username"] + "";
        }
        set
        {
            HttpContext.Current.Session["Username"] = value;
        }
    }
}

a simple example of code behind of login page:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (LoginUser.IsLogin)
        Response.Redirect("~/Default.aspx", true);
}

protected void btLogin_Click(object sender, EventArgs e)
{
    LoginUser.IsLogin = true;
    LoginUser.Username = txtUsername.Text;
    Response.Redirect("~/Default.aspx", true);
}

And the code behind of Default.aspx page:
C#
protected override void OnInit(EventArgs e)
{
    if (!LoginUser.IsLogin)
    {
        Response.Redirect("~/Login.aspx", true);
        return;
    }
    base.OnInit(e);
}

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("User logged In: " + LoginUser.Username);
}

And you can clear the session state values during Logout:
C#
public partial class Logout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Session.Clear();
        Session.Abandon();
        Response.Clear();
        Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddSeconds(-30);
        Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
    }
}
 
Share this answer
 
v2
string strPreviousPage = "";
 if (Request.UrlReferrer != null)
   {
    strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];          
    }        
if(strPreviousPage =="")
    {
      Response.Redirect("~/Login.aspx");
     }

this one is working for me.Asheej Kommery's .NET blog - How to restrict the user to copy and paste the URL in browser address bar in ASP.NET[^][^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900