Makale numarası: 306158 - Son Gözden Geçirme: 23 Şubat 2007 Cuma - Gözden geçirme: 4.10

ASP.NET uygulamasında kimliğe bürünme nasıl uygulanır (Bu bağlantı, bir kısmı veya tamamı İngilizce olan içeriğe işaret edebilir.)

Sistem İpucuBu makale, kullandığınızdan farklı bir işletim sistemine yöneliktir. Sizinle ilgili olmayabilecek makale içeriği devre dışı bırakıldı.
Bu makalede, aşağıdaki Microsoft .NET Framework Sınıf Kitaplığı ad alanlarına başvurulmaktadır:
  • System.Web.Security
  • System.Security.Principal
  • System.Runtime.ınteropservices

Bu Sayfada

Hepsini aç | Hepsini kapa

Özet

Bu makalede, bir ASP.NET uygulamasında kimliğe bürünme uygulamak için farklı yöntemler anlatılmaktadır.

Daha fazla bilgi

ASP.NET'te bir iş parçacığında bir kullanıcının yerine geçmek istiyorsanız, üzerinde kendi requirments bağlı olarak aşağıdaki yöntemlerden birini kullanabilirsiniz: Not Iş parçacığının olarak çalıştırmak için hangi kullanıcı belirlemek için şu kodu kullanın:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
				

Kimliği doğrulanmış IIS özelliklerini hesabı veya kullanıcı

Ve Microsoft ınternet ınformation Services (IIS) her sayfanın bir ASP.NET uygulaması için tüm istekleri, kullanıcı kimliği doğrulanıyor yerine geçmek için <a0></a0>, bir <identity>eklemelisiniz. bu uygulamanın Web.config dosyasında Etiket ve özelliklerini özniteliği true olarak ayarlayın. Örneğin:
<identity impersonate="true" />
				

Belirli bir kullanıcı, bir ASP.NET uygulaması, tüm istekleri için kimliğine bürün

Bir ASP.NET uygulamasının tüm sayfalardaki tüm istekleri için belirli bir kullanıcının yerine geçmek için <a0></a0>, userName ve password özniteliklerini <identity>belirtebilirsiniz söz konusu uygulamanın Web.config dosyasındaki bir etiket. Örneğin:
<identity impersonate="true" userName="accountname" password="password" />
				
bir iş parçacığı üzerinde belirli bir kullanıcının kimliğine bürünür işlem kimliğini Not "işletim sisteminin bir parçası çalış" ayrıcalığına sahip olmanız gerekir. Varsayılan değer olarak Aspnet_wp.exe işlemi ASPNET adlı bir bilgisayar hesabının altında çalışır. Ancak, bu hesabın, belirli bir kullanıcının kimliğine bürünmek için gereken ayrıcalıklara sahip değil. Belirli bir kullanıcının kimliğine bürünmek çalışırsanız bir hata iletisi alırsınız. Bu bilgiler, yalnızca .NET Framework 1.0 için geçerlidir. Bu ayrıcalık, .NET Framework 1. 1'için gerekli değildir.

Bu soruna geçici bir çözüm için aşağıdaki yöntemlerden birini kullanın:
  • ASPNET hesabı (en az ayrıcalıklı hesabı) için "işletim sisteminin bir parçası çalış" ayrıcalığı vermek.

    Not Bu soruna geçici bir çözüm için bu yöntemi kullanabilirsiniz, ancak Microsoft, bu yöntem önermez.
  • Aspnet_wp.exe işlem <processmodel>yapılandırma bölümünde Machine.config dosyasının sistem hesabı için çalıştığı hesabın değiştirin.

Kimlik doğrulama kodu kullanıcı kimliğine bürün

Yalnızca belirli bir bölüm kodu çalıştırdığınızda (User.Identity) kimlik doğrulaması yapan kullanıcının kimliğine bürünmek için <a0></a0>, kod izlemek için kullanabilirsiniz. Bu yöntem, kimlik doğrulama kullanıcı kimliği türü Windowsıdentity olduğunu gerektirir.

Visual Basic. NET'e
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'i
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();
				

Kodu, belirli bir kullanıcı kimliğine bürün

Yalnızca belirli bir bölüm kodu çalıştırdığınızda, belirli bir kullanıcının kimliğine bürünmek için <a0></a0>, şu kodu kullanın:

Visual Basic. NET'e
<%@ 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'i
<%@ 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>
				
Aspnet_wp.exe işlemi Microsoft Windows 2000 tabanlı bir bilgisayarda çalıştırıyorsanız, bir iş parçacığı üzerinde belirli bir kullanıcının kimliğine bürünür işlem kimliğini Not "işletim sisteminin bir parçası çalış" ayrıcalığı olmalıdır. Aspnet_wp.exe işlemi, Windows XP tabanlı bir bilgisayarda veya Windows Server 2003 tabanlı bir bilgisayarda çalışıyorsa, "işletim sisteminin bir parçası çalış" ayrıcalığı gerekli değildir. Varsayılan olarak, the Aspnet_wp.exe işlemini çalıştıran bir bilgisayar hesabının altında ASPNET adlı. Ancak, bu hesabın, belirli bir kullanıcının kimliğine bürünmek için gereken ayrıcalıklara sahip değil. Belirli bir kullanıcının kimliğine bürünmek çalışırsanız bir hata iletisi alırsınız. .

Bu soruna geçici bir çözüm için aşağıdaki yöntemlerden birini kullanın:
  • "Işletim sisteminin bir parçası çalış" ayrıcalığı ASPNET hesabına verin.

    Not Bu soruna geçici bir çözüm için bu yöntem önerilmez.
  • Aspnet_wp.exe işlem <processmodel>yapılandırma bölümünde Machine.config dosyasının sistem hesabı için çalıştığı hesabın değiştirin.

Referanslar

ASP.NET güvenliği hakkında ek bilgi için Microsoft Knowledge Base'deki makaleyi görüntülemek üzere aşağıdaki makale numarasını tıklatın:
306590  (http://support.microsoft.com/kb/306590/ ) ASP.NET güvenliğine genel bakış

Bu makaledeki bilginin uygulandığı durum:
  • Microsoft ASP.NET 1.0
  • Microsoft ASP.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual J# .NET 2003 Standard Edition
Anahtar Kelimeler: 
kbmt kbinfo kbsecurity KB306158 KbMttr
Otomatik TercümeOtomatik Tercüme
ÖNEMLİ: Bu makale, bir kişi tarafından çevrilmek yerine, Microsoft makine-çevirisi yazılımı ile çevrilmiştir. Microsoft size hem kişiler tarafından çevrilmiş, hem de makine-çevrisi ile çevrilmiş makaleler sunar. Böylelikle, bilgi bankamızdaki tüm makalelere, kendi dilinizde ulaşmış olursunuz. Bununla birlikte, makine tarafından çevrilmiş makaleler mükemmel değildir. Bir yabancının sizin dilinizde konuşurken yapabileceği hatalar gibi, makale; kelime dağarcığı, söz dizim kuralları veya dil bilgisi açısından yanlışlar içerebilir. Microsoft, içeriğin yanlış çevrimi veya onun müşteri tarafından kullanımından doğan; kusur, hata veya zarardan sorumlu değildir. Microsoft ayrıca makine çevirisi yazılımını sıkça güncellemektedir.
Makalenin İngilizcesi aşağıdaki gibidir:306158  (http://support.microsoft.com/kb/306158/en-us/ )