仅当在web.config中指定了用户时,ASP.NET模拟才有效

时间:2012-06-26 16:56:25

标签: asp.net impersonation

首先,我道歉这是重复的,但我真的无法在任何地方找到类似的问题。

情况是我正在尝试使用asp.net中的模拟功能来检索位于网络目录中的文件。当我在web.config中指定用户时,它可以正常工作:

<identity impersonate="true" userName="contoso\Jane" password="********" />

然而,当我尝试使用以下内容时,我收到登录该网站的提示,我无法成功完成。

<identity impersonate="true"/>

我对后一个示例的理解是,它将尝试模拟当前正在查看该页面的任何人的Windows凭证(通过Windows身份验证)。这不正确吗?

我应该注意,我确实在应用程序的其他区域正常运行Windows身份验证。

由于

编辑

我还应该提一下,这是在II6上运行......它只是“感觉”就像一个配置问题......

1 个答案:

答案 0 :(得分:0)

我会使用额外的类Impersonate.cs另外一种方式,你需要一个用户,一个密码和一个域。

Imperosnate.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

using System.Web;

namespace [YourProgramName]  //You must change it
{
    public class Impersonate
    {

        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
                                            int dwLogonType, int dwLogonProvider, out int phToken);

        [DllImport("kernel32.dll")]
        private static extern int FormatMessage(int dwFlags, string lpSource, int dwMessageId, int dwLanguageId,
                                                StringBuilder lpBuffer, int nSize, string[] Arguments);


        private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
        private const int LOGON32_PROVIDER_DEFAULT = 0;
        private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;

        private static WindowsImpersonationContext winImpersonationContext = null;

        public static void ImpersonateUser(string domain, string userName, string password)
        {

            //Benutzer einloggen
            int userToken = 0;

            bool loggedOn = (LogonUser(userName, domain, password, LOGON32_LOGON_NETWORK_CLEARTEXT,
                                        LOGON32_PROVIDER_DEFAULT, out userToken) != 0);

            if (loggedOn == false)
            {
                int apiError = Marshal.GetLastWin32Error();
                StringBuilder errorMessage = new StringBuilder(1024);
                FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, null, apiError, 0, errorMessage, 1024, null);
                throw new Exception(errorMessage.ToString());
            }

            WindowsIdentity identity = new WindowsIdentity((IntPtr)userToken);
            winImpersonationContext = identity.Impersonate();

        }

        public static void UndoImpersonation()
        {
            if (winImpersonationContext != null)
            {
                winImpersonationContext.Undo();
            }
        }

    }
}

在您的计划中使用它:

string Admin = Properties.Settings.Default.Admin;
        string AdminPassword = Properties.Settings.Default.AdminPassword;
        string Domain = Properties.Settings.Default.Domain;

        Impersonate.ImpersonateUser(Domain , Admin , AdminPassword);

                                //Your Code as the new User


                      Impersonate.UndoImpersonation();

希望这是你搜索的内容^^