C# B/S 使用域用戶驗證

第一種方便安全的方法, 

通過配置web config來拿到域信息:
在web.config中設置<authentication   mode="Windows"/>   
在IIS-默認的Web站點-虛擬目錄-屬性-目錄安全性-編輯中選擇   "集成Windows驗證",注意"匿名訪問"一定不要選   
在asp.net中使用   User.Identity.Name   就可以得到域名和用戶名.


第二種仍然使用程序驗證,在域控服務器編寫域用戶驗證代碼(我這裏使用WCF 服務供客戶端調用 服務代碼省略不寫了,只寫驗證代碼)如下


[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern int LogonUser(String lpszUserName,
                                          String lpszDomain,
                                          String lpszPassword,
                                          int dwLogonType,
                                          int dwLogonProvider,
                                          ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        public extern static int DuplicateToken(IntPtr hToken,
                                          int impersonationLevel,
                                          ref IntPtr hNewToken);
        /**/
        /// <summary>  
        /// 輸入用戶名、密碼、登錄域判斷是否成功  
        /// </summary>  
        /// <example>  
        /// if (impersonateValidUser(UserName, Domain, Password)){}  
        /// </example>  
        /// <param name="userName">賬戶名稱,如:string UserName = UserNameTextBox.Text;</param>  
        /// <param name="domain">要登錄的域,如:string Domain   = DomainTextBox.Text;</param>  
        /// <param name="password">賬戶密碼, 如:string Password = PasswordTextBox.Text;</param>  
        /// <returns>成功返回true,否則返回false</returns>  
        public static bool ImpersonateValidUser(String userName, String domain, String password)
        {
            //類型必須爲3 ,其他 9或者2 都不行的哦,具體含義參考
            int LOGON32_LOGON_INTERACTIVE = 3;

            int LOGON32_PROVIDER_DEFAULT = 0; 
            WindowsImpersonationContext impersonationContext;
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero; 
            if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }

其中登錄類型的含義如下

enum LogonType : uint
    {
        /// <summary>
        /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on by a terminal server, remote shell, or similar process. This logon type has the additional expense of caching logon information for disconnected operations; therefore, it is inappropriate for some client/server applications, such as a mail server.
        /// </summary>
        Interactive = 2,
        /// <summary>
        /// This logon type is intended for high performance servers to authenticate plaintext passwords. The LogonUser function does not cache credentials for this logon type.
        /// </summary>
        Network = 3,
        /// <summary>
        /// This logon type is intended for batch servers, where processes may be executing on behalf of a user without their direct intervention. This type is also for higher performance servers that process many plaintext authentication attempts at a time, such as mail or Web servers. The LogonUser function does not cache credentials for this logon type.
        /// </summary>
        Batch = 4,
        /// <summary>
        /// Indicates a service-type logon. The account provided must have the service privilege enabled.
        /// </summary>
        Service = 5,
        /// <summary>
        /// This logon type is for GINA DLLs that log on users who will be interactively using the computer. This logon type can generate a unique audit record that shows when the workstation was unlocked.
        /// </summary>
        Unlock = 7,
        /// <summary>
        /// This logon type preserves the name and password in the authentication package, which allows the server to make connections to other network servers while impersonating the client. A server can accept plaintext credentials from a client, call LogonUser, verify that the user can access the system across the network, and still communicate with other servers.
        /// </summary>
        NetworkClearText = 8,
        /// <summary>
        /// This logon type allows the caller to clone its current token and specify new credentials for outbound connections. The new logon session has the same local identifier but uses different credentials for other network connections.<br/>
        /// This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
        /// </summary>
        NewCredentials = 9
    }
 
    /// <summary>
    /// Specifies the logon provider.
    /// </summary>
    enum LogonProvider : uint
    {
        /// <summary>
        /// Use the standard logon provider for the system.<br/>
        /// The default security provider is negotiate, unless you pass NULL for the domain name and the user name is not in UPN format. In this case, the default provider is NTLM.
        /// </summary>
        Default = 0,
        /// <summary>
        /// Use the Windows NT 3.5 logon provider.
        /// </summary>
        WinNT35 = 1,
        /// <summary>
        /// Use the NTLM logon provider.
        /// </summary>
        WinNT40 = 2,
        /// <summary>
        /// Use the negotiate logon provider.
        /// </summary>
        WinNT50 = 3,
    }
 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章