簡體   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