2008-10-29 23 views
3

我有一個空白的測試應用程序在VS 2005中創建爲ASP.NET應用程序。 MSDN saysASP.NET aspx頁面代碼運行模擬,儘管模擬被禁用

默認情況下,ASP.NET不使用模擬,並且您的代碼使用ASP.NET應用程序的進程標識運行。

而且我有以下的web.config

<configuration> 

    <appSettings/> 
    <connectionStrings/> 

    <system.web> 
     <!-- 
      Set compilation debug="true" to insert debugging 
      symbols into the compiled page. Because this 
      affects performance, set this value to true only 
      during development. 
     --> 
     <compilation debug="true" defaultLanguage="c#" /> 
     <!-- 
      The <authentication> section enables configuration 
      of the security authentication mode used by 
      ASP.NET to identify an incoming user. 
     --> 
     <authentication mode="Windows"/> 
     <identity impersonate="false"/> 
     <!-- 
      The <customErrors> section enables configuration 
      of what to do if/when an unhandled error occurs 
      during the execution of a request. Specifically, 
      it enables developers to configure html error pages 
      to be displayed in place of a error stack trace. 

     <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 
      <error statusCode="403" redirect="NoAccess.htm" /> 
      <error statusCode="404" redirect="FileNotFound.htm" /> 
     </customErrors> 
     --> 
    </system.web> 
</configuration> 

所以它看起來模擬被禁用就像the article是在暗示。

我的aspx是空白的默認設置,代碼隱藏是

namespace TestWebapp 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      System.Diagnostics.Debug.WriteLine(String.Format("Before1: Current Princupal = {0}", Thread.CurrentPrincipal.Identity.Name)); 
      WindowsImpersonationContext ctx = WindowsIdentity.Impersonate(IntPtr.Zero); 
      try 
      { 
       int a = 0; 
       System.Diagnostics.Debug.WriteLine(String.Format("After: Current Princupal = {0}", Thread.CurrentPrincipal.Identity.Name)); 
      } finally 
      { 
       ctx.Undo(); 
      } 

     } 
    } 
} 

當我重新加載頁面我得到以下調試輸出:

[5288] Before1:當前Princupal = 域\用戶 [5288]之後:當前Princupal = DOMAIN \用戶

Outpu t與

<identity impersonate="false"/> 

該網站使用默認應用程序池,並且該池設置爲使用NETWORK SERVICE帳戶作爲其工作進程。 我確定應用程序使用它應該使用的web.config,並且w3p.exe工作進程正在NETWORK SERVICE下運行。

在這種情況下會出現什麼問題?

謝謝!

@編輯:搶,感謝您的提示! $用戶快捷方式顯示我發生的一切,如我所期望的:與模擬我有進程運行用戶NT AUTHORITY \ NETWORK SERVICE,並且線程有WindowsIdentity.Impersonate(IntPtr.Zero)和「No Token」之前的域\用戶。線程不模仿。「後。 但Thread.CurrentPrincipal.Identity.Name和HttpContext.Current.User.Identity.Name仍然給我DOMAIN \ User在這兩個地方。

@Edit:我發現,要獲得和Thread.CurrentPrincipal中改變HttpContext.Current.User我必須做手工:

Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
HttpContext.Current.User = Thread.CurrentPrincipal; 

我不知道有什麼意義在這裏,但無論如何。我現在遇到了共享點共享服務管理用戶配置文件許可的問題,但這是另一個問題。

回答

1

似乎很奇怪,有幾件事情嘗試:

  • 雖然在在監視窗口調試類型$用戶斷點,會告訴你的進程和線程的身份。
  • 您使用冒充的不正確,試試這個代碼:

    // Declare the logon types as constants 
    const long LOGON32_LOGON_INTERACTIVE = 2; 
    const long LOGON32_LOGON_NETWORK = 3; 
    
    // Declare the logon providers as constants 
    const long LOGON32_PROVIDER_DEFAULT = 0; 
    const long LOGON32_PROVIDER_WINNT50 = 3; 
    const long LOGON32_PROVIDER_WINNT40 = 2; 
    const long LOGON32_PROVIDER_WINNT35 = 1; 
    
    [DllImport("advapi32.dll", EntryPoint = "LogonUser")] 
    private static extern bool LogonUser(
        string lpszUsername, 
        string lpszDomain, 
        string lpszPassword, 
        int dwLogonType, 
        int dwLogonProvider, 
        ref IntPtr phToken); 
    
    public static WindowsImpersonationContext ImpersonateCurrentUserBegin(System.Net.NetworkCredential credential) 
    { 
        WindowsImpersonationContext impersonationContext = null; 
        if (credential == null || credential.UserName.Length == 0 || credential.Password.Length == 0 || credential.Domain.Length == 0) 
        { 
         throw new Exception("Incomplete user credentials specified"); 
        } 
        impersonationContext = Security.Impersonate(credential); 
        if (impersonationContext == null) 
        { 
         return null; 
        } 
        else 
        { 
         return impersonationContext; 
        } 
    } 
    
    public static void ImpersonateCurrentUserEnd(WindowsImpersonationContext impersonationContext) 
    { 
        if (impersonationContext != null) 
        { 
         impersonationContext.Undo(); 
        } 
    } 
    
+0

感謝您關於$ user的這個技巧。對於$用戶,它顯示(我現在有)一切都如我所料。在WindowsIdentity.Impersonate(IntPtr.Zero)之前存在模擬用戶的標記,並且該進程處於NETWORK SERVICE – axk 2008-10-29 17:42:57

+0

下但是System.Threading.Thread.CurrentPrincipal.Identity.Name和HttpContext.Current.User.Identity .Name仍然給我模擬的用戶。 – axk 2008-10-29 17:43:39

1

是什麼HttpContext.User.Identity.Name給你?

假設您已經檢查過IIS中的安全選項卡,它允許匿名訪問?

你是否在一個有一些奇怪的本地政策的活動目錄?