Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
I want to open all pages in asp.net website should open if user first open login page.
Do not want open any page from direct url, and if user enter any url then redirect to login page.
Currently I am Using a Session variable and I True it before Redirect page and On page load of 
other page I Check whether it is True Or false and then False at the end of page load.
Posted
Updated 20-Feb-14 12:19pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Feb-14 18:20pm    
I think this is not a good idea, but...
—SA
Sergey Alexandrovich Kryukov 20-Feb-14 18:24pm    
And you probably mean ASP.NET, not "ASP" and "NET" (what is it? :-). You should understand that ASP.NET is not a kind of ASP.
—SA

ASP .NET provides mechanism for doing something before the page is loaded i.e rendered.
Page.PreInit[^] methods is invoked before the page is initialized.

The event enables you to set values that are used later in the page life cycle. You can dynamically set a master page or a theme for the requested page, and create dynamic controls
(Each and every damn page has this method)

So what you can do is, check the Session on this PreInit method on every page (Except your Login), like this way,
C#
onPreInit(... ...)
{
    if(Session["UserName"] == null)
    Response.Redirect("Login.aspx");
}

-KR
 
Share this answer
 
Basically, you cannot limit access to any page on your site, if this page is accessible at all. Note that you do not require that only the authenticated user has access to some page (the purpose of it would be more clear), you want to allow access from some page only from a certain page; from your question it is clear that it could be not yet authenticated user.

So, the purpose of it is unclear, and I doubt that it is a good idea. Anyway, you cannot limit the access to the page, as well as you cannot prevent any user from typing any URL in the browser's address bar.

You can do different thing. In your ASP.NET code, you can check up the referrer URL: http://en.wikipedia.org/wiki/Referrer[^].

http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx[^].

Depending on the referrer, your ASP.NET page can provide different content, if the referrer is not what you expect, anything at all, even generate error 404.
I hope the rest of it is clear. :-)

—SA
 
Share this answer
 
v3
Comments
idenizeni 20-Feb-14 20:24pm    
I agree using the referral URL is a viable option, I would only add that a user can easily build a request and manually set the referrer URL to whatever they desire.
Sergey Alexandrovich Kryukov 20-Feb-14 20:50pm    
Yes, that is one of the reasons why I think the OP's requirement is not a really good idea. But we don't know the OP's motivation. The protection against such a trick (which could be done only by a negligible minority of users) could be considered as overkill.

The reliable way to sort out restricted content would be based on user authentication...

Thank you,
—SA
When a user login to your application set a session variable with user name and then when the user requests a page check session variable set or not if it is not set then redirect to login page

Use the following code in the master page of you project
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["user"].ToString() == "")
    {
        Response.Redirect("~/Default.aspx");
    }
}
 
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