2

VS2005でASP.NETアプリケーションとして作成された空のテストアプリがあります。MSDNによる

既定では、ASP.NETは偽装を使用せず、コードはASP.NETアプリケーションのプロセスIDを使用して実行されます。

そして私は次の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>

したがって、記事が示唆しているように、なりすましは無効になっているようです。

私の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]前1:現在のプリンキュパル= DOMAIN \ User [5288]後:現在のプリンキュパル= DOMAIN \ User

出力はと同じです

<identity impersonate="false"/>

Webサイトはデフォルトのアプリケーションプールを使用し、プールはワーカープロセスにNETWORKSERVICEアカウントを使用するように設定されています。アプリケーションが使用する必要のあるweb.configを使用し、w3p.exeワーカープロセスがネットワークサービスで実行されていることを確認します。

この場合、何が間違っている可能性がありますか?

ありがとう!

@編集:ロブ、ヒントをありがとう!$ userショートカットは、すべてが期待どおりに行われていることを示しています。偽装すると、ユーザーNT AUTHORITY \ NETWORK SERVICEを実行しているプロセスがあり、スレッドにはWindowsIdentity.Impersonate(IntPtr.Zero)の前にDOMAIN\Userがあります。なりすましではありません。」後。ただし、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;

ここで何がポイントなのかわかりませんが、とにかく。現在、SharePoint共有サービスがユーザープロファイルのアクセス許可を管理することに問題がありますが、それは別の質問です。

4

3 に答える 3

1

奇妙に思えます、試すべきいくつかのこと:

  • 監視ウィンドウでデバッグタイプ$userのブレークポイントにいる間、プロセスとスレッドのIDが表示されます。
  • 偽装の使用は正しくありません。次のコードを試してください。

    // 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();
        }
    }
    
于 2008-10-29T16:58:16.477 に答える
1

何が HttpContext.User.Identity.Nameあなたに与えますか?

IIS内の[セキュリティ]タブで匿名アクセスが許可されていることを確認したと仮定しますか?

奇妙なローカルポリシーがあるActiveDirectory内にいますか?

于 2008-10-29T16:58:25.620 に答える