tags:

views:

110

answers:

4

Please tell me how to build Windows form Login .(not ASP.net) . This login Should validate using not only windows Authentication method but also Database user.

How validate User name and password. I used this type of Query for it

SELECT COUNT(*) FROM [Table Name] WHERE UserName = @username AND Password = @password;

If user avalable count >0 Above part handle code side.

using int x = Int32.Parse(commandObject.ExecuteScalar().ToString());
if(x > 0}
    **log**
else
    **fail**

Please tell me how Can i do it professional and correct way?

+2  A: 

Since you have the SQL already, all you need is a basic form, there's nothing special about a login form except that you hide the password, you can do that using the TextBox.PasswordChar property.

ho1
Actually, some people feel that hiding the password is Not A Good Thing, although this has a greater imapct on a web site than on desktop application. http://www.useit.com/alertbox/passwords.html
mickeyf
@mickeyf: I thought that was mostly when setting/changing the password, rather than when you're just logging in.
ho1
@ho1 - In either case the trade-off is "am I more concerned about who's looking over my shoulder, or 'drat, I can't see whether I fat-fingered that or not'". Note that I'm not taking sides, merely pointing out the controversy.
mickeyf
A: 

What do you mean by "Windows Authentication"? You could read the logged in User from the Environment and prefill the user-textfield with this value.

tbUserName = Environment.UserName;

After the User has entered his password, your setup dictates what to do. Is the Password Database Hashed (MD5, SHA1) or Verbatim? If so, hash the entered Password - or don't if it isn't.

You could use those two strings to select entities from your DataSource using the technology of your preference (e.g. ADO.Net). If no results are returned, the login was faulty. Remember: That way you won't be able to tell whether just the password or the user or both were wrong.

Sebastian Edelmeier
A: 

"Professional and correct way"

This can depend on what you application does, do you allow any kinda content to be seen or the first think should be login?

If yes:

  • Create a Window Form with your login screen
  • upon "login" button is clicked, check the sql the way you want
  • if ok, close that window and do a form.Show() the main window of your application
  • else, just give a good reason that you don't allow the user to proceed

if windows authentication, you need to check if that windows user can access it, do the same procedure but you do act upon formLogin.Active for example, and ask for credentials if that Windows User is not a valid user and do the above process again.

As an example, SuperOffice CRM 6 Application for windows, have this login form:

alt text

I hope it helps to get an idea.

If you were asking about code only, then you already have good answers.

balexandre
A: 

Just a thought... Have your login as the first screen your user visits. If they pass, then show the main app form and hide the login. If they fail, simply display a message stating that. It's not like web apps where a user can type in a url and try to get to any page they want to. You have FULL CONTROL :)

Jeremy