2011-08-09 17 views
0

私の問題は 私のシステムの管理下に3つのアカウントを作成しました(すべて3つは、システム管理者の下で作成する)です。これらのアカウントは、Windowsの既存の複数の管理者アカウントから1つの管理者アカウントにログインする方法です。アカウント1:管理者 これで、アカウントのクレデンシャルに基づいて各アカウントからiamがc#コードを使用してログインし、C#アプリケーションを実行します。 問題は、各アカウントのiamログイン時にアプリケーションがすべてのアカウントのクレデンシャルをサポートすることです。例:TestUser1からのiamログインは、testuser1クレデンシャルによってうまく機能しますが、admin1、TestUser2クレデンシャルもサポートします。 私の問題は、そのアカウントのクレデンシャルのみに基づいてログインをしたいのです。 iamがC#windowsアプリケーションで作業しています。 このアプリケーション複数のユーザーのログインと自分のアカウントからの作業のためのシフトシステムの作成。 こんにちは誰も私はこの問題でPLZを助ける。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. 
} 
関連する問題