2

我的情况是我有一个需要连接到本地文件共享的 Azure 辅助角色。本地通过 VPN 连接到 Azure 实例。使用 Active Directory 联合服务将本地 AD 与 Azure AD 同步。辅助角色将特定文件从本地复制到 Azure 帐户中的 Blob 存储。

辅助角色是否可以模拟域帐户?如果是这样,怎么做?或者我怎样才能实现我的目标?

谢谢!

4

1 回答 1

3

我们通过使用下面的类来做到这一点

在那里,您可以通过传入我们拥有的令牌轻松调用 Impersonate 和 UndoImpersonation。请注意,将凭证安全地存储在您的云环境中很重要。

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

namespace Codit.Common.Security
{
    public static class Impersonation
    {
        [DllImport("advapi32.DLL", SetLastError = true)]
        public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

        [DllImport("advapi32.DLL")]
        public static extern bool ImpersonateLoggedOnUser(IntPtr hToken);  //handle to token for logged-on user 

        [DllImport("advapi32.DLL")]
        public static extern bool RevertToSelf();

        public static object Impersonate(string user, string password)
        {
            string domain = "";
            if (user.IndexOf(@"\") > 0)
            {
                domain = user.Substring(0, user.IndexOf(@"\"));
                user = user.Substring(user.IndexOf(@"\") + 1);
            }

            IntPtr securityToken;

            LogonUser(user, domain, password, 9, 0, out securityToken);
            if (securityToken != IntPtr.Zero)
            {
                var newIdentity = new WindowsIdentity(securityToken);
                WindowsImpersonationContext impersonationContext = newIdentity.Impersonate();

                return impersonationContext;
            }

            throw new InvalidOperationException("The username or password combination was invalid, please verify your settings");
        }

        public static void UndoImpersonation(object impersonationContext)
        {
            var context = impersonationContext as WindowsImpersonationContext;
            if (context != null) context.Undo();
        }
    }
}

您可以做的另一件事是创建一个本地侦听器(在您的本地服务器上,在所需帐户下运行)此侦听器可以轮询文件并将它们推送到 blob 存储。

于 2013-09-20T14:01:29.037 回答