0

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

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

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

但是,当我尝试使用以下内容时,我会收到登录该站点的提示,但我永远无法成功。

<identity impersonate="true"/>

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

我应该注意,我的 Windows 身份验证在应用程序的其他区域工作正常。

谢谢

编辑

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

4

1 回答 1

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();

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

于 2012-07-24T08:10:56.730 回答