I am using windows username and password to authenticate user for login
following is my code

namespace Test1
{
    public partial class Login_Form : Form
    {
        string emp_code = "";
        public static string temp_emp_code = "";
        string s = WindowsIdentity.GetCurrent().Name.ToString();

        private string _path;
        private string _filterAttribute;

        public Login_Form(string path)
        {
            
            _path = path;
        }
        public Login_Form()
        {
            InitializeComponent();
        }

        private void Login_Form_Load(object sender, EventArgs e)
        {

        }

        //Methid for user authenticating user 
        public bool IsAuthenticated(string domain, string username, string pwd)
        {
            
            
            String domainAndUsername = s;

            DirectoryEntry entry = new DirectoryEntry(_path, s, pwd);
            
            try
            {
                object obj = entry.NativeObject;
                DirectorySearcher searcher = new DirectorySearcher(entry);
                searcher.Filter = "(SAMAccountName=" + username + ")";
                //searcher.Filter = username;
                searcher.PropertiesToLoad.Add("cn");
                SearchResult result = searcher.FindOne();
                if (null == result)
                {
                    return false;
                }
                _path = result.Path;
                _filterAttribute = (string)result.Properties["cn"][0];


            }
            catch (Exception e)
            {
                throw new Exception("Error authenticating user. " + e.Message);

            }

            return true;
        }//IsAuthenticated(String domain, String username, String pwd)

        
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            emp_code = Emp_codetextBox.Text.ToUpper();
            String Username_with_domain = "ACE" + @"\" + emp_code;

            bool authenticate = true;
            string pwd = textBox1.Text;
            String ad_path = "ace.com";
            String domain_name = "ace.com";
            Login_Form login = new Login_Form(ad_path);
            try
            {

                //if (authenticate == login.IsAuthenticated(domain_name, Username_with_domain, pwd))
                authenticate = login.IsAuthenticated(domain_name, Username_with_domain, pwd);[B]Getting exception at this point[/B]
                if(authenticate == true)
                {
                    frm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Invalid UserName / Password ! Type Again");
                    Emp_codetextBox.Text = "";
                    textBox1.Text = "";
                    return;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error authenticating " + ex.Message);
            }
            

        } //button click

        public string Empcode()
        {

            return temp_emp_code;
        }

        private void Emp_codetextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            object o = null;
            EventArgs e1 = null;
            if (e.KeyChar == (char)13)  //(char)13 means if the user press Enter key
            {
                button1_Click(o, e1);
            }
        }




    }
}

I am not getting what is wrong in my code

What errors do you get ?

If you just want to verify that the username entered is the same as the Windows authentication username do something like:

string win_Name = WindowsIdentity.GetCurrent().Name.ToString();

and then compare this with the username entered by the user has entered, or if you have a database witht he Windows usernames in you can use the line above and just compare it with the username in the database.

Thanks chris
But i also want to authenticate with Password
It gives exception on the following line

authenticate = login.IsAuthenticated(domain_name, Username_with_domain, pwd);

from above code

arr i can't help you with that one sorry i couldn't get the password part working when i did my program for university sorry.

Edited 5 Years Ago by ChrisHunter: n/a

Thanks for giving your valuable time.
I need to find something as early as possible before my deadline for this project

This article has been dead for over six months. Start a new discussion instead.