我有一個WCF服務,它在以後的不確定時間代表用戶排隊並執行命令。我希望將WindowsIdentity存儲爲字節數組並將其填充到數據庫中,然後反序列化並使用該對象。如何存儲WindowsIdentity以便在以後不確定的時間使用
某些時候,服務按預期執行:它正確地序列化,存儲,反序列化並以用戶身份執行命令。其他時候,當反序列化WindowsIdentity時,出現「調用目標引發異常」的錯誤,還有一些時候反序列化可行,但通過命令執行的一部分,標識不再有效。
我的問題是這樣的:是否有可能在.NET 4.0框架中使用WCF代表用戶在以後不確定的時間執行命令,而沒有明確的用戶名和密碼?
,我正在使用的代碼如下:
連載:
''' <summary>
''' Serializes a WindowsIdentity as a binary encoded byte array.
''' </summary>
''' <param name="identity">The identity to serialize.</param>
''' <returns>A byte array containing a binary representation of the identity.</returns>
Private Function SerializeWindowsIdentity(ByVal identity As WindowsIdentity) As Byte()
If IsNothing(identity) Then Return Nothing
Try
Dim bf As New BinaryFormatter
Using ms As New MemoryStream()
bf.Serialize(ms, identity)
Return ms.ToArray()
End Using
Catch ex As Exception
Return Nothing
End Try
End Function ' SerializeWindowsIdentity
反序列化:
''' <summary>
''' Deserializes a WindowsIdentity from a binary encoded byte array.
''' </summary>
''' <param name="identity">A byte array containing a binary representation of a WindowsIdentity</param>
''' <returns>The deserialized WindowsIdentity from the byte array.</returns>
Private Function DeserializeWindowsIdentity(ByVal identity As Byte()) As WindowsIdentity
If IsNothing(identity) OrElse identity.Count = 0 Then Return Nothing
Try
Dim bf As New BinaryFormatter()
Using ms As New MemoryStream(identity)
Dim obj As Object = bf.Deserialize(ms)
Return CType(obj, WindowsIdentity)
End Using
Catch ex As Exception
Return Nothing
End Try
End Function ' DeserializeWindowsIdentity
的WindowsIdentity捕獲:
identity = SerializeWindowsIdentity(ServiceSecurityContext.Current.WindowsIdentity)
用法:
Dim cxt As WindowsImpersonationContext
Try
Dim wi As WindowsIdentity = DeserializeWindowsIdentity(identity)
If Not IsNothing(wi) Then cxt = wi.Impersonate()
' Do Stuff
Finally
If Not IsNothing(cxt) Then cxt.Dispose()
End If
你怎麼能期望windows身份在不確定的時間後有效?即如果用戶更改密碼,或被刪除等 – EdmundYeung99 2013-03-07 01:16:45
也許如果你解釋你的用例,我們可以建議一種替代方法? – EdmundYeung99 2013-03-07 01:20:13
感謝您的評論。不,我不希望在密碼更改或帳戶刪除的情況下該身份有效。 – dthagard 2013-03-07 18:45:16