ASP.NET ÀÀ¿ë ÇÁ·Î±×·¥¿¡¼­ °¡Àå(Impersonation)À» ±¸ÇöÇÏ´Â ¹æ¹ý

±â¼ú ÀÚ·á: 306158 - ÀÌ ¹®¼­°¡ Àû¿ëµÇ´Â Á¦Ç° º¸±â.
ÀÌ ¹®¼­´Â ÀÌÀü¿¡ ´ÙÀ½ ID·Î ÃâÆÇµÇ¾úÀ½: KR306158
ÀÌ ¹®¼­¿¡¼­ ÂüÁ¶ÇÏ´Â Microsoft .NET Framework Ŭ·¡½º ¶óÀ̺귯¸® ³×ÀÓ½ºÆäÀ̽º´Â ´ÙÀ½°ú °°½À´Ï´Ù.
  • System.Web.Security
  • System.Security.Principal
  • System.Runtime.InteropServices
¸ðµÎ È®´ë | ¸ðµÎ Ãà¼Ò

ÀÌ ÆäÀÌÁö¿¡¼­

¿ä¾à

ÀÌ ¹®¼­¿¡¼­´Â ASP.NET ÀÀ¿ë ÇÁ·Î±×·¥¿¡¼­ °¡ÀåÀ» ±¸ÇöÇÏ´Â ¿©·¯ °¡Áö ¹æ¹ýÀ» ¼³¸íÇÕ´Ï´Ù.

Ãß°¡ Á¤º¸

ASP.NETÀÇ ½º·¹µå¿¡¼­ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ·Á¸é ´ÙÀ½ Áß »ç¿ëÀÚ¿¡°Ô ÇÊ¿äÇÑ ¹æ¹ýÀ» »ç¿ëÇϽʽÿÀ. Âü°í ´ÙÀ½ Äڵ带 »ç¿ëÇÏ¸é ½º·¹µå°¡ ¾î¶² »ç¿ëÀÚ·Î ½ÇÇà ÁßÀÎÁö È®ÀÎÇÒ ¼ö ÀÖ½À´Ï´Ù.
 System.Security.Principal.WindowsIdentity.GetCurrent().Name 				

IIS ÀÎÁõ °èÁ¤À̳ª »ç¿ëÀÚ °¡Àå

ASP.NET ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ ¸ðµç ÆäÀÌÁö¿¡ ´ëÇÑ ¿äû¿¡¼­ Microsoft Internet Information Services(IIS) ÀÎÁõ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ·Á¸é ÇØ´ç ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ Web.config ÆÄÀÏ¿¡ <identity> ű׸¦ Æ÷ÇÔ½Ã۰í impersonate Ư¼ºÀ» true·Î ¼³Á¤ÇØ¾ß ÇÕ´Ï´Ù. ¿¹¸¦ µé¸é ´ÙÀ½°ú °°½À´Ï´Ù.
<identity impersonate="true" />
				

ASP.NET ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ ¸ðµç ¿äû¿¡ ´ëÇØ ƯÁ¤ »ç¿ëÀÚ °¡Àå

TASP.NET ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ ¸ðµç ÆäÀÌÁö¿¡ ´ëÇÑ ¿äû¿¡¼­ ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ·Á¸é ÇØ´ç ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ Web.config ÆÄÀÏ¿¡¼­ <identity> ű×ÀÇ userName°ú password Ư¼ºÀ» ÁöÁ¤ÇÕ´Ï´Ù. ¿¹¸¦ µé¸é ´ÙÀ½°ú °°½À´Ï´Ù.
 <identity impersonate="true" userName="accountname" password="password" /> 				
Âü°í ½º·¹µå¿¡¼­ ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ´Â ÇÁ·Î¼¼½ºÀÇ ID´Â "¿î¿µ üÁ¦ÀÇ ÀϺηΠȰµ¿" ±ÇÇÑÀ» °®°í ÀÖ¾î¾ß ÇÕ´Ï´Ù. ±âº»ÀûÀ¸·Î Aspnet_wp.exe ÇÁ·Î¼¼½º´Â ASPNETÀ̶ó´Â ÄÄÇ»ÅÍ °èÁ¤À¸·Î ½ÇÇàµË´Ï´Ù. ±×·¯³ª ÀÌ °èÁ¤¿¡´Â ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ´Â µ¥ ÇÊ¿äÇÑ ±ÇÇÑÀÌ ¾ø½À´Ï´Ù. ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ·Á°í ÇÏ¸é ¿À·ù ¸Þ½ÃÁö°¡ ³ªÅ¸³³´Ï´Ù. ÀÌ ¹®Á¦´Â .NET Framework 1.0¿¡¸¸ Àû¿ëµË´Ï´Ù. .NET Framework 1.1¿¡¼­´Â ÀÌ ±ÇÇÑÀÌ ÇÊ¿äÇÏÁö ¾Ê½À´Ï´Ù.

ÀÌ ¹®Á¦¸¦ ÇØ°áÇÏ·Á¸é ´ÙÀ½ ¹æ¹ý Áß Çϳª¸¦ »ç¿ëÇϽʽÿÀ.
  • "¿î¿µ üÁ¦ÀÇ ÀϺηΠȰµ¿" ±ÇÇÑÀ» ASPNET °èÁ¤(ÃÖ¼ÒÀÇ ±ÇÇÑÀÌ ºÎ¿©µÈ °èÁ¤)¿¡ ºÎ¿©ÇÕ´Ï´Ù.

    Âü°í ÀÌ ¹æ¹ýÀ» »ç¿ëÇÏ¿© ¹®Á¦¸¦ ÇØ°áÇÒ ¼ö ÀÖÁö¸¸ Microsoft°¡ ±ÇÀåÇÏ´Â ¹æ¹ýÀº ¾Æ´Õ´Ï´Ù.
  • Aspnet_wp.exe ÇÁ·Î¼¼½º¸¦ ½ÇÇàÇÏ´Â °èÁ¤À» Machine.config ÆÄÀÏÀÇ <processModel> ±¸¼º ¼½¼Ç¿¡ ÀÖ´Â ½Ã½ºÅÛ °èÁ¤À¸·Î º¯°æÇÕ´Ï´Ù.

Äڵ忡¼­ ÀÎÁõ »ç¿ëÀÚ °¡Àå

ƯÁ¤ ÄÚµå ¼½¼ÇÀ» ½ÇÇàÇÒ ¶§¸¸ ÀÎÁõ »ç¿ëÀÚ(User.Identity)¸¦ °¡ÀåÇÏ·Á¸é ´ÙÀ½°ú °°Àº Äڵ带 »ç¿ëÇÕ´Ï´Ù. ÀÌ ¹æ¹ýÀ» »ç¿ëÇÏ·Á¸é ÀÎÁõ »ç¿ëÀÚ ID°¡ 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();
				

Äڵ忡¼­ ƯÁ¤ »ç¿ëÀÚ °¡Àå

ƯÁ¤ ÄÚµå ¼½¼ÇÀ» ½ÇÇàÇÒ ¶§¸¸ ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ·Á¸é ´ÙÀ½°ú °°Àº Äڵ带 »ç¿ëÇÕ´Ï´Ù.

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>
				
Âü°í ½º·¹µå¿¡¼­ ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ´Â ÇÁ·Î¼¼½º ID´Â "¿î¿µ üÁ¦ÀÇ ÀϺηΠȰµ¿" ±ÇÇÑÀ» °®°í ÀÖ¾î¾ß ÇÕ´Ï´Ù. ±âº»ÀûÀ¸·Î Aspnet_wp.exe ÇÁ·Î¼¼½º´Â ASPNETÀ̶ó´Â ÄÄÇ»ÅÍ °èÁ¤À¸·Î ½ÇÇàµË´Ï´Ù. ±×·¯³ª ÀÌ °èÁ¤¿¡´Â ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ´Â µ¥ ÇÊ¿äÇÑ ±ÇÇÑÀÌ ¾ø½À´Ï´Ù. ƯÁ¤ »ç¿ëÀÚ¸¦ °¡ÀåÇÏ·Á°í ÇÏ¸é ¿À·ù ¸Þ½ÃÁö°¡ ³ªÅ¸³³´Ï´Ù. ÀÌ ¹®Á¦´Â .NET Framework 1.0¿¡¸¸ Àû¿ëµË´Ï´Ù. .NET Framework 1.1¿¡¼­´Â ÀÌ ±ÇÇÑÀÌ ÇÊ¿äÇÏÁö ¾Ê½À´Ï´Ù.

ÀÌ ¹®Á¦¸¦ ÇØ°áÇÏ·Á¸é ´ÙÀ½ ¹æ¹ý Áß Çϳª¸¦ »ç¿ëÇϽʽÿÀ.
  • "¿î¿µ üÁ¦ÀÇ ÀϺηΠÀÛµ¿ÇÒ ¼ö ÀÖ´Â" ±ÇÇÑÀ» ASPNET °èÁ¤¿¡ ºÎ¿©ÇÕ´Ï´Ù.

    Âü°í ÀÌ ¹®Á¦¸¦ ÇØ°áÇϱâ À§ÇÑ ¹æ¹ýÀ¸·Î ±ÇÀåÇÏ´Â ¹æ¹ýÀº ¾Æ´Õ´Ï´Ù.
  • Aspnet_wp.exe ÇÁ·Î¼¼½º¸¦ ½ÇÇàÇÏ´Â °èÁ¤À» Machine.config ÆÄÀÏÀÇ <processModel> ±¸¼º ¼½¼Ç¿¡ ÀÖ´Â ½Ã½ºÅÛ °èÁ¤À¸·Î º¯°æÇÕ´Ï´Ù.

ÂüÁ¶

ASP.NET º¸¾È¿¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº Microsoft ±â¼ú ÀÚ·áÀÇ ´ÙÀ½ ¹®¼­¸¦ ÂüÁ¶ÇϽʽÿÀ.
306590 INFO: ASP.NET º¸¾È °³¿ä




Microsoft Á¦Ç° °ü·Ã ±â¼ú Àü¹®°¡µé°ú ¿Â¶óÀÎÀ¸·Î Á¤º¸¸¦ ±³È¯ÇϽ÷Á¸é Microsoft ´º½º ±×·ì¿¡ Âü¿©ÇϽñ⠹ٶø´Ï´Ù.

¼Ó¼º

±â¼ú ÀÚ·á: 306158 - ¸¶Áö¸· °ËÅä: 2005³â 7¿ù 11ÀÏ ¿ù¿äÀÏ - ¼öÁ¤: 4.6
º» ¹®¼­ÀÇ Á¤º¸´Â ´ÙÀ½ÀÇ Á¦Ç°¿¡ Àû¿ëµË´Ï´Ù.
  • 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
Ű¿öµå:?
kbinfo kbsecurity KB306158

Çǵå¹é º¸³»±â