开发者

how to login to one admin account from existing multiple admin accounts in windows, based on credentails using c#.net code

My Problem is iam created three accounts under admin in my system(all three create under systm admin).Those are Account1:admin1 Account2:TestUser1 Account3:TestUser2. Now iam login from each account based on that acco开发者_开发百科unt credentails using c# code and run my c# application. The Problem is the application supports all thee accounts credentails when iam login from each account.Forexample Now iam login from TestUser1 it is working well by testuser1 credentails ,but it also suport admin1,TestUser2 credentails. My problem is i want login based on that account credentails only not from other two. iam working on c#windows application. This application Creating for Shift system for multiple users login and working from their own accounts. Hello Anybody help me plz.. on this problem.

My login code is

public partial class FrmLogIn : Form {

[DllImport("ADVAPI32.dll", EntryPoint = "LogonUserW", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);


public FrmLogIn()
{
  InitializeComponent();
}
private void FrmLogIn_Load(object sender, EventArgs e)
{
  foreach (Control ctl in pnlLogin.Controls)
  {
    if (ctl.TabIndex == 1)
      ctl.Focus();
  }
}

private void btnOK_Click(object sender,System. EventArgs e)
{
  toolStripStatusLabel1.Text = this.showstatus(" Login plz wait Verify the credentails...");
  System.Threading.Thread.Sleep(4000);
  string domainName = GetDomainName(txtUserName.Text); // Extract domain name form provide DomainUsername e.g Domainname\Username
  string userName = GetUsername(txtUserName.Text); // Extract user name from provided DomainUsername e.g Domainname\Username
  IntPtr token = IntPtr.Zero;


  bool result = LogonUser(userName, domainName, txtPassword.Text, 2, 0, ref token);
  if (result)
  {

    MessageBox.Show("LOGIN SUCCESSFULLY ");

    form2 obj = new form2();                   
    obj.Show();        
    this.Hide();
  }
    public static string GetDomainName(string usernameDomain)
{
  if (string.IsNullOrEmpty(usernameDomain))
  {
    throw (new ArgumentException("Argument can't be null.", "usernameDomain"));
  }
  if (usernameDomain.Contains("\\"))
  {
    int index = usernameDomain.IndexOf("\\");
    return usernameDomain.Substring(0, index);
  }
  else if (usernameDomain.Contains("@"))
  {
    int index = usernameDomain.IndexOf("@");
    return usernameDomain.Substring(index + 1);
  }
  else
  {
    return "";
  }
}
public static string GetUsername(string usernameDomain)
{
  if (string.IsNullOrEmpty(usernameDomain))
  {
    throw (new ArgumentException("Argument can't be null.", "usernameDomain"));
  }
  if (usernameDomain.Contains("\\"))
  {
    int index = usernameDomain.IndexOf("\\");
    return usernameDomain.Substring(index + 1);
  }
  else if (usernameDomain.Contains("@"))
  {
    int index = usernameDomain.IndexOf("@");
    return usernameDomain.Substring(0, index);
  }
  else
  {
    return usernameDomain;
  }
}


Use the Impersonator class found here.

It's excellent. He has wrapped LogonUser for you in a nice clean way.

Generally you write code like this:

using (Impersonator impersonator = new Impersonator("administrator", "password"))
{
    //Put your code under another user here.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜