Tips SistemThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
Artikel ini merujuk kepada Microsoft berikut.NET
Kerangka perpustakaan kelas namespaces:
Jika Anda ingin meniru pengguna pada sebuah thread di ASP.NET,
Anda dapat menggunakan salah satu metode berikut, yang didasarkan pada syarat-syarat Anda:
Untuk meniru layanan informasi Internet Microsoft (IIS)
otentikasi pengguna pada setiap permintaan untuk setiap halaman di ASP.NET aplikasi,
Anda harus memasukkan <identity></identity> Tag di file Web.config aplikasi ini dan set meniru atribut benar. Misalnya:
Meniru pengguna tertentu untuk semua permintaan ASP.NET aplikasi
Untuk meniru pengguna tertentu untuk semua permintaan pada semua halaman
dari ASP.NET aplikasi, Anda dapat menentukan userName dan sandi atribut di <identity></identity> Tag file Web.config untuk aplikasi tersebut. Misalnya:
Catatan Identitas dari proses yang impersonates pengguna tertentu pada
thread harus memiliki hak istimewa "Bertindak sebagai bagian dari sistem operasi". Oleh
default, proses Aspnet_wp.exe berjalan di bawah account komputer yang bernama ASPNET.
Namun, akun ini tidak memiliki hak yang diperlukan untuk meniru
pengguna tertentu. Anda menerima pesan kesalahan jika Anda mencoba untuk meniru
pengguna tertentu. Informasi ini berlaku hanya untuk.NET Framework 1.0. Ini
hak istimewa tidak diperlukan untuk.NET Framework 1.1.
Untuk bekerja di sekitar
masalah ini, gunakan salah satu metode berikut:
Memberikan hak istimewa "Bertindak sebagai bagian dari sistem operasi"
ASPNET account (rekening paling tidak istimewa).
Catatan Meskipun Anda dapat menggunakan metode ini untuk bekerja di sekitar masalah,
Microsoft tidak menganjurkan metode ini.
Mengubah account yang menjalankan proses Aspnet_wp.exe
di bawah sistem account di bagian konfigurasi <processmodel>
Machine.config elemen dari file.</processmodel>
Untuk meniru otentikasi pengguna (User.Identity) hanya ketika Anda menjalankan bagian tertentu dari kode, Anda dapat menggunakan
kode untuk mengikuti. Metode ini memerlukan otentikasi pengguna identitas adalah
jenis WindowsIdentity.
Visual Basic.NET
Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
currentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity)
impersonationContext = currentWindowsIdentity.Impersonate()
'Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo()
Visual C#.NET
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();
Visual J#.NET
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();
Untuk meniru pengguna tertentu hanya ketika Anda menjalankan tertentu
bagian dari kode, gunakan kode berikut:
Visual Basic.NET
<%@ Page Language="VB" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script runat=server>
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
Dim impersonationContext As WindowsImpersonationContext
Declare Function LogonUserA Lib "advapi32.dll" (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 Integer
Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
ByVal ExistingTokenHandle As IntPtr, _
ByVal ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If impersonateValidUser("username", "domain", "password") Then
'Insert your code that runs under the security context of a specific user here.
undoImpersonation()
Else
'Your impersonation failed. Therefore, include a fail-safe mechanism here.
End If
End Sub
Private Function impersonateValidUser(ByVal userName As String, _
ByVal domain As String, ByVal password As String) As Boolean
Dim tempWindowsIdentity As WindowsIdentity
Dim token As IntPtr = IntPtr.Zero
Dim tokenDuplicate As IntPtr = IntPtr.Zero
impersonateValidUser = False
If RevertToSelf() Then
If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
impersonationContext = tempWindowsIdentity.Impersonate()
If Not impersonationContext Is Nothing Then
impersonateValidUser = True
End If
End If
End If
End If
If Not tokenDuplicate.Equals(IntPtr.Zero) Then
CloseHandle(tokenDuplicate)
End If
If Not token.Equals(IntPtr.Zero) Then
CloseHandle(token)
End If
End Function
Private Sub undoImpersonation()
impersonationContext.Undo()
End Sub
</script>
Visual C#.NET
<%@ Page Language="C#"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script runat=server>
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public void Page_Load(Object s, EventArgs e)
{
if(impersonateValidUser("username", "domain", "password"))
{
//Insert your code that runs under the security context of a specific user here.
undoImpersonation();
}
else
{
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
}
}
private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if(RevertToSelf())
{
if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
</script>
Visual J#.NET
<%@ Page language="VJ#" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<script runat=server>
public static int LOGON32_LOGON_INTERACTIVE = 2;
public static int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
/** @attribute DllImport("advapi32.dll") */
public static native int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
System.IntPtr[] phToken);
/** @attribute DllImport("advapi32.dll",
CharSet=CharSet.Auto, SetLastError=true) */
public static native int DuplicateToken(System.IntPtr hToken,
int impersonationLevel,
System.IntPtr[] hNewToken);
/** @attribute DllImport("kernel32.dll",CharSet=CharSet.Auto) */
public static native boolean CloseHandle(System.IntPtr[] handle);
/** @attribute DllImport("advapi32.dll",
CharSet=CharSet.Auto,SetLastError=true) */
public static native boolean RevertToSelf();
public void Page_Load(Object s, System.EventArgs e)
{
if(impersonateValidUser("username", "domain", " password"))
{
//Insert your code that runs under the security context of a specific user here.
undoImpersonation();
}
else
{
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
}
}
private boolean impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
System.IntPtr[] token = new System.IntPtr[1];
System.IntPtr[] tokenDuplicate = new System.IntPtr[1];
if(RevertToSelf())
{
if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, token) != 0)
{
if(DuplicateToken(token[0], 2, tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[0]);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(tokenDuplicate);
CloseHandle(token);
return true;
}
}
}
}
if(!token[0].Equals(System.IntPtr.Zero))
CloseHandle(token);
if(!tokenDuplicate[0].Equals(System.IntPtr.Zero))
CloseHandle(tokenDuplicate);
return false;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
</script>
Catatan Identitas dari proses yang impersonates pengguna tertentu pada
thread harus memiliki hak istimewa "Bertindak sebagai bagian dari sistem operasi"
Jika proses Aspnet_wp.exe berjalan di komputer berbasis Microsoft Windows 2000.
The
Hak istimewa "Bertindak sebagai bagian dari sistem operasi" tidak diperlukan jika
Aspnet_wp.exe proses berjalan di komputer berbasis Windows XP atau Windows Server
komputer berbasis 2003. Secara default, Aspnet_wp.exe
proses berjalan di bawah account komputer yang bernama ASPNET. Namun, Apakah account ini
tidak memiliki hak yang diperlukan untuk meniru pengguna tertentu. Anda menerima
pesan galat apabila Anda mencoba untuk meniru pengguna tertentu. .
Untuk menyelesaikan masalah ini, gunakan salah satu dari metode berikut:
Memberikan hak istimewa "Bertindak sebagai bagian dari sistem operasi"
ASPNET account.
Catatan Kami tidak menyarankan metode ini untuk bekerja di sekitar
masalah.
Mengubah account yang menjalankan proses Aspnet_wp.exe
di bawah sistem account di bagian konfigurasi <processmodel>
Machine.config elemen dari file.</processmodel>
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:306158
(http://support.microsoft.com/kb/306158/en-us/
)
Seberapa besar upaya Anda untuk menggunakan artikel ini?
Sangat sedikit
Sedikit
Sedang
Besar
Sangat besar
Berikan saran tentang apa yang dapat kami lakukan untuk menyempurnakan informasi ini
Terima kasih! Masukan Anda akan digunakan untuk membantu kami meningkatkan konten dukungan. Untuk opsi bantuan lainnya, kunjungi Halaman Beranda Bantuan dan Dukungan.