Web Parts, Impersonate and Security Policy, Part 2
Web Parts, Impersonate and Security Policy
Part 2
Written by: Rickie Lee (rickieleemail at yahoo.com)
继续前面的posting《Web Parts, Impersonate and Security Policy, Part 1》,阐明如何解决SharePoint Web Parts开发过程中访问权限的问题。Part 1中以C#的示例代码演示了impersonate(角色扮演)的应用。
这里简单贴出VB.Net代码,方便使用VB.Net的CASE。
Protected Shared Function CreateIdentity(ByVal User As String, _
ByVal Domain As String, ByVal Password As String) As WindowsIdentity
Dim objToken As New IntPtr(0)
Dim ID As WindowsIdentity
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_NETWORK As Integer = 3
'Initialize token object
objToken = IntPtr.Zero
' Attempt to log on
Dim blnReturn As Boolean = LogonUser(User, Domain, Password, _
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, objToken)
'Check for failure
If blnReturn = False Then
Dim intCode As Integer = Marshal.GetLastWin32Error()
Throw New Exception("Logon failed: " & intCode.ToString)
End If
'Return new token
ID = New WindowsIdentity(objToken)
CloseHandle(objToken)
Return ID
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function LogonUser(ByVal lpszUsername As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
***
未完,Part 3待续.
Reference:
1. Rickie Lee, Web Parts, Impersonate and Security Policy, Part 1, you can reach Rickie Lee at rickieleemail (at) yahoo.com.