NT权限认证
主页面
If Not Request.QueryString("status") Is Nothing Then
If Request.QueryString("status").ToString = "nodata" Then
My.Comm.MyObj.ShowOtherMessage(Me, "找不到人事資料!")
ElseIf Request.QueryString("status").ToString = "relogin" Then
Me.txtAccount.Text = ""
Me.txtPassword.Text = ""
Me.ddlDomain.SelectedIndex = 0
End If
Else
If (Request.ServerVariables("LOGON_USER").Split("\")(0) IsNot Nothing) And (Request.ServerVariables("LOGON_USER").Split("\")(1) IsNot Nothing) Then
Dim logonid, domain As String
logonid = Request.ServerVariables("LOGON_USER").Split("\")(1).Trim().ToLower()
domain = Request.ServerVariables("LOGON_USER").Split("\")(0).Trim().ToUpper()
Response.Cookies("logonid").Value = logonid
Response.Cookies("logonid").Expires = Now.AddMonths(1)
Response.Cookies("domain").Value = domain
Response.Cookies("domain").Expires = Now.AddMonths(1)
SetCookie(logonid, domain)
FormsAuthentication.SetAuthCookie(logonid, False)
'1.若user直接輸入非預設頁的URL,則因為Form認證,而將他導到loginURL,驗證通過後,再直接導到他當時輸入的網址。
'2.若在Web.config中有設定defaultUrl,則當user直接進入登入頁面(Default.aspx)並通過驗證後,則會被導到defaultURL。
'3.要在Web.config中設定好正確的loginURL與defaultURL,如此才不會造成無窮迴圈redirect到Default.aspx網頁的問題
'4.Else裡面的程式碼,只是要防止沒有設定defaultURL時的情況
If FormsAuthentication.GetRedirectUrl(logonid, False) <> "" Then
FormsAuthentication.RedirectFromLoginPage(logonid, False)
Else
'Response.Redirect("~/WebForm/Main.aspx") '先不用Master Page
Response.Redirect("Main.aspx")
End If
Else
Response.Redirect("~/WebForm/Logout.aspx")
End If
--------------------------------
Logout.aspx:
button
namespace Imports System.DirectoryServices
Public Function CheckNTAccount(ByVal UserName As String, ByVal Password As String _
, ByVal Domain As String) As Boolean
Dim Ad As DirectoryEntry
Dim ds As DirectorySearcher
' <add key="LDAP" value="LDAP://corpnet.auo.com/DC=corpnet,DC=auo,DC=com"/>
Dim strLDAP As String = ConfigurationSettings.AppSettings("LDAP")
Ad = New DirectoryEntry(strLDAP, Domain & "\" & UserName, Password, AuthenticationTypes.ServerBind)
Try
Dim obj As Object = Ad.NativeObject '檢查是否可正常登入
Return True
Catch ex As Exception
Return False
Finally
Ad.Dispose()
End Try
End Function

浙公网安备 33010602011771号