【问题标题】:Accessing Third Party Network drive when using Azure Websites使用 Azure 网站时访问第三方网络驱动器
【发布时间】:2015-06-29 09:02:39
【问题描述】:

在 azure 网站上托管我的网站/请求时,我在尝试读取存储在网络驱动器上的文件时遇到问题。

代码可以在本地机器上进行测试,它也可以在我们拥有另一个客户端并完全控制的专用服务器上运行,但是当我们尝试读取文件时它会出现以下错误。

对路径“\xxx.xxx.xxx.xxx\abc\abc.xml”的访问被拒绝。

我想知道 Azure 是否被锁定以防止这种我不知道的行为。可能是端口关闭之类的。

我正在使用的代码

if (impersonate.impersonateValidUser("username", "domain.com", "password"))
                    {
                        message = "impersonate ok";
                        if (!System.IO.File.Exists(file))
                        {
                            message = "no file";
                            impersonate.undoImpersonation();
                        }

                        using (StreamReader reader = File.OpenText(file))
                        {
                            message = "";
                            XmlSerializer deserializer = new XmlSerializer(typeof(TextModeSchema));
                            schema = (TextModeSchema)deserializer.Deserialize(reader);
                            reader.Close();
                        }
                    }

它使用了我们在很多地方使用的基本模拟类

public class Impersonate : IDisposable
    {
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_LOGON_NETWORK = 3;
        public const int LOGON32_LOGON_BATCH = 4;
        public const int LOGON32_LOGON_SERVICE = 5;
        public const int LOGON32_LOGON_UNLOCK = 7;
        public const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
        public const int LOGON32_LOGON_NEW_CREDENTIALS = 9;


        public const int LOGON32_PROVIDER_DEFAULT    =0;
        public const int LOGON32_PROVIDER_WINNT35    =1;
        public const int LOGON32_PROVIDER_WINNT40    =2;
        public const int LOGON32_PROVIDER_WINNT50 = 3;

       WindowsImpersonationContext impersonationContext;

        [DllImport("advapi32.dll")]
        public 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)]
        public static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

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

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

        public bool impersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS,
                    LOGON32_PROVIDER_DEFAULT, 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()
        {
            impersonationContext.Undo();
        }


        public void Dispose()
        {
            impersonationContext.Undo();
            GC.Collect();
        }
    }

如果在 azure 上不允许这样做,有没有人知道我们可以绕过它而无需设置完整的虚拟机的方法?理想情况下,我想保留一个 azure 网站/webapp。

【问题讨论】:

    标签: c# .net azure


    【解决方案1】:

    这是针对 Web 角色、辅助角色还是 Web 应用程序?我知道在角色的情况下,您场景中的远程主机必须与您的角色位于同一专用网络上(对于 Web 应用程序也可能如此)。此外,我们有一个类似的场景,我们决定使用 Azure 文件,它允许在您可能想要尝试的角色上安装共享。 http://blogs.msdn.com/b/windowsazurestorage/archive/2014/05/12/introducing-microsoft-azure-file-service.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2016-04-22
      • 2023-03-30
      相关资源
      最近更新 更多