Ive just created a login system using a tutorial, but it doesnt say how to protect the admin page, just logs you in..

Here first is my HTML, Login form

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="login.aspx.vb" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form runat="server">
<h1>Log In</h1>

Username:<br />
<asp:TextBox ID="UserName" Runat="server" /><br />
Password:<br />
<asp:TextBox ID="Password" TextMode="password" Runat="server" /><br />
<asp:Button ID="LoginButton" Text="Log In" OnClick="LogIn" Runat="server" /><br />

<asp:Literal ID="LtlLogin" Runat="server" />



Here is my code behind the login in button..

Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Boolean
        Dim Connection As OleDbConnection
        Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("database\UsersPasswords.mdb"))
        Dim Command As OleDbCommand
        Dim UserExists As Boolean
        Command = New OleDbCommand("SELECT * FROM UsersPasswords WHERE [Username]='" & strUsername & "' AND [Password]='" & strPassword & "'", Connection)
        Dim DataReader As OleDbDataReader
        DataReader = Command.ExecuteReader()

        If DataReader.Read() Then
            UserExists = True
            UserExists = False
        End If

        Return UserExists
    End Function

    Public Sub LogIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginButton.Click
        If FormsAuthentication.Authenticate(UserName.Text, Password.Text) = True Then
            FormsAuthentication.SetAuthCookie(UserName.Text, True)
            If DBAuthenticate(UserName.Text, Password.Text) Then
                FormsAuthentication.SetAuthCookie(UserName.Text, True)
                LtlLogin.Text = "<p>sorry wrong login details</p>"
            End If
        End If
    End Sub

Here is my web.config file code..

<?xml version="1.0"?>
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
      <authentication mode="Forms">
        <forms loginUrl="Login.aspx">
          <credentials passwordFormat="Clear">
            <user name="admin" password="password"/>
        <allow users="*"/>

    <location path="admin">
          <allow users="admin"/>
          <deny users="*"/>

    <location path="members">
          <deny users="?"/>

The problem is anyone can just type /admin/default.aspx and get onto the admin page, i want to them to be redirected to the login page till they use the correct password.

Im guessing its code Behind the admin page, but im not sure what it is.

Hope you can help, Thanks

7 Years
Discussion Span
Last Post by guru_sarkar

----> "The problem is anyone can just type /admin/default.aspx and get onto the admin page, i want to them to be redirected to the login page till they use the correct password."

Looking at your web.config it should take care of this and nobody except user with username "admin" can access the page. Now why is it not behaving properly...not sure but try any of these and see if it works:
1: in admin page_load check the user like below:
If HttpContext.Current.User.Identity.Name IsNot "admin" Then
End If

2: add a web.config to your adminFolder with following settings:

<system.web>        <authorization>          <allow users="admin"/>          <deny users="*"/>        </authorization>      </system.web>

This is just same what your location pat="admin" does but to make sure it is not that one ...remove it after you add web.config with above settings.

3: Or Could be admin cookie is persisted before

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.