none
ValidateUser.... RRS feed

  • Question

  • Hi,
    i'm trying to work out  an authenticate forms with a username and password in Wpf with Linq ToSql to store data from database but till now i couldn't it  right...my purpose is to create a software with a Forms Authentication to access it and after create a Role for each user(after i will give a look to it).
    public partial class Window1 : Window 
        { 
            public Window1() 
            { 
                InitializeComponent(); 
          
            } 
     
     
            public bool ValidateApplicationUser(string userName, string password) 
            { 
                bool validUser = false
                try 
                { 
                    var conn = "Data Source=MAMMA-PC/SQLMAMMA;Initial Catalog=MyWebSite;Integrated Security=True"
                    DataClasses1DataContext dc = new DataClasses1DataContext(conn); 
                    Table<User> users = dc.GetTable<User>(); 
     
                    var query = from c in dc.Users 
                                where (c.Username == userName.ToLower() && c.Password == password.ToLower()) 
                                select c; 
     
     
                    validUser = (query != null); 
                     
                } 
                catch (Exception ex) 
                { 
                    if (ex != null
                    { 
                    } 
                } 
     
                return validUser; 
            } 
     
            private void mahhh(object sender, RoutedEventArgs e) 
            { 
     
                bool authenticated = true
                var conn = "Data Source=MAMMA-PC/SQLMAMMA;Initial Catalog=MyWebSite;Integrated Security=True"
                    DataClasses1DataContext dc = new DataClasses1DataContext(conn); 
                    Table<User> users = dc.GetTable<User>(); 
     
                    var query = from c in dc.Users 
                                where (c.Username == usernameTextBox.Text && c.Password == passwordTextBox.Text) 
                                select c; 
     
             
                { 
                    if (usernameTextBox.Text != "" && passwordTextBox.Text != ""
                    { 
                        authenticated = ValidateApplicationUser(usernameTextBox.Text , passwordTextBox.Text); 
                    } 
                    
                } 
                if (!authenticated) 
                { 
                    MessageBox.Show("Invalid login. Try again."); 
                } 
                else 
                { 
                    MessageBox.Show("Congradulations! You're a valid user!"); 
     
                   
                    Window2 c = new Window2(); 
                    c.ShowDialog(); 
                    this.Close(); 
                } 
     
            } 
        } 
    }

    This is my code that use to validate the username and password but it validate also worng password and username different from the database.

    Do you have any idea or correct me if there is an error in the code.

    I wish you happiness.

    Bye

    Friday, February 13, 2009 5:03 PM

Answers

  •  The line

                    validUser = (query != null); 

    is incorrect as query is just that - a query - so will not be null. Specifically you want to check the results of the query so try changing this line to:

                    validUser = (query.Count() > 0); 

    Which will run the query and make sure there was at least one match. Alternatively if you actually want the user back:

                    var user = query.FirstOrDefault()
                    validUser = (user != null); 
    Regards,

    [)amien
    Friday, March 6, 2009 9:51 PM
    Moderator