Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 3.5 Security, Membership, and Role Management ISBN: 978-0-470-37930-1
This is the forum to discuss the Wrox book Professional ASP.NET 3.5 Security, Membership, and Role Management with C# and VB by Bilal Haidar; ISBN: 9780470379301
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 3.5 Security, Membership, and Role Management ISBN: 978-0-470-37930-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 25th, 2012, 10:31 AM
Raj Raj is offline
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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);

}


}
}

Last edited by Raj; January 25th, 2012 at 10:33 AM.
Reply With Quote
  #2 (permalink)  
Old April 6th, 2012, 04:21 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Login Control and create user wizard is not working in hosting space ramesh ASP.NET 4 General Discussion 2 February 2nd, 2012 11:07 AM
Login failed for user 'username'. stupid ASP.NET 2.0 Basics 4 January 10th, 2008 07:23 AM
Login process without username! AristotleYu SQL Server 2005 2 October 19th, 2006 01:42 AM
Login with username and password morpheus Classic ASP Basics 4 November 19th, 2003 10:54 AM



All times are GMT -4. The time now is 05:10 AM.


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