[英]asp.net WindowsIdentity Impersonate from known credentials C#
早上好,
我有一個asp.net項目,其中一個表單(“身份驗證”設置為“無”)針對第三方服務器驗證用戶憑據,然后在驗證后將用戶重定向到受保護的內容。
我想做的就是根據這些憑據創建一個WindowsIdentity,以便我可以訪問服務器上的其他WindowsAuthentication頁面以及網絡上的其他頁面。
這可能嗎?
您將需要使用LogonUser API函數來模擬您的呼叫(並且您還將需要帳戶登錄名和密碼,以便可以使用此功能)。
看一下這篇文章,它解釋了如何做到這一點: http : //msdn.microsoft.com/en-us/library/ff647404.aspx 。
這是本文的代碼片段,目的是對這種方法有所了解。
try
{
// Create a token for DomainName\Bob
// Note: Credentials should be encrypted in configuration file
bool result = LogonUser("Bob", "DomainName",
"P@ssw0rd",
LogonSessionType.Network,
LogonProvider.Default,
out token);
if (result)
{
WindowsIdentity id = new WindowsIdentity(token);
// Begin impersonation
impersonatedUser = id.Impersonate();
// Log the new identity
Response.Write(String.Format(
"</p>Identity after impersonation: {0}<br>",
WindowsIdentity.GetCurrent().Name));
// Resource access here uses the impersonated identity
}
else
{
Response.Write("</p>LogonUser failed: " +
Marshal.GetLastWin32Error().ToString());
}
}
catch
{
// Prevent any exceptions that occur while the thread is
// impersonating from propagating
}
finally
{
// Stop impersonation and revert to the process identity
if (impersonatedUser != null)
impersonatedUser.Undo();
// Free the token
if (token != IntPtr.Zero)
CloseHandle(token);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.