質問

私は、アップロードされたファイルをネットワーク共有に保存するASP.NETプロジェクトに取り組んでいます。仮想ディレクトリを使用すれば大丈夫だと思いましたが、Directory.CreateDirectoryの権限に苦労しています。

ファイルをアップロードできたため、コードを変更してすべてを単一のディレクトリに配置することにしましたが、これにはFile.Existsを使用して重複を上書きしないようにする必要があります。

すべてのコードが更新されたので、ネットワーク共有に対してテストを行うと、File.Existsが常にfalse(ファイルが確実に存在する)を返すことを発見しました。

アイデアはありますか?私はネットワーク共有でロープの最後に来ています。

役に立ちましたか?

解決

最近、ファイルをネットワーク共有に保存する非常によく似たプロジェクトに取り組みました。 2台のコンピューターは同じサブネット上にありますが、ドメインコントローラーによって制御されていないため、各コンピューターには独自のユーザーがいます。

両方のコンピューターで同じユーザー名とパスワードでユーザーを作成しました。次に、ネットワーク共有を作成し、フォルダー/共有のアクセス許可を設定して、ユーザーの読み取り/書き込みを許可します。

次に、偽装を管理する次のクラスを作成しました:

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

namespace MyProject.Business.Web
{
    public class SecurityManager
    {
        #region DLL Imports
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

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

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
        #endregion

        public string Domain { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        private WindowsImpersonationContext m_CurrentImpersonationContext;

        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        public void StartImpersonation()
        {
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;

            IntPtr tokenHandle = IntPtr.Zero;
            IntPtr dupeTokenHandle = IntPtr.Zero;

            // obtain a handle to an access token
            bool wasLogonSuccessful = LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

            if (!wasLogonSuccessful)
                throw new Exception(String.Format("Logon failed with error number {0}", Marshal.GetLastWin32Error()));

            // use the token handle to impersonate the user
            WindowsIdentity newId = new WindowsIdentity(tokenHandle);
            m_CurrentImpersonationContext = newId.Impersonate();

            // free the tokens
            if (tokenHandle != IntPtr.Zero)
                CloseHandle(tokenHandle);
        }
        public void EndImpersonation()
        {
            m_CurrentImpersonationContext.Undo();
        }
    }
}

その後、ASP.NETページで次のことを行いました。

SecurityManager sm = new SecurityManager();
sm.UserName = ConfigurationManager.AppSettings["UserFileShareUsername"];
sm.Password = ConfigurationManager.AppSettings["UserFileSharePassword"];
sm.StartImpersonation();

if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);

File.Move(sourcePath, destinationPath);

sm.EndImpersonation();

他のヒント

File.Existは、ファイルの存在を実際にチェックしません。代わりに、アクセスの尺度があるファイルの存在を確認します。ファイルが存在することがわかっている場合、問題の可能性は、アクセスできないことです。

実行中のコード(つまり、ASP.NETサーバーコード)は、そのネットワーク共有へのアクセス許可を持たないユーザー(IISユーザーなど)として実行されている可能性があります。

IISは、デフォルトでは他のマシンの共有を表示する権限を持っている、高度な特権を持つユーザーとして実行することは想定されていません。

同じコードを使用しましたが、クラスにIDisposableインターフェイスを実装させ、Undo()をDispose()メソッドに追加しました。あなたがそれを使用している唯一の開発者であり、あなたがいつも正しい方法でやるなら、このコードはうまくいきますか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top