tags:

views:

135

answers:

4

Hey,

I have a ASP.NET page called admin.aspx that needs to be protected from direct access.

I want it to be accessed only when the user enter his name & password in another page called login.aspx

I'm working in ASP.NET with Visual Basic .NET 2008, and I have no idea how to do it.

Can you help me doing it ?

+1  A: 

You can handle it via Forms authentication. In your case you want to make sure that you restrict the access of admin.aspx so you can do so by giving that entry in web .config by specifying the location tag. Check out this site:

http://www.dnzone.com/go?60

HTH

Raja
Raja ( After protecting this pages, how can I access to them and others can't do it ?)
dotNET
As you said you have to provide username and password (a login page) which authenticates you to access that page.
Raja
Ok thank you Raja
dotNET
+4  A: 

The correct term for this behavior is Authorization

Some things I need to know beforehand:

  • Do you have your own Login / Logout Logic?
  • Are you using a custom User database / table?
  • If both of the above were answered with a yes: Have you read / heard something about Membership- and RoleProviders?

.NET has great built in mechanisms for solving this problem. It doesn't just offer great configuration possibilities, it is also very easy to implement!

Here is a very very detailed walk trough on the ASP.NET Membership Provider:

ASP.NET 2.0 Membership and Roles Tutorial Series

Even though it is using ASP.NET 2.0 and C#, it shouldn't really be that different on .NET3.5/4.0 and VB.NET

Shaharyar
It's too long to read "Shaharyar", i need a quick tutorial.
dotNET
- `Part 2 - master how to create roles and assign users to roles. This article shows how to setup roles, using role-based authorization, and displaying output on a page depending upon the visitor's roles.`You just need to read up to Part 2. This can't take too long ;-).
Shaharyar
A: 

I found it :

In the login page ("login.aspx") do this :

Session("Name") = "Yes"

Response.Redirect("admin.aspx")

In the admin page ("admin.aspx") this :

If Session("Name") = "Yes" Then
    'You can here display anything you want, or just leave it blank
Else
    Response.Redirect("ErrorPage.aspx")
End If
dotNET
+1  A: 

Hi,

you should check the user session first before loading your page.

protected void Page_Load(object sender, EventArgs e)
{
            if (session == null)
            {
                // Just redirect to login page or no access page warning.**
            }
            if (!Page.IsPostBack)
            {
               //if your were logged in then you will access this page
            }
}
Christofel