PDA

View Full Version : Managing OnAuthenticate event of asp:login in code-behind [ASP.NET 2005]


Shaitan00
08-06-2008, 09:11 AM
I have a page [default.aspx] which associated code-behind [default.aspx.cs].
In my default.aspx page I am using the ASP.NET Login Control and want to be able to manage the authentication event myself by doing the following:
<asp:login id="Login1" OnAuthenticate="OnAuthenticate" runat="server">

In the code-behind [default.aspx.cs] I have added the following function within (public partial class Default_aspx : System.Web.UI.Page {...})


private void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
string sUser = Login1.UserName;
string sPwrd = Login1.Password;
PerformCustomerAuthentication(sUser, sPwrd);
}


However this approach causes me two problems
a) Error 1 'Default_aspx.OnAuthenticate(object, System.Web.UI.WebControls.AuthenticateEventArgs)' is inaccessible due to its protection level
b) Error 2 The name 'Login1' does not exist in the current context

Is there anyway for me to implement it this way or do I have to add the "OnAuthenticate" function within <script> in the default.aspx page directly?

Any help would be greatly appreciated...
Thanks,

Brandoe85
08-06-2008, 10:42 PM
Change the method scope to be protected and not private. Take a look at variable scope (http://msdn.microsoft.com/en-us/library/ms973875.aspx)

Good luck;

Shaitan00
08-07-2008, 03:37 AM
That resolves problem (a) but still having the issue with problem (b)
b) Error 2 The name 'Login1' does not exist in the current context

Any clues?
Login1 is the asp:Login control id, not sure why I can't access it.

Reading through the article you provided (much appreciated) but don't see how to resolve (b)...
Thanks,

Brandoe85
08-07-2008, 04:52 PM
That's weird - have you tried rebuilding the app? What's the codebehind and aspx file look like?

Did you change anything in the Page declarative?

Shaitan00
08-07-2008, 05:03 PM
Page declaritive is:


<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Home"
CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>


As for my code-behind aspx.cs file.


public partial class Default_aspx : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
}

protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = AuthenticationMethod(Login1.UserName, Login1.Password);

e.Authenticated = Authenticated;
}

Brandoe85
08-07-2008, 05:19 PM
Is your login control in a content placeholder - or is it in the master page? What's the aspx look like? Seems this should work if it's in a content placeholder:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Default.master" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>


<asp:Content ID="c" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:login ID="Login1" runat="server" OnAuthenticate="OnAuthenticate"></asp:login>
</asp:Content>

Shaitan00
08-08-2008, 02:56 AM
I tried making some changes (to emulate what you posted) - still doesn't work... Login1 does not exist in the current context ... (same error).

The login control is on the default page (non-master). Here is what they look like:

default.aspx

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Home" CodeFile="Default.aspx.cs" Inherits="Default_aspx" AutoEventWireup="true" %>

<asp:content id="Content1" contentplaceholderid="Main" runat="server">
</asp:login>


default.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="Default_master" %>

<!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" xml:lang="en" >
<head runat="server">
<title></title>
</head>
<body>


(recall - I am trying to access Login1.Username within the code-behind default.aspx.cs file)

Brandoe85
08-08-2008, 02:32 PM
The only thing there is- I don't see the placeholder on the masterpage as well...but that would throw a different error. It seems it's hard to say without seeing all the code and testing it out. I'll post the example I made to try to recreate your issue here:

Default.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.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:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
There is nothing in Default.master.cs.

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Default.master" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>

<asp:Content ID="c" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:login ID="Login1" runat="server" OnAuthenticate="OnAuthenticate"></asp:login>
</asp:Content>
Default.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class Default_aspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
Response.Write(Login1.UserName);
}
}


Compare those pages to your own, if that doesn't work I'd have to see some more to test out.

Good luck;

Shaitan00
08-10-2008, 12:53 AM
Thanks to your example I was able to find out where the problem is created, but I still have not found a solution... If I make my Default.aspx the same as the sample you provided it works fine, so the problem is caused by my way of doing it (as shown below):

Default.aspx

<div class="page" id="home">
<div id="sidebar">
<asp:loginview id="LoginArea" runat="server">
<AnonymousTemplate>
<asp:login id="Login1" OnAuthenticate="OnAuthenticate" OnLoggedIn="OnLoggedIn" DestinationPageUrl="~/Clients.aspx" runat="server">
<layouttemplate>
<div class="login">
<h4>Login to Site</h4>
<asp:label runat="server" id="UserNameLabel" CssClass="label" associatedcontrolid="UserName">Card Number</asp:label>
<asp:textbox runat="server" id="UserName" cssclass="textbox" accesskey="u" />
<asp:requiredfieldvalidator runat="server" id="UserNameRequired" controltovalidate="UserName" validationgroup="Login1" errormessage="User Name is required." tooltip="User Name is required." >*</asp:requiredfieldvalidator>
<asp:label runat="server" id="PasswordLabel" CssClass="label" associatedcontrolid="Password">Password</asp:label>
<asp:textbox runat="server" id="Password" textmode="Password" cssclass="textbox" accesskey="p" />
<asp:requiredfieldvalidator runat="server" id="PasswordRequired" controltovalidate="Password" validationgroup="Login1" tooltip="Password is required." >*</asp:requiredfieldvalidator>
<div>
<asp:checkbox runat="server" id="RememberMe" text="Remember me next time"/>
</div>
<asp:imagebutton runat="server" id="LoginButton" CommandName="Login" AlternateText="login" skinid="login" CssClass="button"/>
<p><asp:literal runat="server" id="FailureText" enableviewstate="False"></asp:literal></p>
</div>
</layouttemplate>
</asp:login>
</AnonymousTemplate>
<LoggedInTemplate>
<h4><asp:loginname id="LoginName1" runat="server" formatstring="Welcome {0}!" /></h4>
</LoggedInTemplate>
</asp:loginview>
<hr />
</div>
</div>


Any clues what I am doing wrong?
Thanks,

Brandoe85
08-11-2008, 04:24 PM
Ok-

I see how you're overriding the login control + view, try to do a typecast to get the reference:

protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
Login login = (Login)sender;
Response.Write(login.UserName);
}