簡體   English   中英

用戶通過身份驗證后,文件上傳失敗。 使用IIS7集成模式

[英]File upload fails when user is authenticated. Using IIS7 Integrated mode

這些是我的網站告訴我的用戶身份,它使用:

登錄: NT AUTHORITY \\ NETWORK SERVICE (完全無法寫入任何文件)

未登錄: WSW32 \\ IUSR_77 (可以將文件寫入任何文件夾)

我在共享主機IIS7 Web服務器上有一個ASP.NET 4.0網站,該服務器以集成模式運行,並啟用了32位應用程序支持並支持MSSQL2008。使用經典模式不是一種選擇,因為我需要保護一些靜態文件並且使用路由。

在我的web.config文件中,進行了以下設置:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

我的托管公司說,默認情況下,在計算機級別啟用了模擬功能,因此我無法更改。

我請求他們的支持,他們向我推薦了這篇文章: http : //www.codinghub.net/2010/08/differences-between-integrated-mode-and.html

引用此部分:

表單身份驗證中的不同Windows身份

當應用程序使用表單身份驗證並允許匿名訪問時,集成模式身份與經典模式身份在以下方面有所不同:

 * ServerVariables["LOGON_USER"] is filled. * Request.LogognUserIdentity uses the credentials of the [NT AUTHORITY\\NETWORK SERVICE] account instead of the [NT AUTHORITY\\INTERNET USER] account. 

出現此現象的原因在於身份驗證是在集成模式下的單個階段中執行的。 相反,在經典模式下,首先使用匿名訪問的IIS 7.0進行身份驗證,然后使用窗體身份驗證的ASP.NET進行身份驗證。 因此,身份驗證的結果始終是一個用戶-Forms身份驗證用戶。 AUTH_USER / LOGON_USER返回同一用戶,因為Forms身份驗證用戶憑據在IIS 7.0和ASP.NET之間同步。

副作用是LOGON_USER,HttpRequest.LogonUserIdentity和模擬不再可以訪問IIS 7.0通過使用經典模式已通過身份驗證的匿名用戶憑據。

如何設置我的網站,以便它可以使用具有適當權限的適當身份?

我一直在尋找有關此特定問題的任何答案,但到目前為止卻沒有發現...

希望您能提供幫助!

[ 凹凸 ]

因此,我終於找到了解決問題的方法。

通過使用這篇知識庫文章“如何在ASP.NET應用程序中實現模擬/用代碼模擬特定用戶”,我找到了一種模擬共享的托管FTP用戶的方法

這樣,我將獲得所述用戶的特權,而不會通過降低NT AUTHORITY \\ NETWORK SERVICE用戶的安全性來損害服務器的安全性。

這是我使用的代碼:

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Configuration;

namespace App_Code
{
    public class Impersonation : IDisposable
    {
        private WindowsImpersonationContext _impersonationContext;

        #region Win32 API Declarations
        private const int Logon32LogonInteractive = 2; //This parameter causes LogonUser to create a primary token.
        private const int Logon32ProviderDefault = 0;

        [DllImport("advapi32.dll")]
        private static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool RevertToSelf();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern bool CloseHandle(IntPtr handle);
        #endregion

        public bool ImpersonateDefaultFtpUser()
        {
            return ImpersonateFtpUser(ConfigurationManager.AppSettings["ftpUsername"], ConfigurationManager.AppSettings["ftpDomain"], ConfigurationManager.AppSettings["ftpPassword"]);
        }

        public bool ImpersonateFtpUser(string userName, string domain, string password)
        {
            WindowsIdentity tempWindowsIdentity;
            var token = IntPtr.Zero;
            var tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, Logon32LogonInteractive, Logon32ProviderDefault, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        _impersonationContext = tempWindowsIdentity.Impersonate();

                        if (_impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }

            if (token != IntPtr.Zero)
                CloseHandle(token);

            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);

            return false;
        }

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

        /// <summary>
        /// Constructor. Impersonates the default ftp user. Impersonation lasts until
        /// the instance is disposed.
        /// </summary>
        public Impersonation()
        {
            ImpersonateDefaultFtpUser();
        }

        /// <summary>
        /// Constructor. Impersonates the requested user. Impersonation lasts until
        /// the instance is disposed.
        /// </summary>
        public Impersonation(string userName, string domain, string password)
        {
            ImpersonateFtpUser(userName, domain, password);
        }

        #region IDisposable Pattern

        /// <summary>
        /// Revert to original user and cleanup.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing) return;

            // Revert to original user identity
            UndoImpersonation();

            if (_impersonationContext != null)
                _impersonationContext.Dispose();
        }

        /// <summary>
        /// Explicit dispose.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Destructor
        /// </summary>
        ~Impersonation()
        {
            Dispose(false);
        }

        #endregion
    }
}

您可以授予用戶Networkservice對有關目錄的寫權限嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM