001 Imports ...System.Security.Principal 002 003 004 Partial Class NTAccount 005 Inherits System.Web.UI.Page 006 007 '=============================================== 008 '== 下列範例顯示 WindowsIdentity 類別中,目前這位成員的用法。== 009 '=============================================== 010 ' 011 '==參考網址 http://msdn.microsoft.com/zh-tw/library/system.security.principal.windowsidentity.aspx 012 013 Protected Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 014 ' Retrieve the [Windows account token] for the current user. 015 Dim logonToken As IntPtr = WindowsIdentity.GetCurrent().Token 016 017 ' Constructor implementations. 執行下面四個 Function 018 IntPtrConstructor(logonToken) 019 IntPtrStringConstructor(logonToken) 020 IntPtrStringTypeConstructor(logonToken) 021 IntPrtStringTypeBoolConstructor(logonToken) 022 023 ' Property implementations. 024 UseProperties(logonToken) '--UseProperties()是下面的Function 025 026 ' Method implementations. 027 GetAnonymousUser() '--GetAnonymousUser()是下面的Function 028 ImpersonateIdentity(logonToken) '--ImpersonateIdentity()是下面的Function 029 030 ' Align interface and conclude application. 031 Response.Write("<br><br><hr>程式完成。This sample completed ......successfully<hr>") 032 End Sub 033 034 ' Create a WindowsIdentity object for the user represented by the 035 ' specified Windows account token. 036 Private Sub IntPtrConstructor() Sub IntPtrConstructor(ByVal logonToken As IntPtr) 037 ' Construct a WindowsIdentity object using the input account token. 038 '重點!!一個參數 039 Dim windowsIdentity As New WindowsIdentity(logonToken) 040 041 Response.Write("Created a Windows identity object named ( IntPtrConstructor() )-- <font color=red>" + windowsIdentity.Name + "</font>.<br>") 042 End Sub 043 044 ' Create a WindowsIdentity object for the user represented by the 045 ' [specified account token] and [authentication type]. 046 Private Sub IntPtrStringConstructor() Sub IntPtrStringConstructor(ByVal logonToken As IntPtr) 047 ' Construct a WindowsIdentity object using the input account token 048 ' and the specified authentication type. 049 Dim authenticationType = "WindowsAuthentication" 050 051 '重點!!兩個參數 052 Dim windowsIdentity As New WindowsIdentity(logonToken, authenticationType) 053 054 Response.Write("Created a Windows identity object named ( IntPtrStringConstructor() )-- <font color=red>" + windowsIdentity.Name + "</font>.<br>") 055 End Sub 056 057 ' Create a WindowsIdentity object for the user represented by the 058 ' specified account token, authentication type, and Windows account type. 059 Private Sub IntPtrStringTypeConstructor() Sub IntPtrStringTypeConstructor(ByVal logonToken As IntPtr) 060 ' Construct a WindowsIdentity object using the input account token, 061 ' and the specified authentication type and Windows account type. 062 Dim authenticationType As String = "WindowsAuthentication" 063 Dim guestAccount As WindowsAccountType = WindowsAccountType.Guest 064 065 '重點!!三個參數 066 Dim windowsIdentity As _ 067 New WindowsIdentity(logonToken, authenticationType, guestAccount) 068 069 Response.Write("Created a Windows identity object named ( IntPtrStringTypeConstructor() )--<font color=red>" + windowsIdentity.Name + "</font>.<br>") 070 End Sub 071 072 ' Create a WindowsIdentity object for the user represented by the 073 ' specified account token, authentication type, Windows account type, 074 ' and Boolean authentication flag. 075 Private Sub IntPrtStringTypeBoolConstructor() Sub IntPrtStringTypeBoolConstructor(ByVal logonToken As IntPtr) 076 ' Construct a WindowsIdentity object using the input account token, 077 ' and the specified authentication type, Windows account type, and 078 ' authentication flag. 079 Dim authenticationType As String = "WindowsAuthentication" 080 Dim guestAccount As WindowsAccountType = WindowsAccountType.Guest 081 Dim isAuthenticated As Boolean = True 082 083 '重點!!四個參數 084 Dim windowsIdentity As _ 085 New WindowsIdentity(logonToken, authenticationType, guestAccount, isAuthenticated) 086 087 Response.Write("Created a Windows identity object named ( IntPrtStringTypeBoolConstructor() )--<font color=red>" + windowsIdentity.Name + "</font>.<br>") 088 End Sub 089 090 ' Access the properties of a WindowsIdentity object. 091 Private Sub UseProperties() Sub UseProperties(ByVal logonToken As IntPtr) 092 Dim windowsIdentity As New WindowsIdentity(logonToken) '重點!!一個參數 093 Dim propertyDescription As String = "<br>The Windows identity named -- " 094 095 ' Retrieve the Windows logon name from the Windows identity object. 096 propertyDescription += "<font color=red>" + windowsIdentity.Name + "</font>" 097 098 '--------------------------------------------------------------(start)-- 099 ' Verify that the user account is not considered to be an Anonymous 100 ' account by the system. 101 If Not windowsIdentity.IsAnonymous Then 102 propertyDescription += " is not(非) an Anonymous(匿名) account<br>" 103 End If 104 105 ' Verify that the user account has been authenticated by Windows. 106 If (windowsIdentity.IsAuthenticated) Then 107 propertyDescription += ", is authenticated(已認證)<br>" 108 End If 109 110 ' Verify that the user account is considered to be a System account by 111 ' the system. 112 If (windowsIdentity.IsSystem) Then 113 propertyDescription += ", is a System account(系統帳號)<br>" 114 End If 115 116 ' Verify that the user account is considered to be a Guest account by 117 ' the system. 118 If (windowsIdentity.IsGuest) Then 119 propertyDescription += ", is a Guest account(Guest客人帳戶)<br>" 120 End If 121 '--------------------------------------------------------------(end)-- 122 123 Dim authenticationType As String = windowsIdentity.AuthenticationType 124 125 ' Append the authenication type to the output message. 126 If (Not authenticationType Is Nothing) Then 127 propertyDescription += (" and uses <font color=red>" + authenticationType + "</font> authentication type.<br>") 128 End If 129 130 Response.Write(propertyDescription) 131 132 ' Display the SID for the owner. 133 Response.Write("<hr>The SID for the owner is : ") 134 Dim si As SecurityIdentifier = windowsIdentity.Owner 135 Response.Write("<font color=red>" + si.ToString() + "</font><br>") 136 137 '--------------------------------------------------------------(start) 138 ' Display the SIDs for the groups the current user belongs to. 139 Response.Write("<hr>Display the SIDs for the groups the current user belongs to. 目前這名使用者隸屬於哪些群組?<br>") 140 141 Dim irc As IdentityReferenceCollection = windowsIdentity.Groups 142 Dim ir As IdentityReference 143 144 For Each ir In irc 145 Response.Write("<font color=green>" + ir.Value + "</font><br>") 146 Next 147 '--------------------------------------------------------------(end) 148 149 Dim token As TokenImpersonationLevel = windowsIdentity.ImpersonationLevel 150 Response.Write("<br>The impersonation(模擬) level for the current user is : <font color=red>" + token.ToString() + "</font><br>") 151 End Sub 152 153 154 155 '----------------------------------------------------------------------------------------------------------------------------- 156 157 ' Get the WindowsIdentity object for an Anonymous user. 158 Private Sub GetAnonymousUser() Sub GetAnonymousUser() 159 ' Retrieve a WindowsIdentity object that represents an anonymous 160 ' Windows user. 161 Dim windowsIdentity As WindowsIdentity = windowsIdentity.GetAnonymous() 162 End Sub 163 164 165 ' Impersonate a Windows identity. 166 Private Sub ImpersonateIdentity() Sub ImpersonateIdentity(ByVal logonToken As IntPtr) 167 ' Retrieve the Windows identity using the specified token. 168 Dim windowsIdentity As New WindowsIdentity(logonToken) 169 170 ' Create a WindowsImpersonationContext object by impersonating the Windows identity. 171 Dim impersonationContext As WindowsImpersonationContext = windowsIdentity.Impersonate() 172 173 Response.Write("Name of the identity after impersonation(模擬): <font color=red>" + windowsIdentity.GetCurrent().Name + "</font>.<br>") 174 175 ' Stop impersonating the user. 176 impersonationContext.Undo() 177 ' Check the identity. 178 Response.Write("Name of the identity after performing an <b>Undo</b> on the impersonation(模擬): <font color=red>" + windowsIdentity.GetCurrent().Name + "</font>.") 179 End Sub 180 181 End Class