tags:

views:

176

answers:

2
+1  Q: 

Forms Authenticate

HI , i must create an application with a Forms Authentication in WPF, so i create a database(UserId,Username,Password,PasswordQuestion,PassworAnswer) and store data with Linq ToSQL but i don't know the right procedure how to create LoginForm to authenticate the username and password with Linq ToSQl and set up the roles for each user. Do you have any idea or advice to do it right?

Thanks

Bye

A: 

I think you should look into creating a roles table first.

With that said, your LINQ query would look something like this:

var result = 
  (
    from c in dataContext where username == Username and password == Password 
    select c
  )

Very vague question, though. If you can clarify, you'll get better results.

Ian P
Hi lan P,infact i'm a bit confuse about the procedure to authenticate the username and password in WPF.My purpose is to create a forms Authentication with Roles fer each user but i did not find out some a good example on internet.Do you have a good example to show me about my idea?Thanksa lot!!!
JayJay
A: 

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();
        }

    }
}

THat's an example of my code but easch time that i will authenticate with a wrong password(different from the database) its doesn't recognise the error and validate each password.

Do you have some idea about that?

Thanks

JayJay