2011-03-16 61 views
0

我想製作一個Windows窗體應用程序,並希望使用Windows身份驗證登錄用戶,它必須在Intranet中使用。應用程序應接受來自用戶的用戶名和密碼,並應對其進行驗證。如何實現這一點。Dotnet:如何在窗體窗體應用程序中實現Windows身份驗證?

+2

這裏是一個[執行](http://geekswithblogs.net/Ramaraju/archive/2009/07/14/windows_authentication_in_winform_application .aspx) – 2011-03-16 06:13:23

+0

@ Sanjeev: - 我用你的想法。感謝您的幫助 – Prachur 2011-03-16 09:56:32

回答

2

您可以使用Interop Services實現此目的。使用下面的代碼。

[System.Runtime.InteropServices.DllImport("advapi32.dll")] 
    public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken); 

    public bool IsValidateCredentials(string userName, string password, string domain) 
    { 
     IntPtr tokenHandler = IntPtr.Zero; 
     bool isValid = LogonUser(userName, domain, password, 3, 0, ref tokenHandler); 
     return isValid; 
    } 
1

Environment.UserName爲您提供了當前用戶的用戶名。由於用戶已登錄到Windows,所以不需要密碼。

+1

Environment.Username爲我提供登錄的用戶名,但足以讓我登錄到我的應用程序?我自然需要將登錄的用戶與用戶表中的用戶進行匹配,並通過用戶名看起來很弱。如果某人在他的計算機上創建了一個與Intranet域名相同的本地域,並創建一個用戶名稱爲他想破解的名稱,該怎麼辦?所以他可以登錄任何用戶hew想要的。我錯了嗎? – 2011-11-30 09:46:48

相關問題