繁体   English   中英

仅当在web.config中指定用户时,ASP.NET模拟才起作用

[英]ASP.NET impersonation works only when a user is specified in the web.config

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

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

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

但是,当我尝试使用以下内容时,会收到提示登录该站点的提示,但我从未成功完成该操作。

<identity impersonate="true"/>

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

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

谢谢

编辑

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

我会使用其他类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();

希望这是您搜索的^^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM