p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Professional ASP.NET 3.5 Security, Membership, and Role Management ISBN: 978-0-470-37930-1 (http://p2p.wrox.com/book-professional-asp-net-3-5-security-membership-role-management-isbn-978-0-470-37930-1-429/)
-   -   Login control with username dropdown not working (http://p2p.wrox.com/book-professional-asp-net-3-5-security-membership-role-management-isbn-978-0-470-37930-1/86459-login-control-username-dropdown-not-working.html)

Raj January 25th, 2012 10:31 AM

Login control with username dropdown not working
 
Hi,
I need a dropdown list of usernames in login control to authenticate users. I followed your webpage
http://imar.spaanjaars.com/409/chang...-list-of-users
However, for some reason, the login control still seems to be expecting a username entry rather than getting the selected value from dropdown.
I also tried to display (using response.write) the selected dropdown value in logingIn event...but no message appears.

Here is my code. Please help!
(BTW I am using a SQLdatasource to populate the dropdown with usernames instead of objectdatasource.)

Code:

<%@ Page Title="Login" Language="C#" MasterPageFile="~/Welcome.Master" AutoEventWireup="true"
 CodeBehind="Login.aspx.cs" Inherits="devhub.Login" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 
    <table>
       
        <tr>
            <td>
              <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate" DisplayRememberMe="false" >
                  <LayoutTemplate>
                      <table border="0" cellpadding="1" cellspacing="0"
                          style="border-collapse:collapse;">
                          <tr>
                              <td>
                                  <table border="0" cellpadding="0">
                                      <tr>
                                          <td align="center" colspan="2">
                                              Log In</td>
                                      </tr>
                                      <tr>
                                          <td align="right">
                                              <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                          </td>
                                          <td>
                                              <asp:TextBox ID="UserName" runat="server" Visible="false"></asp:TextBox>
                                              <asp:DropDownList ID="ddlUsername" runat="server" DataSourceID="SqlDataSource1"
                                                  DataTextField="Full_Name" DataValueField="ID" />
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="right">
                                              <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                          </td>
                                          <td>
                                              <asp:TextBox ID="Password" runat="server" TextMode="SingleLine"></asp:TextBox>
                                              <asp:RequiredFieldValidator ID="PasswordRequired" runat="server"
                                                  ControlToValidate="Password" ErrorMessage="Password is required."
                                                  ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="center" colspan="2" style="color:Red;">
                                              <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="right" colspan="2">
                                              <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In"
                                                  ValidationGroup="Login1" />
                                          </td>
                                      </tr>
                                  </table>
                              </td>
                          </tr>
                      </table>
                  </LayoutTemplate>
                </asp:Login> 
            </td>
        </tr>
    </table>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:HUBConnectionString %>"
        SelectCommand="SELECT [Full_Name], [ID] FROM [REDMs]"></asp:SqlDataSource>
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;

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

}

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Boolean bauthenticated = false;

bauthenticated = isValidUser(Login1.UserName, Login1.Password);
if (bauthenticated)
{
e.Authenticated = true;
}
else
{
e.Authenticated = false;

}
}
private Boolean isValidUser(string username, string password)
{
//Validate username and password
SqlConnection conn;
string connectionString = ConfigurationManager.ConnectionStrings["HUBConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
SqlCommand comm;
comm = new SqlCommand("SELECT Full_Name,Password,Position FROM REDMs WHERE Full_Name=@Username AND Password=@Password", conn);
comm.Parameters.AddWithValue("@Username", username);
comm.Parameters.AddWithValue("@Password", password);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
conn.Open();
da.Fill(dt);
conn.Close();
if (dt.Rows.Count == 0)
{
dt.Dispose();
return false;
}
//FormsAuthentication.RedirectFromLoginPage(Login1.U serName, false);
return true;

}
protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
Login1.UserName = ((DropDownList)
Login1.FindControl("ddlUsername")).SelectedValue;
Response.Write(""+Login1.UserName);

}


}
}

akonlaura April 6th, 2012 04:21 PM

you should check it again step by step because in my view you did well but if the result not shown you can recheck with highly focus then you got the result.










fort st john


All times are GMT -4. The time now is 08:18 AM.

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.