2011-08-09 88 views
0

從基於credentails的windows登錄到一個管理員帳戶的一個管理員帳戶我的問題是 iam在我的系統管理員下創建了三個帳戶(所有三個在systm管理下創建)。 Account1:admin1 Account2:TestUser1 Account3:TestUser2。 現在iam從每個帳戶登錄使用c#代碼基於該帳戶credentails並運行我的c#應用程序。 問題是,當iam從每個帳戶登錄時,應用程序支持所有帳戶credentails。示例現在,iam從TestUser1登錄,它通過testuser1 credentails運行良好,但它也支持admin1,TestUser2 credentails。 我的問題是我想登錄的基礎上,該帳戶credentails只有不是從其他兩個。我工作的C#窗口應用程序。 此應用程序爲多用戶創建Shift系統登錄和工作從他們自己的帳戶。 你好,任何人都可以幫我解決這個問題。如何使用c#.net代碼

我登錄的代碼是

公共部分類FrmLogIn:表 {

[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; 
    } 
} 

回答

1

使用冒領類中here

非常好。他以一種非常乾淨的方式爲您包裝了LogonUser。

一般來說,你這樣寫代碼:

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