I will be honest, this issue is madning time wasting and makes me want to throw .net out of the window.
I have been developing in .net for about 5 years and consider myself an advanced user, I did classic asp for 5 years before that. I have spent all weekend just trying to configure these login controls to work, when I could have written the whole thing in
classic in about a day.
I have the LoginView > AnonymousTemplate > Login control hierarchy as below...
My users fall into several different roles and have different home pages. Thus when they login, I need to redirect to a different folder/page. To do this I added the OnLoggedIn="Login1_LoggedIn" event handler to the <asp:Login > control as below...
The problem is in the code behind, I cannot access the username from the event handler code (protected void Login1_LoggedIn(object sender, EventArgs e)). Remember Login1.UserName does not work because the Login control lives in the LoginView and AnonymousTemplate
control. I have tried the following... I saw it in an earlier post, but username is not available on the loginControl variable.
Basically how can I redirect a user based on their role and the fact that the Login1 control is not accessible. I hope someone can help, otherwise .net might be losing another friend.
For the LoginView control, when being added onto a page, at a certain time,
only one Template (anonymous or loggedIn ) is applied on the Control instance, so at that time, we can only retrieve the reference of those controls in the active template( can't access those in the non-active template).
So you can first determine whether the user has been authenticated or not and then use the LoginView.FindControl( stringId )
Here is an example and it works well, Hope it helps.
Best Regards
XiaoYong Dai
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
I got this issue resolved using the following. A friend showed my how to use Quick Watch to view the control hierarchy, and we determined the username, text control could be accessed by using the following
Your hierarchy may be different than mine so the code above may not work for you, but just use Quick Watch to find the username control in the control heirarchy and copy the path. Also you may have to replace private control names like _control[0] with Control[0].
public void Login1_LoggedIn(object sender, EventArgs e)
{
//Login loginControl = LoginView1.Controls[0].FindControl("Login1") as Login; string txtUsername = ((System.Web.UI.WebControls.TextBox)(LoginView1.Controls[0].Controls[1].Controls[0].Controls[1])).Text;
I am glad to hear you have straightened everything out.
Thank you for your advice. It is so helpful to us
Best Regards
XiaoYong Dai
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
I am sorry but this is ridiculous, how can you build a control like this and expect us to have to dig to find controls. It seems to me Microsoft was only thinking about the drag and drop crowed when they built this stuff. You have to have access to these controls
our the whole security metaphor is worthless! LoginView.RoleGroups.Role.LiteralControl. How did you use QuickWatch to find the control? can you expand on that?
Post your code and maybe I can help, I recently spent some time trying figure this thing out...
As for using quickwatch, I have been just setting a breakpoint, then mousing over the control, login for example. You can follow the path to the internal control doing this...
Thanks for this post -- I have been struggling with this exact same scenario using a login control in an anonymous template. I ran across the following on another forum and it works for me. Since the sender of the loggedin event is the login control, you
can cast the sender object to a login object and then access it to get the username.
RobertKGaude...
Member
202 Points
74 Posts
Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Apr 16, 2007 01:52 AM|LINK
I will be honest, this issue is madning time wasting and makes me want to throw .net out of the window.
I have been developing in .net for about 5 years and consider myself an advanced user, I did classic asp for 5 years before that. I have spent all weekend just trying to configure these login controls to work, when I could have written the whole thing in classic in about a day.
I have the LoginView > AnonymousTemplate > Login control hierarchy as below...
<asp:LoginView ID="LoginView1" Runat="server">
<AnonymousTemplate>
<asp:Login id="Login1" runat="server">
My users fall into several different roles and have different home pages. Thus when they login, I need to redirect to a different folder/page. To do this I added the OnLoggedIn="Login1_LoggedIn" event handler to the <asp:Login > control as below...
<asp:Login id="Login1" runat="server" OnLoggedIn="Login1_LoggedIn">
The problem is in the code behind, I cannot access the username from the event handler code (protected void Login1_LoggedIn(object sender, EventArgs e)). Remember Login1.UserName does not work because the Login control lives in the LoginView and AnonymousTemplate control. I have tried the following... I saw it in an earlier post, but username is not available on the loginControl variable.
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Login loginControl = (Login)LoginView1.Controls[0].Controls[1].FindControl("Login1");
TextBox txtUsername = (TextBox)loginControl.UserName;
if(Roles.IsUserInRole(txtUsername,"Candidate"))
{
this.Response.Redirect("~/Candidate/default.aspx");
}
else if(Roles.IsUserInRole(txtUsername,"Employer"))
{
this.Response.Redirect("~/Employer/default.aspx");
}
}
Basically how can I redirect a user based on their role and the fact that the Login1 control is not accessible. I hope someone can help, otherwise .net might be losing another friend.
username LoginView AnonymousTemplate Login controls destinationpageurl
Bayou Peeps Louisiana Friend Network
Help Me Move Moving?
XiaoYong Dai...
All-Star
38310 Points
4229 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Apr 17, 2007 11:07 AM|LINK
Hi
For the LoginView control, when being added onto a page, at a certain time, only one Template (anonymous or loggedIn ) is applied on the Control instance, so at that time, we can only retrieve the reference of those controls in the active template( can't access those in the non-active template).
So you can first determine whether the user has been authenticated or not and then use the LoginView.FindControl( stringId )
Here is an example and it works well, Hope it helps.
Login lg = (Login)LoginView1.Controls[0].Controls[0].FindControl("Login2");
TextBox tb = (TextBox)lg.FindControl("UserName");
tb.Text = "set username";
XiaoYong Dai
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
RobertKGaude...
Member
202 Points
74 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Apr 17, 2007 02:38 PM|LINK
I got this issue resolved using the following. A friend showed my how to use Quick Watch to view the control hierarchy, and we determined the username, text control could be accessed by using the following
--> string txtUsername = ((System.Web.UI.WebControls.TextBox)(LoginView1.Controls[0].Controls[1].Controls[0].Controls[1])).Text;. <--
Your hierarchy may be different than mine so the code above may not work for you, but just use Quick Watch to find the username control in the control heirarchy and copy the path. Also you may have to replace private control names like _control[0] with Control[0].
public void Login1_LoggedIn(object sender, EventArgs e)
{
//Login loginControl = LoginView1.Controls[0].FindControl("Login1") as Login;
string txtUsername = ((System.Web.UI.WebControls.TextBox)(LoginView1.Controls[0].Controls[1].Controls[0].Controls[1])).Text;
if(Roles.IsUserInRole(txtUsername, "Candidate"))
{
this.Response.Redirect("~/Candidate/default.aspx");
}
else if(Roles.IsUserInRole(txtUsername, "Employer"))
{
this.Response.Redirect("~/Employer/default.aspx");
}
}
Bayou Peeps Louisiana Friend Network
Help Me Move Moving?
XiaoYong Dai...
All-Star
38310 Points
4229 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Apr 18, 2007 01:38 AM|LINK
I am glad to hear you have straightened everything out.
Thank you for your advice. It is so helpful to us
XiaoYong Dai
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Suimple
Member
6 Points
3 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
May 04, 2007 11:31 PM|LINK
RobertKGaude...
Member
202 Points
74 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
May 04, 2007 11:56 PM|LINK
Post your code and maybe I can help, I recently spent some time trying figure this thing out...
As for using quickwatch, I have been just setting a breakpoint, then mousing over the control, login for example. You can follow the path to the internal control doing this...
Bayou Peeps Louisiana Friend Network
Help Me Move Moving?
jeffenglish
Member
2 Points
1 Post
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Dec 05, 2007 05:20 PM|LINK
Thanks for this post -- I have been struggling with this exact same scenario using a login control in an anonymous template. I ran across the following on another forum and it works for me. Since the sender of the loggedin event is the login control, you can cast the sender object to a login object and then access it to get the username.
protected void LoginControl_LoggedIn(object sender, EventArgs e)
{
Login loginControl = (Login)sender;
if (Roles.IsUserInRole(loginControl.UserName, "Administrator"))
{
Response.Redirect("~/Admin/default.aspx");
}
else
{
Response.Redirect("~/Users/default.aspx");
}
}
JustRun
Member
21 Points
51 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Mar 25, 2008 02:11 AM|LINK
I hope you find this useful, It worked with me. Beside I didn't like this mess about .Control[0].Control[1]...
this is much easier and it working perfectly.
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { Login Login1 = ((Login)LoginView1.FindControl("Login1")); string userName = Login1.UserName.ToString(); string password = Login1.Password.ToString(); if (Membership.ValidateUser(userName, password)) { if (Request.QueryString["ReturnUrl"] != null) { FormsAuthentication.RedirectFromLoginPage(userName, true); } else { FormsAuthentication.SetAuthCookie(userName, true); Response.Redirect("~/Default.aspx"); } } else { Label lblResults = (Label)Login1.FindControl("lblResults"); lblResults.Visible = true; lblResults.Text = "regarding that you have only 5 times to try"; if ((Membership.GetUser(userName) != null) && (Membership.GetUser(userName).IsLockedOut == true)) lblResults.Text = " <b>Your account has been locked out.</b>"; } };)
Dont forget to mark it as answer if it helps !
vonbondies
Member
48 Points
37 Posts
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
Apr 02, 2008 01:58 PM|LINK
Thanks JustRun!
Exactly what I was looking for!!
peterq
Member
2 Points
1 Post
Re: Cannot access username from LoginView > AnonymousTemplate > Login ... control.
May 08, 2008 05:06 AM|LINK
i have a function in my base page class. in this case I call:
Dim obj As Object = Me.FindControlDeep(Me, "UserName")
Protected Function FindControlDeep(ByRef Parent As Control, ByVal strId As String) As Object
Dim o As Object
o = Parent.FindControl(strId)
If o IsNot Nothing Then
Return o
End If
Dim oc As Control
For Each oc In Parent.Controls
o = FindControlDeep(oc, strId)
If o IsNot Nothing Then
Return o
End If
Next
Return Nothing
End Function