Athena,
I’ve looked at your code and your problem stems from a basic
misunderstanding of ASP.NET’s “Forms†authentication. Since I am also a
rookie programmer let me give you a brief break down of how it works.
When a user clicks the login button on the login form the following sequence
occurs:
1. Username and Password are validated against the data store (XML, SQL etc)
2. If valid and Authentication Ticket is created that contains the Username
3. The Ticket is encrypted and passed into the pending Http Response
4. The current page is “Refreshed†with a Response.Redirect (and the cookie
is delivered to the browser)
The user is now logged in and User.Identity.Name and User.Identity will now
be populated. How? The following occurs:
1. Http request begins (before the Page is even created)
2. If the request contains a Authentication cookie it is decrypted (it does.
Step 4 above)
3. A user Principal is created containing the Username
4. This Principal is assigned to the current Context (User.Identity.XXXX is
now available)
5. … rest of the request processing, page processing continues …
This may look confusing at first but understanding it is vital in
understanding how authentication in ASP.NET works.
I’ve included a sample (unfortunately I only speak C# - but there is very
little and it is well commented) that will provide the behavior you are
looking for.
The default.aspx page has a [LoginStatus] control as well as a [LoginView]
to hide our controls from anonymous users. The [CreateUserWizard] control is
part of the <loggedIn> template and visible is false.
In Page_Load we check to see if the user is “adminâ€. If so
[CreateUserWizard].Visible = true;
To make it work, run the “Web Site Administration Toolâ€, enable security and
add a “admin†and a couple of test users.
I hope this lifts the fog.
Wainage
=============================================
[default.aspx]
-------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
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">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
</div>
<div>
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
To Work on the site u need to log in
</AnonymousTemplate>
<LoggedInTemplate>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
Visible="false">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1"
runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1"
runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
<div>
All Logged in users can see this ...
</div>
</LoggedInTemplate>
</asp:LoginView>
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
// the Wizard control is only shown when the user is Authenticated
// so we need to find the control (this.CreateUserWizard1 does not
work)
// We ask LoginView to find the control
CreateUserWizard wizard =
(CreateUserWizard)LoginView1.FindControl("CreateUserWizard1");
// did we find it?
if (wizard != null) // Yes!
{
// check username
if ("admin" == User.Identity.Name)
wizard.Visible = true; // for "admin"
else
wizard.Visible = false; // for everyone else
}
}
=============================================
[login.aspx]
-------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs"
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">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Login ID="Login1" runat="server">
</asp:Login>
</form>
</body>
</html>
=============================================