Kaip ?gyvendinti apsimetimo ASP.NET programai

Straipsni? vertimai Straipsni? vertimai
Straipsnio ID: 306158 - Per?i?r?ti ?iame straipsnyje minimus produktus.
?iame straipsnyje nurodytas ?i? Microsoft .NET Pagrind? klas?s biblioteka vard?:
  • System.Web.Security
  • System.Security.Principal
  • System.Runtime.InteropServices
I?pl?sti visus | Sutraukti visus

?iame puslapyje

Santrauka

?iame straipsnyje apra?oma ?vairi? b?d?, kaip ?gyvendinti Apsimetin?jimas ? ASP.NET programai.

Daugiau informacijos

Jei j?s norite apsimesti ASP.NET, gijos vartotojo galite naudoti vien? i? ?i? metod?, pagal j?s? reikalavimus: Pastaba J?s galite naudoti ?? kod? nustatyti kas viskas gija vykdo kaip:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
				

Apsimesti patvirtintas IIS paskyr? ar vartotojo

Apsimesti Microsoft interneto informacijos paslaugose (IIS) autentifikavimo vartotojo apie kiekvien? pra?ym? d?l kiekvieno puslapio dalyje ASP.NET programai, turite ?traukti yra <identity></identity> ?ymel? Web.config faile ?ios taikomosios programos ir nustatykite atribut? apsimestiTrue. Pvz.:
<identity impersonate="true" />
				

Apsimesti konkre?iam vartotojui visus pra?ymus, ASP.NET programai

Apsimesti konkre?iam vartotojui visus pra?ymus visuose puslapiuose i? ASP.NET programai, galite nurodyti vartotojo vard? ir slapta?od? atribut?, <identity></identity> ?yma, kad taikyti, faile Web.config. Pvz.:
<identity impersonate="true" userName="accountname" password="password" />
				
Pastaba Proces?, kuris impersonates konkre?iam vartotojui d?l tapatyb?s si?lai turi tur?ti "Veikia kaip operacin?s sistemos dalis" privilegija. I? numatyta, Aspnet_wp.exe procesas veikia pagal kompiuterio abonemento pavadinim? ASPNET. Ta?iau ?is abonementas neturi reikiam? teisi? apsimesti, konkre?iam vartotojui. Gaunate klaidos prane?im? bandydami apsimesti, konkre?iam vartotojui. ?i informacija taikoma tik .NET pagrind? 1,0. Tai teis? n?ra b?tinas .NET sistemos 1.1.

Nor?dami i?spr?sti ?i? problem?, naudokite vien? i? ?i? metod?:
  • Suteikti "Veikia kaip operacin?s sistemos dalis" privilegija ? ASPNET s?skait? (skurd iausiomis kategorijomis, s?skaita).

    Pastaba Nor?dami i?spr?sti ?i? problem?, galite naudoti ?? metod? Microsoft nerekomenduoja ?is metodas.
  • Pakeiskite paskyr?, Aspnet_wp.exe procesas prasideda pagal sistem? sudaro <processModel>konfig?racijos skyriuje Machine.config failo.</processModel>

Apsimesti autentifikavimo vartotojo kodas

Apsimesti autentifikavimo vartotojo (User.Identity) tik paleidus kad konkreti sekcija kodas, j?s galite naudoti su kodas laikytis. ?is metodas reikalauja, kad autentifikavimo vartotojo tapatyb? tipo 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();
				

Apsimesti konkre?iam vartotojui kodas

Apsimesti konkre?iam vartotojui tik tada, kai jums paleisti tam tikr? skyrius, kodas, naudokite ?? kod?:

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>
				
Pastaba Proces?, kuris impersonates konkre?iam vartotojui d?l tapatyb?s si?lai turi tur?ti "Veikia kaip operacin?s sistemos dalis" privilegija Jei Aspnet_wp.exe procesas veikia "Microsoft Windows 2000" kompiuteryje. ? Jeigu "Veikia kaip operacin?s sistemos dalis" privilegija neb?tina, Aspnet_wp.exe procesas veikia kompiuteryje su Windows XP arba Windows Server 2003 m. kompiuteryje. Numatyta, kad Aspnet_wp.exe procesas veikia pagal kompiuterio abonemento pavadinim? ASPNET. Ta?iau ?is abonementas n?ra Neturite reikiam? teisi? apsimesti konkre?iam vartotojui. Gaunate yra klaidos prane?imas, jei bandote apsimesti konkre?iam vartotojui. .

Nor?dami i?spr?sti ?i? problem?, naudokite vien? i? ?i? metod?:
  • Suteikti "Veikia kaip operacin?s sistemos dalis" privilegija ? ASPNET s?skait?.

    Pastaba Mes rekomenduojame ?? metod? Nor?dami i?spr?sti, problema.
  • Pakeiskite paskyr?, Aspnet_wp.exe procesas prasideda pagal sistem? sudaro <processModel>konfig?racijos skyriuje Machine.config failo.</processModel>

Nuorodos

Papildomos informacijos apie ASP.NET saugumo, spustel?kite toliau straipsnio numer? ir per?i?r?kite straipsn?, ir Microsoft ?ini? baz?je:
306590ASP.NET saugumo ap?valga

Savyb?s

Straipsnio ID: 306158 - Paskutin? per?i?ra: 2013 m. bir?elio 6 d. - Per?i?ra: 1.0
Taikoma:
  • 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
Rakta?od?iai: 
kbinfo kbsecurity kbmt KB306158 KbMtlt
Atliktas automatinis vertimas
SVARBU: ?is straipsnis i?verstas naudojant ?Microsoft? ma?ininio vertimo programin? ?rang? ir gali b?ti pataisytas naudojant ?Community Translation Framework? (CTF) technologij?. ?Microsoft? si?lo ma?inos i?verstus ir po to bendruomen?s suredaguotus straipsnius, taip pat ?mogaus i?verstus straipsnius siekdama suteikti prieig? prie vis? savo ?ini? baz?s straipsni? daugeliu kalb?. Ma?inos i?verstuose ir v?liau paredaguotuose straipsniuose gali b?ti ?odyno, sintaks?s ir / arba gramatikos klaid?. ?Microsoft? neatsako u? jokius netikslumus, klaidas arba ?al?, patirt? d?l neteisingo turinio vertimo arba m?s? klient? naudojimosi juo. Daugiau apie CTF ?r. http://support.microsoft.com/gp/machine-translation-corrections.
Spustel?kite ?ia, nor?dami pamatyti ?io straipsnio versij? angl? kalba: 306158

Pateikti atsiliepim?

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com