Simple Login programming: Authentication does not work for me!



Hello Bob,

I thought it should be a simple matter to put a login page together after watching LearnVisualNet videos ! O boy how wrong I was. First of all I realized that even the guru's web site doesn't work(!) (Try sending an email in http://www.learnvisualstudio.net/Contact.aspx, even if I after I notified them by phone)

Here is the project:

I have a default page (default.aspx) with a login hyperlink. The user clicks on it. A login page (login.aspx) opens and lets the user to fill a user name and a password. The code checks if the user is "admin" and has the right password. If so, it opens a page (demo.html) with a CreateUserWizard and makes this control visible. If not and the user is validated the same page where the user can do something else (See the code below).

The problem is where to check user's credentials to direct the program's output. I tried Authenticate and LoggedIn events in Login.aspx. In both cases User.Identity.Name returns empty string and User.Identity.IsAuthenticated is false. I have been posting questions to ASP.Net web site. So far 11695 people viewed and 13 people answered my posts. So far nobody made it to work.

So where can I place my code to validate the user to direct the user to right page? The code in Login.aspx shown below never works although User.xxx are in LoggedIn event i.e. the user is supposedly authenticated. So I expect that User.Identity.Name should not be empty and User.Identity.IsAuthenticated is true. I even had a Page_Load event to test the values of these variables. The program comes to Page_Load event twice. Once before the loging starts and second after the the user logged in. Even after the second time User.Identity.Name is empty! This code does not work in Demo.aspx either for similar reasons. I listed all the aspx files below. I hope they help to show where I am doing wrong.

As a solution I can define a global variable and use that to direct my program flow. How does one define a global variable in VS2005 Visual Web? I am a newbie.

Thank you very much.

Athena

Login.aspx : if the user is authenticated this routine should open Demo.aspx. The Login1.Authenticate sub may not be necessary.

---------------------------------------------

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)

If User.Identity.Name = Login1.UserName And User.Identity.IsAuthenticated = True Then

e.Authenticated = True

End If

End Sub


Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs)

If User.Identity.Name = Login1.UserName And User.Identity.IsAuthenticated = True Then

Server.Transfer("Demo.aspx")

End If

End Sub

----------------------------------------------------------------------

Demo.aspx : if the user is Admin then the CreatUSerWizard1 control should be visible

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

If User.Identity.Name = "admin" And User.Identity.IsAuthenticated = True Then <<<<<<< User.Identity.Name is empty

CreateUserWizard1.Visible = True

End If

End Sub

Default.aux:

------------------------------------------------

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

<!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">

</head>

<body>

<form id="form1" runat="server">

<div>

<span style="font-size: 10pt; font-family: Verdana">Support Options<br />

<br />

Support functions require you to login to a secure web site<br />

</span>

<span style="font-size: 10pt; font-family: Verdana">&nbsp;</span>

<br />

<table style="width: 447px">

<tr>

<td style="width: 444px">

<asp:LoginStatus

ID="LoginStatus1" runat="server" Font-Names="Verdana" Font-Size="10pt" Width="34px" />

</td>

</tr>

<tr>

<td style="width: 444px; height: 27px">

<asp:LoginName ID="LoginName1" runat="server" Font-Names="Verdana" Font-Size="10pt"

FormatString="Hello {0}" Style="z-index: 100; left: 15px; position: absolute;

top: 110px" />

</td>

</tr>

<tr>

<td style="width: 444px; height: 77px">

<asp:LoginView ID="LoginView1" runat="server">

<LoggedInTemplate>

<span style="font-size: 10pt; font-family: Verdana">You are now &nbsp;logged in. Welcome.</span>

<br />

<br />

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/CreateClient.aspx">Create Client</asp:HyperLink>

</LoggedInTemplate>

<AnonymousTemplate>

<span style="font-size: 10pt; font-family: Verdana">You are not logged in. Click the

login link to sign in.</span>

</AnonymousTemplate>

</asp:LoginView>

</td>

</tr>

</table>


</div>

</form>

</body>

</html>



Login.aspx

-------------------------------------------------

<%@ Page Language="VB" %>

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

<script runat="server">


Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)

If User.Identity.Name = Login1.UserName And User.Identity.IsAuthenticated = True Then

e.Authenticated = True

End If

End Sub


Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs)

If User.Identity.Name = Login1.UserName And User.Identity.IsAuthenticated = True Then

Server.Transfer("Demo.aspx")

End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml"; >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4"

BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"

ForeColor="#333333" OnAuthenticate="Login1_Authenticate" DestinationPageUrl="~/demo.aspx" OnLoggedIn="Login1_LoggedIn">

<TextBoxStyle Font-Size="0.8em" />

<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"

Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />

<InstructionTextStyle Font-Italic="True" ForeColor="Black" />

<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />

</asp:Login>


</div>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Login1" />

</form>

</body>

</html>

Demo.html

----------------------------------

<%@ Page Language="VB" %>

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

<script runat="server">


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

If User.Identity.Name = "admin" And User.Identity.IsAuthenticated = True Then

CreateUserWizard1.Visible = True

End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml"; >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:RadioButton ID="optTMDemo" runat="server" Font-Names="Verdana" Font-Size="10pt"

ForeColor="#336699" Style="z-index: 100; left: 6px; position: absolute; top: 70px"

Text="Tumor Management System Demo" />

<asp:RadioButton ID="optPMDemo" runat="server" Font-Names="Verdana" Font-Size="10pt"

ForeColor="#336699" Style="z-index: 101; left: 6px; position: absolute; top: 95px"

Text="Paw Management System Demo" />

<asp:Label ID="Label1" runat="server" Font-Names="Verdana" Font-Size="10pt" ForeColor="#336699"

Style="z-index: 102; left: 8px; position: absolute; top: 37px" Text="Product demos"></asp:Label>

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8"

BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"

Style="z-index: 104; left: 5px; position: absolute; top: 159px">

<SideBarStyle BackColor="#5D7B9D" BorderWidth="0px" Font-Size="0.9em" VerticalAlign="Top" />

<SideBarButtonStyle BorderWidth="0px" Font-Names="Verdana" ForeColor="White" />

<ContinueButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"

BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />

<NavigationButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"

BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />

<HeaderStyle BackColor="#5D7B9D" BorderStyle="Solid" Font-Bold="True" Font-Size="0.9em"

ForeColor="White" HorizontalAlign="Center" />

<CreateUserButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"

BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />

<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

<StepStyle BorderWidth="0px" />

<WizardSteps>

<asp:CreateUserWizardStep runat="server">

</asp:CreateUserWizardStep>

<asp:CompleteWizardStep runat="server">

</asp:CompleteWizardStep>

</WizardSteps>

</asp:CreateUserWizard>


</div>

</form>

</body>

</html>

web.config:

--------------------------

<?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

\Windows\Microsoft.Net\Framework\v2.x\Config

-->

<configuration>

<appSettings/>

<connectionStrings/>

<system.web>

<!--

Set compilation debug="true" to insert debugging

symbols into the compiled page. Because this

affects performance, set this value to true only

during development.

Visual Basic options:

Set strict="true" to disallow all data type conversions

where data loss can occur.

Set explicit="true" to force declaration of all variables.

-->

<roleManager enabled="true" />

<pages>

<namespaces>

<clear/>

<add namespace="System"/>

<add namespace="System.Collections"/>

<add namespace="System.Collections.Specialized"/>

<add namespace="System.Configuration"/>

<add namespace="System.Text"/>

<add namespace="System.Text.RegularExpressions"/>

<add namespace="System.Web"/>

<add namespace="System.Web.Caching"/>

<add namespace="System.Web.SessionState"/>

<add namespace="System.Web.Security"/>

<add namespace="System.Web.Profile"/>

<add namespace="System.Web.UI"/>

<add namespace="System.Web.UI.WebControls"/>

<add namespace="System.Web.UI.WebControls.WebParts"/>

<add namespace="System.Web.UI.HtmlControls"/>

</namespaces>

</pages>

<!--

The <authentication> section enables configuration

of the security authentication mode used by

ASP.NET to identify an incoming user.

-->

<!--

The <customErrors> section enables configuration

of what to do if/when an unhandled error occurs

during the execution of a request. Specifically,

it enables developers to configure html error pages

to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<error statusCode="403" redirect="NoAccess.htm" />

<error statusCode="404" redirect="FileNotFound.htm" />

</customErrors>

-->

<compilation debug="true"/></system.web>

<system.net>

<mailSettings>

<smtp from="hiittite@xxxxxxxxx">

<network host="localhost" password="" userName=""/>

</smtp>

</mailSettings>

</system.net>

</configuration>

.