Click here to Skip to main content
14,770,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello All,
I have created a windows form Application in C#.
Now I want to add authentication to this application by means of creating a login panel just before the application is launched.
I want to create such a login panel which ask user for his windows username and password and check whether he has entered the right password.
He should enter the Domain and Username in DOMAIN\USERNAME way and then his password.

Has anybody created such kind of application?

Please share the code for me to start.

I want to create a login form before application runs. This Login panel asks for windows username and windows password.

Thanks and Regards,
Updated 19-May-11 2:58am

Try this Codeproject article to see if it will help.

User Login For WinForm Applications[^]

And this semi-related tip/trick may also be applicable to your needs:

Multiple Subsequent "Main" Forms in C# Apps[^]

BTW, there's no such thing as "sso" with rerspect to winforms (at least not in the normal context of the term).
rahul_raut 19-May-11 7:32am
Tried the First one but it is giving some errors on my machine :(
#realJSOP 19-May-11 7:57am
You're a programmer - run it under the debugger and see what it's doing that's "giving you errors".
IF you want to create a single sign on application, you don't need to be showing a logon form - that defeats the purpose of SSO!

Instead, you should be using the WindowsPrincipal[^] and WindowsIdentity [^] objects to retrieve the details of the current logged on user.


WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
Thread.CurrentPrincipal = principal;

if (principal.IsInRole("Administrator")
    // Do some admin work...

You can then enable application areas based on the users group permissions
rahul_raut 19-May-11 7:35am
That seems correct. I will try. Also I will remove the sso tag :)
ZeeroC00l 19-May-11 7:52am
But what if a Normal user wants to Use the same application ? Its better to authenticate all the users rather to do it just for Admin. Is it not ?
Dylan Morley 19-May-11 8:01am
That's just an example of how to programmatically use the group membership functionality.
I would suggest you make use of PrincipalContext provided by the System.DirectoryServices.AccountManagement namespace.

using(PrincipalContext principalCntxt = new PrincipalContext(ContextType.Domain, "DOMAIN_NAME"))
    bool isValid = principalCntxt.ValidateCredentials("DOMAIN_NAME\\USER", "PASSWORD")

rahul_raut 19-May-11 8:11am
This seems a good option.
I am not a real programmer like you guys. I am QA. :)
Can you provide me some details how to use the same?
How to check the text value in username and password boxes.
ZeeroC00l 19-May-11 8:30am
Can you be a little more clear about what this means :
--"Can you provide me some details how to use the same? How to check the text value in username and password boxes. "

Get the username and password from the text box and replace it instead of the DOMAIN_NAME\\USER and PASSWORD. Thats all you need to do.
rahul_raut 20-May-11 3:21am
I tried this way but the isValid value is always true.
can you help a little?
Use Session Management.

this[^] might help you.
#realJSOP 19-May-11 7:25am
This is NOT for a web page (according to the tags on the question).
rahul_raut 19-May-11 7:28am
Does that mean I need to remove the tag SSO? I am not clear.
rahul_raut 19-May-11 7:27am
Thanks! I will take a look. Let me know if there is any readily made application :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900