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