บทความที่มีการทีละขั้นตอนนี้อธิบายวิธี ASP.NET โปรแกรมประยุกต์สามารถใช้การรับรองความถูกต้องของฟอร์มเพื่ออนุญาตให้ผู้ใช้การรับรองความถูกต้องสำหรับ Active Directory โดยใช้การ Lightweight ไดเรกทอรีการเข้าถึงโพรโทคอล (LDAP) ได้
หลังจากที่ผู้ใช้ที่ได้รับรองความถูกต้อง และการเปลี่ยนเส้นทาง คุณสามารถใช้การ
Application_AuthenticateRequestวิธีการจัดเก็บแฟ้ม Global.asax เป็น
GenericPrincipalวัตถุในนั้น
HttpContext.Userคุณสมบัติ flows ตลอดการร้องขอ
สร้างแอพลิเคชันเว็บ ASP.NET ที่ใน Visual Basic .NET
ทำตามขั้นตอนเหล่านี้เพื่อสร้างโปรแกรมประยุกต์เว็บ ASP.NET ที่ใหม่ที่ชื่อว่า FormsAuthAd ใน Visual Basic .NET:
- เริ่ม Microsoft Visual Studio .NET
- ในการแฟ้ม:เมนู ให้ชี้ไปที่ใหม่แล้ว คลิกProject.
- คลิกโครงการ visual Basicภายใต้ชนิดโครงการแล้ว คลิกแอพลิเคชันเว็บ asp.netภายใต้แม่แบบ.
- ในการตำแหน่ง:กล่อง ชนิดhttp:// <servername> / FormsAuthAd </servername>(แทนที่http://localhostถ้าคุณกำลังใช้เซิร์ฟเวอร์ภายในเครื่อง (so เป็นไปได้http://localhost/FormsAuthAdแล้ว คลิกตกลง.
- คลิกขวาอ้างอิง:โหนดใน Explorer โซลูชัน แล้วคลิกเพิ่มการอ้างอิง.
- ในการ.NETแท็บในนั้นเพิ่มการอ้างอิงกล่องโต้ตอบ คลิกSystem.DirectoryServices.dllคลิกเลือกแล้ว คลิกตกลง.
เขียนรหัสการรับรองความถูกต้อง
ทำตามขั้นตอนเหล่านี้เพื่อสร้างแฟ้มการคลาใหม่ที่ชื่อ LdapAuthentication.vb:
- โซลูชัน Explorer คลิกขวาที่โหนโครงการ ชี้ไปที่addแล้ว คลิกเพิ่มรายการใหม่.
- คลิกคลาสภายใต้แม่แบบ.
- ประเภท:LdapAuthentication.vbในการชื่อ:กล่อง แล้วคลิกOPEN.
- แทนโค้ดที่มีอยู่ในแฟ้ม LdapAuthentication.vb ด้วยรหัสต่อไปนี้:
Imports System
Imports System.Text
Imports System.Collections
Imports System.DirectoryServices
Namespace FormsAuth
Public Class LdapAuthentication
Dim _path As String
Dim _filterAttribute As String
Public Sub New(ByVal path As String)
_path = path
End Sub
Public Function IsAuthenticated(ByVal domain As String, ByVal username As String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain & "\" & username
Dim entry As DirectoryEntry = New DirectoryEntry(_path, domainAndUsername, pwd)
Try
'Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As DirectorySearcher = New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" & username & ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If (result Is Nothing) Then
Return False
End If
'Update the new path to the user in the directory.
_path = result.Path
_filterAttribute = CType(result.Properties("cn")(0), String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " & ex.Message)
End Try
Return True
End Function
Public Function GetGroups() As String
Dim search As DirectorySearcher = New DirectorySearcher(_path)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As StringBuilder = New StringBuilder()
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberOf").Count
Dim dn As String
Dim equalsIndex, commaIndex
Dim propertyCounter As Integer
For propertyCounter = 0 To propertyCount - 1
dn = CType(result.Properties("memberOf")(propertyCounter), String)
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (equalsIndex = -1) Then
Return Nothing
End If
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1))
groupNames.Append("|")
Next
Catch ex As Exception
Throw New Exception("Error obtaining group names. " & ex.Message)
End Try
Return groupNames.ToString()
End Function
End Class
End Namespace
คำอธิบายของรหัสการขาย
รหัสการรับรองความถูกต้องยอมรับในโดเมน ชื่อผู้ใช้ รหัสผ่าน และเส้นทางไปยังแผนภูมิใน Active Directory รหัสนี้ใช้ตัวให้บริการ LDAP ไดเรกทอรี
การรับรองความถูกต้องของผู้ใช้
รหัสใน Logon.aspx หน้าการเรียกไป
LdapAuthentication.IsAuthenticatedวิธีการและผ่านไปในข้อมูลประจำตัวที่เก็บรวบรวมจากผู้ใช้ จาก นั้น คำ
DirectoryEntryวัตถุถูกสร้างขึ้น ด้วยเส้นทางไปยังแผนภูมิไดเรกทอรี ชื่อผู้ใช้ และรหัสผ่าน ชื่อผู้ใช้ต้องอยู่ในรูปแบบ "โดเมน\ชื่อ"
กระบวนการ
DirectoryEntryวัตถุแล้วพยายามบังคับ
AdsObjectการผูก โดยได้รับการ
NativeObjectคุณสมบัติ ถ้านี่สำเร็จ
cnคุณลักษณะสำหรับผู้ใช้จะได้รับ โดยการสร้างเป็น
DirectorySearcherวัตถุ และด้วยการกรองบน
SAMAccountName. หลังจากที่ผู้ใช้ที่ได้รับรองความถูกต้อง
IsAuthenticatedวิธีการส่งคืน
True.
หมายเหตุ:เมื่อคุณใช้ LDAP ผูกกับวัตถุที่เกี่ยวกับ Active Directory พอร์ต TCP จะถูกใช้งาน เพิ่มการใช้ LDAP ด้วย
System.DirectoryServicesnamespace อาจใช้พอร์ต TCP ทั้งหมดที่มีอยู่ คุณอาจสามารถลดการโหลด TCP โดยการเชื่อมต่อที่คุณใช้การรับรองความถูกต้องของผู้ใช้ของคุณที่นำมาใช้ใหม่
กลุ่มผู้ใช้
เมื่อต้องการดูรายการของกลุ่มผู้ใช้ที่อยู่ เรียกรหัสนี้จะ
LdapAuthentication.GetGroupsวิธีการ กระบวนการ
LdapAuthentication.GetGroupsวิธีการขอรับรายการของกลุ่มรักษาความปลอดภัยและการแจกจ่ายที่ผู้ใช้อยู่ โดยการสร้างเป็น
DirectorySearcherวัตถุ และการกรองข้อมูลตามด้วย
memberOfแอตทริบิวต์ วิธีนี้ส่งกลับรายการของกลุ่มที่คั่น ด้วย pipes (|)
สังเกตว่า การ
LdapAuthentication.GetGroupsวิธี manipulates และ truncates สตริงการ ซึ่งช่วยลดความยาวของสายอักขระที่ถูกเก็บอยู่ในคุกกี้การรับรองความถูกต้อง ถ้าสายอักขระไม่ถูกตัดทอน รูปแบบของแต่ละกลุ่มปรากฏเป็นดังนี้:
CN=...,...,DC=domain,DC=com
วิธีนี้สามารถสร้างสตริงที่มีความยาวมาก ถ้าความยาวของข้อความนี้มีค่ามากกว่าความยาวของคุกกี้ อาจไม่มีสร้างคุกกี้การรับรองความถูกต้อง ถ้าข้อความนี้อาจอาจจะมากกว่าความยาวของคุกกี้ คุณอาจต้องการเก็บข้อมูลกลุ่ม ในแคช ASP.NET วัตถุ หรือ ในฐานข้อมูล อีกวิธีหนึ่งคือ คุณอาจต้องการเข้ารหัสลับข้อมูลกลุ่ม และเก็บข้อมูลนี้ในเขตข้อมูลฟอร์มที่ซ่อนไว้
เขียนรหัส Global.asax
แสดงรหัสในแฟ้ม Global.asax ข้อ
Application_AuthenticateRequestตัวจัดการเหตุการณ์ ตัวจัดการเหตุการณ์นี้ดึงข้อมูลคุกกี้การรับรองความถูกต้องจากนั้น
Context.Request.Cookiesคอลเลกชัน decrypts คุกกี้ และดึงรายการของกลุ่มที่จะถูกเก็บไว้ในนั้น
FormsAuthenticationTicket.UserDataคุณสมบัติ กลุ่มปรากฏในรายชื่อที่คั่นไปป์ที่สร้างขึ้นในหน้า Logon.aspx
รหัสแยกวิเคราะห์สตริงที่ในอาร์เรย์เป็นสตริงที่สร้างเป็น
GenericPrincipalวัตถุ หลังจาก
GenericPrincipalวัตถุถูกสร้าง วางวัตถุนี้อยู่ในนั้น
HttpContext.Userคุณสมบัติ
- คลิกขวาใน Explorer โซลูชันGlobal.asaxแล้ว คลิกรหัสของมุมมอง.
- เพิ่มรหัสต่อไปนี้ที่ด้านบนของรหัส หลังแฟ้ม Global.asax.vb:
Imports System.Web.Security
Imports System.Security.Principal
- ตัวจัดการเหตุการณ์ที่ว่างที่มีอยู่สำหรับแทนApplication_AuthenticateRequestมีรหัสต่อไปนี้:
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
Dim cookieName As String = FormsAuthentication.FormsCookieName
Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
If (authCookie Is Nothing) Then
'There is no authentication cookie.
Return
End If
Dim authTicket As FormsAuthenticationTicket = Nothing
Try
authTicket = FormsAuthentication.Decrypt(authCookie.Value)
Catch ex As Exception
'Write the exception to the Event Log.
Return
End Try
If (authTicket Is Nothing) Then
'Cookie failed to decrypt.
Return
End If
'When the ticket was created, the UserData property was assigned a
'pipe-delimited string of group names.
Dim groups As String() = authTicket.UserData.Split(New Char() {"|"})
'Create an Identity.
Dim id As GenericIdentity = New GenericIdentity(authTicket.Name, "LdapAuthentication")
'This principal flows throughout the request.
Dim principal As GenericPrincipal = New GenericPrincipal(id, groups)
Context.User = principal
End Sub
ปรับเปลี่ยนแฟ้ม Web.config
ในส่วนนี้ คุณกำหนดค่านี้
แบบฟอร์มกระบวนการ
รับรองความถูกต้องและ
การอนุญาตองค์ประกอบในแฟ้ม Web.config ด้วยการเปลี่ยนแปลงเหล่านี้ เฉพาะ authenticated ผู้ใช้สามารถเข้าถึงแอพลิเคชัน และมีการเปลี่ยนเส้นทางไปยังเพ Logon.aspx unauthenticated การร้องขอ คุณสามารถปรับเปลี่ยนการตั้งค่าคอนฟิกนี้จะอนุญาตให้มีเฉพาะค่าที่แน่นอนผู้ใช้และกลุ่มการเข้าถึงแอพลิเคชัน
แทนโค้ดที่มีอยู่ในแฟ้ม Web.config ด้วยรหัสต่อไปนี้:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name="adAuthCookie" timeout="60" path="/" >
</forms>
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
<identity impersonate="true" />
</system.web>
</configuration>
คำประกาศนี้
รหัสประจำตัว impersonate = "true" /องค์ประกอบของการตั้งค่าคอนฟิก ซึ่งทำให้ ASP.NET เพื่อ impersonate แอคเคาท์มีการกำหนดค่าเป็นบัญชีที่ไม่ระบุชื่อจาก Microsoft ข้อมูลบริการทางอินเทอร์เน็ต (IIS) เป็นผลมาจากการกำหนดค่านี้ ร้องขอทั้งหมดไปยังโปรแกรมประยุกต์นี้ทำงานภายใต้บริบทการรักษาความปลอดภัยของบัญชีที่จัดโครงแบบ ผู้ใช้ให้ข้อมูลประจำตัวของการรับรองความถูกต้องสำหรับ Active Directory แต่บัญชีที่เข้าถึง Active Directory คือ บัญชีที่จัดโครงแบบ สำหรับข้อมูลเพิ่มเติม ให้ดู
อ้างอิง:ส่วน
การตั้งค่าคอนฟิก IIS สำหรับการรับรองความถูกต้องแบบไม่ระบุชื่อ
เมื่อต้องการตั้งค่าคอนฟิก IIS สำหรับการรับรองความถูกต้องแบบไม่ระบุชื่อ ดำเนินการดังต่อไปนี้:
- ในคอนโซลการจัดการบริการข้อมูลทางอินเทอร์เน็ต (IIS) คลิกขวาที่โหนไดเรกทอรีเสมือนสำหรับ"FormsAuthAd".
- คลิกการคุณสมบัติแล้ว คลิกการความปลอดภัยของไดเรกทอรีแท็บ
- คลิกแก้ไขภายใต้ควบคุมการเข้าถึงและการรับรองความถูกต้องแบบไม่ระบุชื่อ.
- เลือกการการเข้าถึงแบบไม่ระบุชื่อกล่องกาเครื่องหมาย
- ทำให้บัญชีที่ไม่ระบุชื่อสำหรับโปรแกรมประยุกต์ที่ใช้บัญชีที่มีสิทธิ์ในการ Active Directory
- คลิกเพื่อยกเลิกเลือกนั้นอนุญาตให้ IIS ไปยังตัวควบคุมรหัสผ่านกล่องกาเครื่องหมาย
ค่าเริ่มต้น IUSR_
computernameบัญชีไม่มีสิทธิ์ในการ Active Directory
สร้างเพ Logon.aspx
ทำตามขั้นตอนเหล่านี้เพื่อสร้างฟอร์มเว็บ ASP.NET ใหม่ซึ่งชื่อ Logon.aspx:
- โซลูชัน Explorer คลิกขวาที่โหนโครงการ ชี้ไปที่addแล้ว คลิกเพิ่มฟอร์มเว็บ.
- ประเภท:Logon.aspxในการชื่อ:กล่อง แล้วคลิกOPEN.
- คลิกขวาใน Explorer โซลูชันLogon.aspxแล้ว คลิกตัวออกแบบมุมมอง.
- คลิกการhtmlแท็บใน Designer
- แทนรหัสที่มีอยู่ ด้วยรหัสต่อไปนี้:
<%@ Page language="vb" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuthAd.FormsAuth" %>
<html>
<body>
<form id="Login" method="post" runat="server">
<asp:Label ID="Label1" Runat="server">Domain:</asp:Label>
<asp:TextBox ID="txtDomain" Runat="server"></asp:TextBox><br>
<asp:Label ID="Label2" Runat="server">Username:</asp:Label>
<asp:TextBox ID="txtUsername" Runat="server"></asp:TextBox><br>
<asp:Label ID="Label3" Runat="server">Password:</asp:Label>
<asp:TextBox ID="txtPassword" Runat="server" TextMode="Password"></asp:TextBox><br>
<asp:Button ID="btnLogin" Runat="server" Text="Login" OnClick="Login_Click"></asp:Button><br>
<asp:Label ID="errorLabel" Runat="server" ForeColor="#ff3300"></asp:Label><br>
<asp:CheckBox ID="chkPersist" Runat="server" Text="Persist Cookie" />
</form>
</body>
</html>
<script runat="server">
sub Login_Click(sender as object,e as EventArgs)
Dim adPath as String = "LDAP://DC=..,DC=.." 'Path to your LDAP directory server
Dim adAuth as LdapAuthentication = new LdapAuthentication(adPath)
try
if(true = adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text)) then
Dim groups as string = adAuth.GetGroups()
'Create the ticket, and add the groups.
Dim isCookiePersistent as boolean = chkPersist.Checked
Dim authTicket as FormsAuthenticationTicket = new FormsAuthenticationTicket(1, _
txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups)
'Encrypt the ticket.
Dim encryptedTicket as String = FormsAuthentication.Encrypt(authTicket)
'Create a cookie, and then add the encrypted ticket to the cookie as data.
Dim authCookie as HttpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
if(isCookiePersistent = true) then
authCookie.Expires = authTicket.Expiration
end if
'Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie)
'You can redirect now.
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false))
else
errorLabel.Text = "Authentication did not succeed. Check user name and password."
end if
catch ex as Exception
errorLabel.Text = "Error authenticating. " & ex.Message
end try
end sub
</script>
- ปรับเปลี่ยนเส้นทางในหน้า Logon.aspx ให้ชี้ไปยังเซิร์ฟเวอร์ LDAP ไดเรกทอรีของคุณ
หน้า Logon.aspx มีหน้าที่การเก็บรวบรวมข้อมูลจากผู้ใช้และการเรียกวิธีในการ
LdapAuthenticationคลาสที่ หลังจากรหัส authenticates ผู้ใช้ และรายการของกลุ่มการขอรับ รหัสทำต่อไปนี้ในใบสั่งนี้:
- สร้างคำFormsAuthenticationTicketวัตถุ
- บัตร การเข้ารหัส
- เพิ่มบัตรที่เข้ารหัสลับคุกกี้
- เพิ่มคุกกี้ไปยังHttpResponse.Cookiesคอลเลกชัน
- การเปลี่ยนเส้นทางการร้องขอไปยัง URL ที่ถูกร้องขอตั้งแต่แรก
ปรับเปลี่ยนเพ WebForm1.aspx
หน้า WebForm1.aspx คือ เพจที่มีการร้องขอตั้งแต่แรก
เมื่อผู้ใช้ที่ร้องขอเพจนี้ การร้องขอถูกเปลี่ยนเส้นทางไปยัง Logon.aspx หน้า หลังจากที่ร้องขอจะรับรองความถูกต้อง คำขอถูกเปลี่ยนเส้นทางไปยังเพจการ WebForm1.aspx
- คลิกขวาใน Explorer โซลูชันWebForm1.aspxแล้ว คลิกตัวออกแบบมุมมอง.
- คลิกการhtmlแท็บใน Designer
- แทนรหัสที่มีอยู่ ด้วยรหัสต่อไปนี้:
<%@ Page language="vb" AutoEventWireup="true" %>
<%@ Import Namespace="System.Security.Principal" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label ID="lblName" Runat="server" /><br>
<asp:Label ID="lblAuthType" Runat="server" />
</form>
</body>
</html>
<script runat="server">
sub Page_Load(sender as object, e as EventArgs)
lblName.Text = "Hello " + Context.User.Identity.Name & "."
lblAuthType.Text = "You were authenticated using " & Context.User.Identity.AuthenticationType & "."
end sub
</script>
- บันทึกแฟ้มทั้งหมด และการคอมไพล์โครงการแล้ว
- การร้องขอเพ WebForm1.aspx ขอให้สังเกตว่า คุณจะเปลี่ยนเส้นเพื่อ Logon.aspx
- พิมพ์ข้อมูลประจำตัวการเข้าสู่ระบบ และจากนั้น คลิกส่ง. เมื่อคุณเปลี่ยนเส้นทางไปยัง WebForm1.aspx ขอให้สังเกตว่า ชื่อผู้ใช้ปรากฏขึ้น และที่LdapAuthenticationมีชนิดของการรับรองความถูกต้องสำหรับการContext.User.Identity.AuthenticationTypeคุณสมบัติ
หมายเหตุ:Microsoft แนะนำให้ คุณใช้ Secure Sockets Layer (SSL) เข้ารหัสลับเมื่อคุณใช้การรับรองความถูกต้องของฟอร์ม นี่คือเนื่องจากผู้ใช้ที่ระบุโดยยึดตามคุกกี้การรับรองความถูกต้อง และการเข้ารหัสลับ SSL บนเว็บไซต์โปรแกรมประยุกต์นี้ป้องกันทุกคน compromising คุกกี้การรับรองความถูกต้องและมีค่าข้อมูลอื่น ๆ ที่ไม่ถูกส่ง
หากต้องการทราบข้อมูลเพิ่มเติม โปรดคลิกที่หมายเลขบทความต่อไปนี้เพื่อดูบทความใน Microsoft Knowledge Base::
306590
(http://support.microsoft.com/kb/306590/
)
ภาพรวมการรักษาความปลอดภัยของ asp.net
317012
(http://support.microsoft.com/kb/317012/
)
ตัวประมวลผลและการร้องขอใน ASP.NET
306238
(http://support.microsoft.com/kb/306238/
)
วิธีการใช้ตามบทบาทการรักษาความปลอดภัย ด้วยใช้แบบฟอร์มการรับรองความถูกต้องในโปรแกรมประยุกต์ ASP.NET ของคุณ โดยใช้ Visual Basic .NET
313091
(http://support.microsoft.com/kb/313091/
)
วิธีการสร้างคีย์ โดยใช้ Visual Basic .NET สำหรับใช้ในการรับรองความถูกต้องของฟอร์ม
313116
(http://support.microsoft.com/kb/313116/
)
แบบฟอร์มการร้องขอการรับรองความถูกต้องไม่นำหน้า loginUrl
หมายเลขบทความ (Article ID): 326340 - รีวิวครั้งสุดท้าย: 13 มกราคม 2554 - Revision: 5.0
ใช้กับ
- Microsoft ASP.NET 1.0
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft ASP.NET 1.1
- Microsoft Visual Basic .NET 2003 Standard Edition
| kbconfig kbcookie kbhowtomaster kbsecurity kbwebforms kbmt KB326340 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
326340
(http://support.microsoft.com/kb/326340/en-us/
)