Wrox Programmer Forums
|
ASP Forms As of Oct 5, 2005, this forum is now locked. Please use "Classic ASP beginner" at http://p2p.wrox.com/forum.asp?FORUM_ID=54 or "Classic ASP Professional" http://p2p.wrox.com/forum.asp?FORUM_ID=56 instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Forms section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2005, 09:30 PM
Registered User
 
Join Date: Jun 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Forms Authentication problem

Hi everyone, im new in asp.net. I have already installed visual web developer 2005 beta 2. Running .aspx works fine. Now i want to try to create an application. I want to use forms as my authentication type. I understand clearly what it should do. For simplicity i have 2 aspx pages. default.aspx and login.aspx. Ofcourse it is obvious that i want to redirect automatically to login.aspx everytime i try to access any pages on my application without loggin in first. Im using SQL Server 2000 to store username and password. and uses stored procedure to check for username and password. It works fine, actually i just copy this in a book.

here is the code.

"web.config"

        <authentication mode="Forms"/>
        <authorization>
          <deny users="?"/>
        </authorization>

Note: I just put the important

"default.aspx"

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
<h2> Hello, main page</h2>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

"login.aspx"

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
    Sub Button_Click(ByVal s As Object, ByVal e As EventArgs)
        If IsValid Then
            If DBAuthenticate(txtEmployeeID.Text, txtPassword.Text ) > 0 Then
                FormsAuthentication.RedirectFromLoginPage(txtEmplo yeeID.Text, True)
            End If
        End If
    End Sub
    Function DBAuthenticate(ByVal strEmpID As String, ByVal strPassword As String) As Integer
        Dim conMyData As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim parmReturnValue As SqlParameter
        Dim intResult As Integer

        conMyData = New SqlConnection("Server=APMC-2KSQLSRV;Initial Catalog=APMC_master;Integrated Security=True")
        cmdSelect = New SqlCommand("DBAuthenticate", conMyData)
        cmdSelect.CommandType = CommandType.StoredProcedure
        parmReturnValue = cmdSelect.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
        parmReturnValue.Direction = ParameterDirection.ReturnValue
        cmdSelect.Parameters.Add("@empid", strEmpID)
        cmdSelect.Parameters.Add("@password", strPassword)
        conMyData.Open()
        cmdSelect.ExecuteNonQuery()
        intResult = cmdSelect.Parameters("RETURN_VALUE").Value
        conMyData.Close()
        If intResult < 0 Then
            If intResult = -1 Then
                lblMessage.Text = "No such Employee ID"
            Else
                lblMessage.Text = "Invalid Password"
            End If
        Else
            ' Session("employeeid") = txtEmployeeID.Text
            ' lblMessage.Text = Session("employeeid")
        End If

    End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <table align="center" border="1" style="width: 50%; height: 50%">
        <tr>
            <td>
                <asp:Label ID="lblMessage" ForeColor="Red" Font-Bold="True" runat="server" />
            </td>
            <td>
                <asp:Label ID="lblMessage2" ForeColor="Red" Font-Bold="True" runat="server" />
            </td>
        </tr>
           <tr>
                <td>
                  Employee ID:
                </td>
                <td>
                <asp:TextBox ID="txtEmployeeID" runat="Server" />
                <asp:RequiredFieldValidator ID="fldvalidatorempid" ControlToValidate="txtEmployeeID" Text="Employee Id is required!" runat="Server" />
                </td>
           </tr>
           <tr>
                <td>
                    Password:
                </td>
                <td>
                <asp:TextBox ID="txtPassword" runat="Server" />
                <asp:RequiredFieldValidator ID="fldvalidatorpassword" ControlToValidate="txtPassword" Text="Required" runat="Server" />
                </td>
           </tr>
           <tr>
                <td colspan="2" align=center>
                <asp:Button ID="btnLogin" Text="Login" OnClick="Button_Click" runat="Server"/>
                </td>
           </tr>
           <tr>
            <td>

            </td>
           </tr>
        </table>
    </div>
    </form>
</body>
</html>

The problem: Authentication is working fine... But what i want is, after the successful authentication i want redirect automatically from the source page, or if no resource page will go automatic to default.aspx..

Hope i am clear..

Thank you very much!!!







Similar Threads
Thread Thread Starter Forum Replies Last Post
Forms Authentication thetway Classic ASP Basics 1 August 18th, 2005 05:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.